-
Notifications
You must be signed in to change notification settings - Fork 281
/
openIdMetadata.ts
145 lines (128 loc) · 4.17 KB
/
openIdMetadata.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @module botframework-connector
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import getPem from 'rsa-pem-from-mod-exp';
import base64url from 'base64url';
import fetch from 'node-fetch';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { AuthenticationError } from './authenticationError';
import { StatusCodes } from 'botframework-schema';
import { ProxySettings } from '@azure/core-http';
/**
* Class in charge of manage OpenId metadata.
*/
export class OpenIdMetadata {
private lastUpdated = 0;
private keys: IKey[];
/**
* Initializes a new instance of the [OpenIdMetadata](xref:botframework-connector.OpenIdMetadata) class.
*
* @param url Metadata Url.
* @param proxySettings The proxy settings for the request.
*/
constructor(private url: string, private proxySettings?: ProxySettings) {}
/**
* Gets the Signing key.
*
* @param keyId The key ID to search for.
* @returns A `Promise` representation for either a [IOpenIdMetadataKey](botframework-connector:module.IOpenIdMetadataKey) or `null`.
*/
async getKey(keyId: string): Promise<IOpenIdMetadataKey | null> {
// If keys are more than 24 hours old, refresh them
if (this.lastUpdated < Date.now() - 1000 * 60 * 60 * 24) {
await this.refreshCache();
// Search the cache even if we failed to refresh
const key = this.findKey(keyId);
return key;
} else {
// Otherwise read from cache
const key = this.findKey(keyId);
// Refresh the cache if a key is not found (max once per hour)
if (!key && this.lastUpdated < Date.now() - 1000 * 60 * 60) {
await this.refreshCache();
return this.findKey(keyId);
}
return key;
}
}
/**
* @private
*/
private async refreshCache(): Promise<void> {
let agent = null;
if (this.proxySettings) {
const proxyUrl = `http://${this.proxySettings.host}:${this.proxySettings.port}`;
agent = new HttpsProxyAgent(proxyUrl);
}
const res = await fetch(this.url, { agent: agent });
if (res.ok) {
const openIdConfig = (await res.json()) as IOpenIdConfig;
const getKeyResponse = await fetch(openIdConfig.jwks_uri, { agent: agent });
if (getKeyResponse.ok) {
this.lastUpdated = new Date().getTime();
this.keys = (await (getKeyResponse.json() as Promise<IOpenIdResponse>)).keys;
} else {
throw new AuthenticationError(
`Failed to load Keys: ${getKeyResponse.status}`,
StatusCodes.INTERNAL_SERVER_ERROR
);
}
} else {
throw new AuthenticationError(
`Failed to load openID config: ${res.status}`,
StatusCodes.INTERNAL_SERVER_ERROR
);
}
}
/**
* @private
*/
private findKey(keyId: string): IOpenIdMetadataKey | null {
if (!this.keys) {
return null;
}
for (const key of this.keys) {
if (key.kid === keyId) {
if (!key.n || !key.e) {
// Return null for non-RSA keys
return null;
}
const modulus = base64url.toBase64(key.n);
const exponent = key.e;
return {
key: getPem(modulus, exponent),
endorsements: key.endorsements,
};
}
}
return null;
}
}
interface IOpenIdConfig {
issuer: string;
authorization_endpoint: string;
jwks_uri: string;
id_token_signing_alg_values_supported: string[];
token_endpoint_auth_methods_supported: string[];
}
interface IOpenIdResponse {
keys: IKey[];
}
interface IKey {
kty: string;
use: string;
kid: string;
x5t: string;
n: string;
e: string;
x5c: string[];
endorsements?: string[];
}
export interface IOpenIdMetadataKey {
key: string;
endorsements?: string[];
}