/
wwwAuthenticateParser.ts
51 lines (48 loc) · 1.76 KB
/
wwwAuthenticateParser.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* Defined supported names of WWW-Authenticate name-value pairs.
*/
export type ValidParsedWWWAuthenticateProperties =
// "authorization_uri" was used in the track 1 version of KeyVault.
// This is not a relevant property anymore, since the service is consistently answering with "authorization".
// | "authorization_uri"
| "authorization"
| "claims"
// Even though the service is moving to "scope", both "resource" and "scope" should be supported.
| "resource"
| "scope"
| "service";
/**
* Represents the result of `parseWWWAuthenticate()`;
*/
export type ParsedWWWAuthenticate = {
[Key in ValidParsedWWWAuthenticateProperties]?: string;
};
/**
* Parses an WWW-Authenticate response.
* This transforms a string value like:
* `Bearer authorization="some_authorization", resource="https://some.url"`
* into an object like:
* `{ authorization: "some_authorization", resource: "https://some.url" }`
* @param wwwAuthenticate - String value in the WWW-Authenticate header
*/
export function parseWWWAuthenticate(wwwAuthenticate: string): ParsedWWWAuthenticate {
// First we split the string by either `,`, `, ` or ` `.
const parts = wwwAuthenticate.split(/, *| +/);
// Then we only keep the strings with an equal sign after a word and before a quote.
// also splitting these sections by their equal sign
const keyValues = parts.reduce<string[][]>(
(acc, str) => (str.match(/\w="/) ? [...acc, str.split("=")] : acc),
[]
);
// Then we transform these key-value pairs back into an object.
const parsed = keyValues.reduce<ParsedWWWAuthenticate>(
(result, [key, value]: string[]) => ({
...result,
[key]: value.slice(1, -1)
}),
{}
);
return parsed;
}