-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
477 lines (423 loc) · 15.9 KB
/
index.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import {
ErrorInvalidReturnedStateParam,
ErrorNoAccessToken,
ErrorNoAuthCode,
toErrorObject
} from './errors';
import {
extractParamFromUrl,
generatePKCECodeChallengeAndVerifier,
generateRandomState,
objectToQueryString,
parseWwwAuthenticateHeader
} from './helpers';
export * from './errors';
export type ObjStringDict = { [_: string]: string };
export type URL = string;
export interface Configuration {
authorizationUrl: URL;
clientId: string;
onAccessTokenExpiry?: () => Promise<AccessContext>;
onInvalidGrant?: () => Promise<any> | void;
onInvalidToken?: () => Promise<any> | void;
redirectUrl: URL;
scopes?: string[];
tokenUrl: URL;
extraAuthorizationParams?: ObjStringDict;
extraRefreshParams?: ObjStringDict;
storeRefreshToken?: boolean;
}
interface State {
accessToken?: string;
accessTokenExpiry?: string;
authorizationCode?: string;
codeChallenge?: string;
codeVerifier?: string;
idToken?: string;
refreshToken?: string;
stateQueryParam?: string;
scopes?: string[];
}
export type Scopes = string[];
export interface AccessContext {
accessToken?: string;
idToken?: string;
refreshToken?: string;
scopes?: Scopes;
}
export interface TokenResponse {
accessToken?: string;
expiresIn?: string;
scope?: string;
refreshToken?: string;
idToken?: string;
}
export interface Storage {
saveState(serializedState: string): void | Promise<void>;
loadState(): string | null | Promise<string | null>;
}
const HEADER_AUTHORIZATION = 'Authorization';
const HEADER_WWW_AUTHENTICATE = 'WWW-Authenticate';
/**
* A sensible length for the state's length, for anti-csrf.
*/
export const RECOMMENDED_STATE_LENGTH = 32;
type FetchFunc = (input: Request | string, ...rest: any[]) => Promise<Response>;
/**
* OAuth 2.0 client that ONLY supports authorization code flow with PKCE.
*
* Many applications structure their OAuth usage in different ways. This class
* aims to provide both flexible and easy ways to use this configuration of
* OAuth.
*/
export class OAuth2AuthCodePkceClient {
readonly config: Configuration;
private state: State = { };
private authCodeForAccessTokenPromise?: Promise<TokenResponse>;
private refreshTokenForAccessTokenPromise?: Promise<TokenResponse>;
private refreshToken: string;
private storage: Storage;
private ready: Promise<void>;
private setReady: Function;
constructor(config: Configuration, storage?: Storage) {
this.config = config;
this.storage = storage || LocalStorage;
this.ready = new Promise(resolve => this.setReady = resolve);
this.recoverState();
}
/**
* Resets the state of the client. Equivalent to "logging out" the user.
*/
public async reset() {
this.state = { };
await this.saveState();
this.authCodeForAccessTokenPromise = undefined;
this.refreshTokenForAccessTokenPromise = undefined;
}
/**
* Fetch an authorization code via redirection. In a sense this function
* doesn't return because of the redirect behavior (uses `location.replace`).
*
* @param oneTimeParams A way to specify "one time" query string
* parameters during the authorization code fetching process, usually for
* values which need to change at run-time.
*/
public async requestAuthorizationCode(oneTimeParams?: ObjStringDict) {
const { clientId, extraAuthorizationParams, redirectUrl, scopes } = this.config;
const { codeChallenge, codeVerifier } = await generatePKCECodeChallengeAndVerifier();
const stateQueryParam = generateRandomState(RECOMMENDED_STATE_LENGTH);
this.state = {
...this.state,
codeChallenge,
codeVerifier,
stateQueryParam
};
this.saveState();
let url = this.config.authorizationUrl
+ '?response_type=code&'
+ `client_id=${encodeURIComponent(clientId)}&`
+ `redirect_uri=${encodeURIComponent(redirectUrl)}&`
+ `state=${stateQueryParam}&`
+ `code_challenge=${encodeURIComponent(codeChallenge)}&`
+ 'code_challenge_method=S256';
if (scopes) {
url += `&scope=${encodeURIComponent(scopes.join(' '))}`;
}
if (extraAuthorizationParams || oneTimeParams) {
const extraParameters: ObjStringDict = {
...extraAuthorizationParams,
...oneTimeParams
};
url += `&${objectToQueryString(extraParameters)}`;
}
location.replace(url);
}
/**
* Check if it looks like we are coming back from requesting an auth code.
*/
public isReturningFromAuthServer(): boolean {
return !!extractParamFromUrl('code', location.href);
}
/**
* Read the code from the URL and store it.
*/
public async receiveCode() {
await this.ready;
const error = extractParamFromUrl('error', location.href);
if (error) {
throw toErrorObject(error);
}
const stateQueryParam = extractParamFromUrl('state', location.href);
if (stateQueryParam !== this.state.stateQueryParam) {
console.warn('"state" parameter doesn\'t match the one sent! ' +
'Possible malicious activity.');
throw new ErrorInvalidReturnedStateParam();
}
this.state.authorizationCode = extractParamFromUrl('code', location.href);
if (!this.state.authorizationCode) {
throw new ErrorNoAuthCode();
}
this.saveState();
}
/**
* Using a previously fetched authorization code try to get the auth tokens.
* If there is no authorization code return the previously fetched access token.
*
* @param oneTimeParams A way to specify "one time" query string
* parameters when fetching tokens from the auth server, usually for
* values which need to change at run-time.
*/
public async getTokens(oneTimeParams?: ObjStringDict): Promise<AccessContext> {
const {
accessToken,
authorizationCode,
idToken,
refreshToken,
scopes
} = this.state;
if (authorizationCode) {
return this.exchangeAuthCodeForAccessToken(oneTimeParams);
}
if (!accessToken) {
throw new ErrorNoAccessToken();
}
if (this.isAccessTokenExpired()) {
if (this.config.onAccessTokenExpiry) {
return this.config.onAccessTokenExpiry();
}
}
return Promise.resolve({ accessToken, idToken, refreshToken, scopes });
}
/**
* Fetch an access token from the remote service.
* This gets implicitly called by `getTokens()`.
*/
public async exchangeAuthCodeForAccessToken(oneTimeParams?: ObjStringDict): Promise<AccessContext> {
if (!this.authCodeForAccessTokenPromise) {
this.authCodeForAccessTokenPromise = this.fetchAccessTokenUsingCode(oneTimeParams);
}
const tokenResponse = await this.authCodeForAccessTokenPromise;
this.authCodeForAccessTokenPromise = undefined;
this.state.authorizationCode = undefined;
return this.setTokens(tokenResponse);
}
/**
* Refresh an access token from the remote service.
*/
public async exchangeRefreshTokenForAccessToken(): Promise<AccessContext> {
if (!this.refreshTokenForAccessTokenPromise) {
this.refreshTokenForAccessTokenPromise = this.fetchAccessTokenUsingRefreshToken();
}
const tokenResponse = await this.refreshTokenForAccessTokenPromise;
this.refreshTokenForAccessTokenPromise = undefined;
return this.setTokens(tokenResponse);
}
/**
* Make a `fetch()` function that retries in case an access token is not valid any more.
*/
public makeRetryFetchFunction(fetchFunc: FetchFunc): FetchFunc {
return async (input: Request | string, ...rest): Promise<Response> => {
const response = await fetchFunc(input, ...rest);
if (response.status === 401) {
const authenticateHeader = response.headers.get(
HEADER_WWW_AUTHENTICATE.toLowerCase()
);
if (authenticateHeader) {
const error = parseWwwAuthenticateHeader(authenticateHeader).error;
if (error === 'invalid_token') {
await this.exchangeRefreshTokenForAccessToken();
input = await this.requestInterceptor(input as Request);
return fetchFunc(input, ...rest);
}
}
}
return response;
};
}
/**
* Make a `fetch()` function that has both the `requestInterceptor` and the
* `responseInterceptor` attached, which add the OAuth logic to all fetch requests
* and handle / translate errors.
* This function can be used if the host application / framework does not provide
* a request / response processing mechanism.
*/
public decorateFetchWithInterceptors(fetchFunc: FetchFunc): FetchFunc {
return async (input: Request | string, ...rest): Promise<Response> => {
if (typeof input === 'string') {
input = new Request(input);
}
input = await this.requestInterceptor(input);
const response = await fetchFunc(input, ...rest);
return this.responseInterceptor(response);
};
}
/**
* Put the access token on `fetch()` `Request`s. Gets a fresh access token
* if the current one is invalid.
* This function is meant to be wired into the request processing of an app / a framework.
*
* @see decorateFetchWithInterceptors
*/
public async requestInterceptor(request: Request) {
const tokenContext = await this.getTokens();
request.headers.set(HEADER_AUTHORIZATION, `Bearer ${tokenContext.accessToken}`);
return request;
}
/**
* Handle auth related errors in `fetch()` `Response`s.
* This function is meant to be wired into the response processing of an app / a framework.
*
* @see decorateFetchWithInterceptors
*/
public async responseInterceptor(response: Response) {
if (response.status !== 401) {
return response;
}
const authenticateHeader = response.headers.get(HEADER_WWW_AUTHENTICATE.toLowerCase());
if (authenticateHeader) {
const error = parseWwwAuthenticateHeader(authenticateHeader).error;
if (error === 'invalid_grant' && this.config.onInvalidGrant) {
await this.config.onInvalidGrant();
}
if (error === 'invalid_token' && this.config.onInvalidToken) {
await this.config.onInvalidToken();
}
throw toErrorObject(error);
}
return response;
}
/**
* Get the scopes that were granted by the authorization server.
*/
public getGrantedScopes(): Scopes | undefined {
return this.state.scopes;
}
/**
* Tells if the client is authorized or not. This means the client has at
* least once successfully fetched an access token. The access token could be
* expired.
*/
public isAuthorized(): boolean {
return !!this.state.accessToken;
}
/**
* Checks to see if the access token has expired.
*/
public isAccessTokenExpired(): boolean {
const { accessTokenExpiry } = this.state;
return Boolean(accessTokenExpiry && (new Date()) >= (new Date(accessTokenExpiry)));
}
/**
* Use the current grant code to fetch a fresh authorization token.
*/
private async fetchAccessTokenUsingCode(oneTimeParams?: ObjStringDict) {
const { authorizationCode, codeVerifier = '' } = this.state;
const { clientId, redirectUrl} = this.config;
if (!codeVerifier) {
console.warn('No code verifier is being sent.');
}
else if (!authorizationCode) {
console.warn('No authorization grant code is being passed.');
}
const url = this.config.tokenUrl;
const paramsObject = {
grant_type: 'authorization_code',
code: authorizationCode,
redirect_uri: redirectUrl,
client_id: clientId,
code_verifier: codeVerifier,
...oneTimeParams
}
const body = new URLSearchParams(paramsObject).toString();
return this.makeTokenRequest(url, body);
}
/**
* Fetch a new access token using the refresh token.
*/
private fetchAccessTokenUsingRefreshToken() {
const { extraRefreshParams, clientId, tokenUrl } = this.config;
const { refreshToken } = this.state;
if (!refreshToken) {
console.warn('No refresh token is present.');
}
const url = tokenUrl;
let body = 'grant_type=refresh_token&'
+ `refresh_token=${refreshToken}&`
+ `client_id=${clientId}`;
if (extraRefreshParams) {
body = `${url}&${objectToQueryString(extraRefreshParams)}`;
}
return this.makeTokenRequest(url, body);
}
private async makeTokenRequest(url: string, body: string): Promise<TokenResponse> {
const tokenResponse = await fetch(url, {
method: 'POST',
body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
const jsonContent = await tokenResponse.json();
if (!tokenResponse.ok) {
if (jsonContent.error === 'invalid_grant' && this.config.onInvalidGrant) {
await this.config.onInvalidGrant();
}
throw toErrorObject(jsonContent.error);
}
const { access_token, expires_in, id_token, refresh_token, scope } = jsonContent;
return {
accessToken: access_token,
expiresIn: expires_in,
idToken: id_token,
refreshToken: refresh_token,
scope
};
}
private async setTokens(tokenResponse: TokenResponse): Promise<AccessContext> {
const { accessToken, expiresIn, idToken, refreshToken, scope } = tokenResponse;
this.state.accessToken = accessToken;
this.state.accessTokenExpiry = (new Date(Date.now() + (parseInt(expiresIn, 10) * 1000)))
.toString();
if (idToken) {
this.state.idToken = idToken;
}
if (refreshToken) {
this.state.refreshToken = refreshToken;
}
if (scope) {
// Multiple scopes are passed and delimited by spaces,
// despite using the singular name "scope".
this.state.scopes = scope.split(' ');
}
await this.saveState();
return {
accessToken: this.state.accessToken,
idToken: this.state.idToken,
refreshToken: this.state.refreshToken,
scopes: scope ? this.state.scopes : []
};
}
private async recoverState() {
this.state = JSON.parse(await this.storage.loadState() || '{}');
this.setReady();
if (!this.config.storeRefreshToken) {
this.state.refreshToken = this.refreshToken;
}
}
private async saveState() {
this.refreshToken = this.state.refreshToken;
const state = { ...this.state };
if (!this.config.storeRefreshToken) {
delete state.refreshToken;
}
await this.storage.saveState(JSON.stringify(state));
}
}
/**
* To store the OAuth client's data between websites due to redirection.
*/
const LOCALSTORAGE_STATE = 'oauth2authcodepkce-state';
const LocalStorage: Storage = {
saveState: (serializedState: string) => localStorage.setItem(LOCALSTORAGE_STATE, serializedState),
loadState: () => localStorage.getItem(LOCALSTORAGE_STATE)
};