-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
parseWWWAuthenticate.ts
73 lines (67 loc) · 2.17 KB
/
parseWWWAuthenticate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* @internal
*
* Valid key names in WWW-Authenticate header.
*/
const validParsedWWWAuthenticateProperties = [
"authorization",
"authorization_url",
"resource",
"scope",
"tenantId",
] as const;
/**
* @internal
*
* A union type representing all valid key names in WWW-Authenticate header.
*/
type ValidParsedWWWAuthenticateProperties = typeof validParsedWWWAuthenticateProperties[number];
/**
* @internal
*
* Holds the known WWWAuthenticate keys and their values as a result of
* parsing a WWW-Authenticate header.
*/
export type ParsedWWWAuthenticate = {
[Key in ValidParsedWWWAuthenticateProperties]?: string;
};
/**
* Parses an WWW-Authenticate response.
* This transforms a string value like:
* `Bearer authorization="https://some.url/tenantId", resource="https://some.url"`
* into an object like:
* `{ authorization: "https://some.url/tenantId", resource: "https://some.url" }`
* @param wwwAuthenticate - String value in the WWW-Authenticate header
*/
export function parseWWWAuthenticate(wwwAuthenticate: string): ParsedWWWAuthenticate {
const pairDelimiter = /,? +/;
const parsed = wwwAuthenticate
.split(pairDelimiter)
.reduce<ParsedWWWAuthenticate>((kvPairs, p) => {
if (p.match(/\w="/)) {
// 'sampleKey="sample_value"' -> [sampleKey, "sample_value"] -> { sampleKey: sample_value }
const [key, value] = p.split("=");
if (
validParsedWWWAuthenticateProperties.includes(key as ValidParsedWWWAuthenticateProperties)
) {
// The values will be wrapped in quotes, which need to be stripped out.
return { ...kvPairs, [key]: value.slice(1, -1) };
}
}
return kvPairs;
}, {});
// Finally, we pull the tenantId from the authorization header to support multi-tenant authentication.
if (parsed.authorization) {
try {
const tenantId = new URL(parsed.authorization).pathname.substring(1);
if (tenantId) {
parsed.tenantId = tenantId;
}
} catch (_) {
throw new Error(`The challenge authorization URI '${parsed.authorization}' is invalid.`);
}
}
return parsed;
}