/
wwwAuthenticateParser.ts
46 lines (40 loc) · 1.53 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
// 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 {
// Match on things that look like property name/value pairs. We assume that values are quoted,
// this hasn't been an issue yet...
const pattern = /(\w+?)="(.*?)"/g;
const value: ParsedWWWAuthenticate = {};
let match: string[] | null;
while ((match = pattern.exec(wwwAuthenticate)) !== null) {
value[match[1] as keyof ParsedWWWAuthenticate] = match[2];
}
return value;
}