-
Notifications
You must be signed in to change notification settings - Fork 54
/
index.ts
794 lines (718 loc) · 30.5 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
import { CognitoJwtVerifier } from 'aws-jwt-verify';
import type { CloudFrontRequest, CloudFrontRequestEvent, CloudFrontResultResponse } from 'aws-lambda';
import axios from 'axios';
import pino from 'pino';
import { parse, stringify } from 'querystring';
import { CookieAttributes, CookieSettingsOverrides, CookieType, Cookies, SAME_SITE_VALUES, SameSite, getCookieDomain } from './util/cookie';
import { CSRFTokens, NONCE_COOKIE_NAME_SUFFIX, NONCE_HMAC_COOKIE_NAME_SUFFIX, PKCE_COOKIE_NAME_SUFFIX, generateCSRFTokens, signNonce, urlSafe } from './util/csrf';
export interface AuthenticatorParams {
region: string;
userPoolId: string;
userPoolAppId: string;
userPoolAppSecret?: string;
userPoolDomain: string;
cookieExpirationDays?: number;
disableCookieDomain?: boolean;
httpOnly?: boolean;
sameSite?: SameSite;
logLevel?: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'silent';
cookiePath?: string;
cookieDomain?: string;
cookieSettingsOverrides?: CookieSettingsOverrides;
logoutConfiguration?: LogoutConfiguration;
parseAuthPath?: string;
csrfProtection?: {
nonceSigningSecret: string;
},
}
interface LogoutConfiguration {
logoutUri: string;
logoutRedirectUri: string;
}
interface Tokens {
accessToken?: string;
idToken?: string;
refreshToken?: string;
}
export class Authenticator {
_region: string;
_userPoolId: string;
_userPoolAppId: string;
_userPoolAppSecret: string | undefined;
_userPoolDomain: string;
_cookieExpirationDays: number;
_disableCookieDomain: boolean;
_httpOnly: boolean;
_sameSite?: SameSite;
_cookieBase: string;
_cookiePath?: string;
_cookieDomain?: string;
_csrfProtection?: {
nonceSigningSecret: string;
};
_logoutConfiguration?: LogoutConfiguration;
_parseAuthPath?: string;
_cookieSettingsOverrides?: CookieSettingsOverrides;
_logger;
_jwtVerifier;
constructor(params: AuthenticatorParams) {
this._verifyParams(params);
this._region = params.region;
this._userPoolId = params.userPoolId;
this._userPoolAppId = params.userPoolAppId;
this._userPoolAppSecret = params.userPoolAppSecret;
this._userPoolDomain = params.userPoolDomain;
this._cookieExpirationDays = params.cookieExpirationDays || 365;
this._disableCookieDomain = ('disableCookieDomain' in params && params.disableCookieDomain === true);
this._cookieDomain = params.cookieDomain;
this._httpOnly = ('httpOnly' in params && params.httpOnly === true);
this._sameSite = params.sameSite;
this._cookieBase = `CognitoIdentityServiceProvider.${params.userPoolAppId}`;
this._cookiePath = params.cookiePath;
this._cookieSettingsOverrides = params.cookieSettingsOverrides || {};
this._logger = pino({
level: params.logLevel || 'silent', // Default to silent
base: null, //Remove pid, hostname and name logging as not usefull for Lambda
});
this._jwtVerifier = CognitoJwtVerifier.create({
userPoolId: params.userPoolId,
clientId: params.userPoolAppId,
tokenUse: 'id',
});
this._csrfProtection = params.csrfProtection;
this._logoutConfiguration = params.logoutConfiguration;
this._parseAuthPath = (params.parseAuthPath || '').replace(/^\//, '');
}
/**
* Verify that constructor parameters are corrects.
* @param {object} params constructor params
* @return {void} throw an exception if params are incorects.
*/
_verifyParams(params: AuthenticatorParams) {
if (typeof params !== 'object') {
throw new Error('Expected params to be an object');
}
[ 'region', 'userPoolId', 'userPoolAppId', 'userPoolDomain' ].forEach(param => {
if (typeof params[param as keyof AuthenticatorParams] !== 'string') {
throw new Error(`Expected params.${param} to be a string`);
}
});
if (params.cookieExpirationDays && typeof params.cookieExpirationDays !== 'number') {
throw new Error('Expected params.cookieExpirationDays to be a number');
}
if ('disableCookieDomain' in params && typeof params.disableCookieDomain !== 'boolean') {
throw new Error('Expected params.disableCookieDomain to be boolean');
}
if ('cookieDomain' in params && typeof params.cookieDomain !== 'string') {
throw new Error('Expected params.cookieDomain to be a string');
}
if ('httpOnly' in params && typeof params.httpOnly !== 'boolean') {
throw new Error('Expected params.httpOnly to be a boolean');
}
if (params.sameSite !== undefined && !SAME_SITE_VALUES.includes(params.sameSite)) {
throw new Error('Expected params.sameSite to be a Strict || Lax || None');
}
if ('cookiePath' in params && typeof params.cookiePath !== 'string') {
throw new Error('Expected params.cookiePath to be a string');
}
if (params.logoutConfiguration && !/\/\w+/.test(params.logoutConfiguration.logoutUri)) {
throw new Error('Expected params.logoutConfiguration.logoutUri to be a valid non-empty string starting with "/"');
}
}
/**
* Exchange authorization code for tokens.
* @param {String} redirectURI Redirection URI.
* @param {String} code Authorization code.
* @return {Promise} Authenticated user tokens.
*/
_fetchTokensFromCode(redirectURI: string, code: string): Promise<Tokens> {
const authorization = this._getAuthorization();
const request = {
url: `https://${this._userPoolDomain}/oauth2/token`,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
...(authorization && {'Authorization': `Basic ${authorization}`}),
},
data: stringify({
client_id: this._userPoolAppId,
code: code,
grant_type: 'authorization_code',
redirect_uri: redirectURI,
}),
} as const;
this._logger.debug({ msg: 'Fetching tokens from grant code...', request, code });
return axios.request(request)
.then(resp => {
this._logger.debug({ msg: 'Fetched tokens', tokens: resp.data });
return {
idToken: resp.data.id_token,
accessToken: resp.data.access_token,
refreshToken: resp.data.refresh_token,
};
})
.catch(err => {
this._logger.error({ msg: 'Unable to fetch tokens from grant code', request, code });
throw err;
});
}
/**
* Fetch accessTokens from refreshToken.
* @param {String} redirectURI Redirection URI.
* @param {String} refreshToken Refresh token.
* @return {Promise<Tokens>} Refreshed user tokens.
*/
_fetchTokensFromRefreshToken(redirectURI: string, refreshToken: string): Promise<Tokens> {
const authorization = this._getAuthorization();
const request = {
url: `https://${this._userPoolDomain}/oauth2/token`,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
...(authorization && {'Authorization': `Basic ${authorization}`}),
},
data: stringify({
client_id: this._userPoolAppId,
refresh_token: refreshToken,
grant_type: 'refresh_token',
redirect_uri: redirectURI,
}),
} as const;
this._logger.debug({ msg: 'Fetching tokens from refreshToken...', request, refreshToken });
return axios.request(request)
.then(resp => {
this._logger.debug({ msg: 'Fetched tokens', tokens: resp.data });
return {
idToken: resp.data.id_token,
accessToken: resp.data.access_token,
};
})
.catch(err => {
this._logger.error({ msg: 'Unable to fetch tokens from refreshToken', request, refreshToken });
throw err;
});
}
_getAuthorization(): string | undefined {
return this._userPoolAppSecret && Buffer.from(`${this._userPoolAppId}:${this._userPoolAppSecret}`).toString('base64');
}
_validateCSRFCookies(request: CloudFrontRequest) {
if (!this._csrfProtection) {
throw new Error('_validateCSRFCookies should not be called if CSRF protection is disabled.');
}
const requestParams = parse(request.querystring);
const requestCookies = request.headers.cookie?.flatMap(h => Cookies.parse(h.value)) || [];
this._logger.debug({ msg: 'Validating CSRF Cookies', requestCookies});
const parsedState = JSON.parse(
Buffer.from(urlSafe.parse(requestParams.state as string), 'base64').toString()
);
const {nonce: originalNonce, nonceHmac, pkce} = this._getCSRFTokensFromCookie(request.headers.cookie);
if (
!parsedState.nonce ||
!originalNonce ||
parsedState.nonce !== originalNonce
) {
if (!originalNonce) {
throw new Error('Your browser didn\'t send the nonce cookie along, but it is required for security (prevent CSRF).');
}
throw new Error('Nonce mismatch. This can happen if you start multiple authentication attempts in parallel (e.g. in separate tabs)');
}
if (!pkce) {
throw new Error('Your browser didn\'t send the pkce cookie along, but it is required for security (prevent CSRF).');
}
const calculatedHmac = signNonce(parsedState.nonce, this._csrfProtection.nonceSigningSecret);
if (calculatedHmac !== nonceHmac) {
throw new Error(`Nonce signature mismatch! Expected ${calculatedHmac} but got ${nonceHmac}`);
}
}
_getOverridenCookieAttributes(cookieAttributes: CookieAttributes = {}, cookieType: CookieType): CookieAttributes {
const res = {...cookieAttributes};
const overrides = this._cookieSettingsOverrides?.[cookieType];
if (overrides) {
if (overrides.httpOnly !== undefined) {
res.httpOnly = overrides.httpOnly;
}
if (overrides.sameSite !== undefined) {
res.sameSite = overrides.sameSite;
}
if (overrides.path !== undefined) {
res.path = overrides.path;
}
if (overrides.expirationDays !== undefined) {
res.expires = new Date(Date.now() + overrides.expirationDays * 864e+5);
}
}
this._logger.debug({
msg: 'Cookie settings overriden',
cookieAttributes,
cookieType,
cookieSettingsOverrides: this._cookieSettingsOverrides,
});
return res;
}
/**
* Create a Lambda@Edge redirection response to set the tokens on the user's browser cookies.
* @param {Object} tokens Cognito User Pool tokens.
* @param {String} domain Website domain.
* @param {String} location Path to redirection.
* @return Lambda@Edge response.
*/
async _getRedirectResponse(tokens: Tokens, domain: string, location: string): Promise<CloudFrontResultResponse> {
const decoded = await this._jwtVerifier.verify(tokens.idToken as string);
const username = decoded['cognito:username'] as string;
const usernameBase = `${this._cookieBase}.${username}`;
const cookieDomain = getCookieDomain(domain, this._disableCookieDomain, this._cookieDomain);
const cookieAttributes: CookieAttributes = {
domain: cookieDomain,
expires: new Date(Date.now() + this._cookieExpirationDays * 864e+5),
secure: true,
httpOnly: this._httpOnly,
sameSite: this._sameSite,
path: this._cookiePath,
};
const cookies = [
Cookies.serialize(`${usernameBase}.accessToken`, tokens.accessToken as string, this._getOverridenCookieAttributes(cookieAttributes, 'accessToken')),
Cookies.serialize(`${usernameBase}.idToken`, tokens.idToken as string, this._getOverridenCookieAttributes(cookieAttributes, 'idToken')),
...(tokens.refreshToken ? [Cookies.serialize(`${usernameBase}.refreshToken`, tokens.refreshToken, this._getOverridenCookieAttributes(cookieAttributes, 'refreshToken'))] : []),
Cookies.serialize(`${usernameBase}.tokenScopesString`, 'phone email profile openid aws.cognito.signin.user.admin', cookieAttributes),
Cookies.serialize(`${this._cookieBase}.LastAuthUser`, username, cookieAttributes),
];
// Clear CSRF Token Cookies
if (this._csrfProtection) {
// Domain attribute is always not set here as CSRF cookies are used
// exclusively by the CF distribution
const csrfCookieAttributes = {...cookieAttributes, domain: undefined, expires: new Date()};
cookies.push(
Cookies.serialize(`${this._cookieBase}.${PKCE_COOKIE_NAME_SUFFIX}`, '', csrfCookieAttributes),
Cookies.serialize(`${this._cookieBase}.${NONCE_COOKIE_NAME_SUFFIX}`, '', csrfCookieAttributes),
Cookies.serialize(`${this._cookieBase}.${NONCE_HMAC_COOKIE_NAME_SUFFIX}`, '', csrfCookieAttributes),
);
}
const response: CloudFrontResultResponse = {
status: '302' ,
headers: {
'location': [{
key: 'Location',
value: location,
}],
'cache-control': [{
key: 'Cache-Control',
value: 'no-cache, no-store, max-age=0, must-revalidate',
}],
'pragma': [{
key: 'Pragma',
value: 'no-cache',
}],
'set-cookie': cookies.map(c => ({ key: 'Set-Cookie', value: c })),
},
};
this._logger.debug({ msg: 'Generated set-cookie response', response });
return response;
}
/**
* Extract value of the authentication token from the request cookies.
* @param {Array} cookieHeaders 'Cookie' request headers.
* @return {Tokens} Extracted id token or access token. Null if not found.
*/
_getTokensFromCookie(cookieHeaders: Array<{ key?: string | undefined, value: string }> | undefined): Tokens {
if (!cookieHeaders) {
this._logger.debug("Cookies weren't present in the request");
throw new Error("Cookies weren't present in the request");
}
this._logger.debug({ msg: 'Extracting authentication token from request cookie', cookieHeaders });
const cookies = cookieHeaders.flatMap(h => Cookies.parse(h.value));
const tokenCookieNamePrefix = `${this._cookieBase}.`;
const idTokenCookieNamePostfix = '.idToken';
const refreshTokenCookieNamePostfix = '.refreshToken';
const tokens: Tokens = {};
for (const {name, value} of cookies){
if (name.startsWith(tokenCookieNamePrefix) && name.endsWith(idTokenCookieNamePostfix)) {
tokens.idToken = value;
}
if (name.startsWith(tokenCookieNamePrefix) && name.endsWith(refreshTokenCookieNamePostfix)) {
tokens.refreshToken = value;
}
}
if (!tokens.idToken && !tokens.refreshToken) {
this._logger.debug('Neither idToken, nor refreshToken was present in request cookies');
throw new Error('Neither idToken, nor refreshToken was present in request cookies');
}
this._logger.debug({ msg: 'Found tokens in cookie', tokens });
return tokens;
}
/**
* Extract values of the CSRF tokens from the request cookies.
* @param {Array} cookieHeaders 'Cookie' request headers.
* @return {CSRFTokens} Extracted CSRF Tokens from cookie.
*/
_getCSRFTokensFromCookie(cookieHeaders: Array<{ key?: string | undefined, value: string }> | undefined): CSRFTokens {
if (!cookieHeaders) {
this._logger.debug("Cookies weren't present in the request");
throw new Error("Cookies weren't present in the request");
}
this._logger.debug({ msg: 'Extracting CSRF tokens from request cookie', cookieHeaders });
const cookies = cookieHeaders.flatMap(h => Cookies.parse(h.value));
const csrfTokens: CSRFTokens = cookies.reduce((tokens, {name, value}) => {
if (name.startsWith(this._cookieBase)) {
[
NONCE_COOKIE_NAME_SUFFIX,
NONCE_HMAC_COOKIE_NAME_SUFFIX,
PKCE_COOKIE_NAME_SUFFIX,
].forEach(key => {
if (name.endsWith(`.${key}`)) {
tokens[key] = value;
}
});
}
return tokens;
}, {} as CSRFTokens);
this._logger.debug({ msg: 'Found CSRF tokens in cookie', csrfTokens });
return csrfTokens;
}
/**
* Extracts the redirect uri from the state param. When CSRF protection is
* enabled, redirect uri is encoded inside state along with other data. So, it
* needs to be base64 decoded. When CSRF is not enabled, state can be used
* directly.
* @param {string} state
* @returns {string}
*/
_getRedirectUriFromState(state: string): string {
if (this._csrfProtection) {
const parsedState = JSON.parse(
Buffer.from(urlSafe.parse(state), 'base64').toString()
);
this._logger.debug({ msg: 'Parsed state param to extract redirect uri', parsedState });
return parsedState.redirect_uri;
}
return state;
}
async _revokeTokens(tokens: Tokens) {
const authorization = this._getAuthorization();
const revokeRequest = {
url: `https://${this._userPoolDomain}/oauth2/revoke`,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
...(authorization && {'Authorization': `Basic ${authorization}`}),
},
data: stringify({
client_id: this._userPoolAppId,
token: tokens.refreshToken,
}),
} as const;
this._logger.debug({ msg: 'Revoking refreshToken...', request: revokeRequest, refreshToken: tokens.refreshToken });
return axios.request(revokeRequest)
.then(() => {
this._logger.debug({ msg: 'Revoked refreshToken', refreshToken: tokens.refreshToken });
})
.catch(err => {
this._logger.error({ msg: 'Unable to revoke refreshToken', request: revokeRequest, err: JSON.stringify(err) });
throw err;
});
}
async _clearCookies(event: CloudFrontRequestEvent, tokens: Tokens = {}): Promise<CloudFrontResultResponse> {
this._logger.info({ msg: 'Clearing cookies...', event, tokens });
const { request } = event.Records[0].cf;
const cfDomain = request.headers.host[0].value;
const requestParams = parse(request.querystring);
const redirectURI = this._logoutConfiguration?.logoutRedirectUri ||
requestParams.redirect_uri as string ||
`https://${cfDomain}`;
const cookieDomain = getCookieDomain(cfDomain, this._disableCookieDomain, this._cookieDomain);
const cookieAttributes: CookieAttributes = {
domain: cookieDomain,
expires: new Date(),
secure: true,
httpOnly: this._httpOnly,
sameSite: this._sameSite,
path: this._cookiePath,
};
let responseCookies: string[] = [];
try {
const decoded = await this._jwtVerifier.verify(tokens.idToken as string);
const username = decoded['cognito:username'] as string;
this._logger.info({ msg: 'Token verified. Clearing cookies...', idToken: tokens.idToken, username });
const usernameBase = `${this._cookieBase}.${username}`;
responseCookies = [
Cookies.serialize(`${usernameBase}.accessToken`, '', cookieAttributes),
Cookies.serialize(`${usernameBase}.idToken`, '', cookieAttributes),
...(tokens.refreshToken ? [Cookies.serialize(`${usernameBase}.refreshToken`, '', cookieAttributes)] : []),
Cookies.serialize(`${usernameBase}.tokenScopesString`, '', cookieAttributes),
Cookies.serialize(`${this._cookieBase}.LastAuthUser`, '', cookieAttributes),
];
} catch (err) {
this._logger.info({
msg: 'Unable to verify token. Inferring data from request cookies and clearing them...',
idToken: tokens.idToken,
});
const requestCookies = request.headers.cookie?.flatMap(h => Cookies.parse(h.value)) || [];
for (const { name } of requestCookies) {
if (name.startsWith(this._cookieBase)) {
responseCookies.push(
Cookies.serialize(name, '', cookieAttributes),
);
}
}
}
const response: CloudFrontResultResponse = {
status: '302' ,
headers: {
'location': [{
key: 'Location',
value: redirectURI,
}],
'cache-control': [{
key: 'Cache-Control',
value: 'no-cache, no-store, max-age=0, must-revalidate',
}],
'pragma': [{
key: 'Pragma',
value: 'no-cache',
}],
'set-cookie': responseCookies.map(c => ({ key: 'Set-Cookie', value: c })),
},
};
this._logger.debug({ msg: 'Generated set-cookie response', response });
return response;
}
/**
* Get redirect to cognito userpool response
* @param {CloudFrontRequest} request The original request
* @param {string} redirectURI Redirection URI.
* @return {CloudFrontResultResponse} Redirect response.
*/
_getRedirectToCognitoUserPoolResponse(request: CloudFrontRequest, redirectURI: string): CloudFrontResultResponse {
let redirectPath = request.uri;
if (request.querystring && request.querystring !== '') {
redirectPath += encodeURIComponent('?' + request.querystring);
}
let csrfTokens: CSRFTokens = {};
let state: string | undefined = redirectPath;
if (this._csrfProtection) {
csrfTokens = generateCSRFTokens(redirectURI, this._csrfProtection.nonceSigningSecret);
state = csrfTokens.state;
}
const userPoolUrl = `https://${this._userPoolDomain}/authorize?redirect_uri=${redirectURI}&response_type=code&client_id=${this._userPoolAppId}&state=${state}`;
this._logger.debug(`Redirecting user to Cognito User Pool URL ${userPoolUrl}`);
let cookies: string[] | undefined;
if (this._csrfProtection) {
const cookieAttributes: CookieAttributes = {
expires: new Date(Date.now() + 10 * 60 * 1000),
secure: true,
httpOnly: this._httpOnly,
sameSite: this._sameSite,
path: this._cookiePath,
};
cookies = [
Cookies.serialize(`${this._cookieBase}.${PKCE_COOKIE_NAME_SUFFIX}`, csrfTokens.pkce || '', cookieAttributes),
Cookies.serialize(`${this._cookieBase}.${NONCE_COOKIE_NAME_SUFFIX}`, csrfTokens.nonce || '', cookieAttributes),
Cookies.serialize(`${this._cookieBase}.${NONCE_HMAC_COOKIE_NAME_SUFFIX}`, csrfTokens.nonceHmac || '', cookieAttributes),
];
}
const response: CloudFrontResultResponse = {
status: '302',
headers: {
'location': [{
key: 'Location',
value: userPoolUrl,
}],
'cache-control': [{
key: 'Cache-Control',
value: 'no-cache, no-store, max-age=0, must-revalidate',
}],
'pragma': [{
key: 'Pragma',
value: 'no-cache',
}],
...(cookies
? { 'set-cookie': cookies && cookies.map(c => ({ key: 'Set-Cookie', value: c })) }
: {}),
},
};
return response;
}
/**
* Handle Lambda@Edge event:
* * if authentication cookie is present and valid: forward the request
* * if authentication cookie is invalid, but refresh token is present: set cookies with refreshed tokens
* * if ?code=<grant code> is present: set cookies with new tokens
* * else redirect to the Cognito UserPool to authenticate the user
* @param {Object} event Lambda@Edge event.
* @return {Promise} CloudFront response.
*/
async handle(event: CloudFrontRequestEvent): Promise<CloudFrontResultResponse | CloudFrontRequest> {
this._logger.debug({ msg: 'Handling Lambda@Edge event', event });
const { request } = event.Records[0].cf;
const cfDomain = request.headers.host[0].value;
const redirectURI = this._parseAuthPath ? `https://${cfDomain}/${this._parseAuthPath}` : `https://${cfDomain}`;
try {
const tokens = this._getTokensFromCookie(request.headers.cookie);
if (this._logoutConfiguration && request.uri.startsWith(this._logoutConfiguration.logoutUri)) {
this._logger.info({ msg: 'Revoking tokens', tokens });
await this._revokeTokens(tokens);
this._logger.info({ msg: 'Revoked tokens. Clearing cookies', tokens });
return this._clearCookies(event, tokens);
}
try {
this._logger.debug({ msg: 'Verifying token...', tokens });
const user = await this._jwtVerifier.verify(tokens.idToken as string);
this._logger.info({ msg: 'Forwarding request', path: request.uri, user });
return request;
} catch (err) {
this._logger.info({ msg: 'Token verification failed', tokens, refreshToken: tokens.refreshToken });
if (tokens.refreshToken) {
this._logger.debug({ msg: 'Verifying idToken failed, verifying refresh token instead...', tokens, err });
return await this._fetchTokensFromRefreshToken(redirectURI, tokens.refreshToken)
.then(tokens => this._getRedirectResponse(tokens, cfDomain, request.uri));
} else {
throw err;
}
}
} catch (err) {
if (this._logoutConfiguration && request.uri.startsWith(this._logoutConfiguration.logoutUri)) {
this._logger.info({ msg: 'Clearing cookies', path: cfDomain });
return this._clearCookies(event);
}
this._logger.debug("User isn't authenticated: %s", err);
const requestParams = parse(request.querystring);
if (requestParams.code) {
return this._fetchTokensFromCode(redirectURI, requestParams.code as string)
.then(tokens => this._getRedirectResponse(tokens, cfDomain, this._getRedirectUriFromState(requestParams.state as string)));
} else {
return this._getRedirectToCognitoUserPoolResponse(request, redirectURI);
}
}
}
/**
*
* 1. If the token cookies are present in the request, send users to the redirect_uri
* 2. If cookies are not present, initiate the authentication flow
*
* @param event Event that triggers this Lambda function
* @returns Lambda response
*/
async handleSignIn(event: CloudFrontRequestEvent): Promise<CloudFrontResultResponse> {
this._logger.debug({ msg: 'Handling Lambda@Edge event', event });
const { request } = event.Records[0].cf;
const requestParams = parse(request.querystring);
const cfDomain = request.headers.host[0].value;
const redirectURI = requestParams.redirect_uri as string || `https://${cfDomain}`;
try {
const tokens = this._getTokensFromCookie(request.headers.cookie);
this._logger.debug({ msg: 'Verifying token...', tokens });
const user = await this._jwtVerifier.verify(tokens.idToken as string);
this._logger.info({ msg: 'Redirecting user to', path: redirectURI, user });
return {
status: '302',
headers: {
'location': [{
key: 'Location',
value: redirectURI,
}],
},
};
} catch (err) {
this._logger.debug("User isn't authenticated: %s", err);
return this._getRedirectToCognitoUserPoolResponse(
request, this._parseAuthPath ? `https://${cfDomain}/${this._parseAuthPath}` : redirectURI,
);
}
}
/**
*
* Handler that performs OAuth token exchange -- exchanges the authorization
* code obtained from the query parameter from server for tokens -- and sets
* tokens as cookies. This is done after performing CSRF checks, by verifying
* that the information encoded in the state query parameter is related to the
* one stored in the cookies.
*
* @param event Event that triggers this Lambda function
* @returns Lambda response
*/
async handleParseAuth(event: CloudFrontRequestEvent): Promise<CloudFrontResultResponse> {
this._logger.debug({ msg: 'Handling Lambda@Edge event', event });
const { request } = event.Records[0].cf;
const cfDomain = request.headers.host[0].value;
const requestParams = parse(request.querystring);
try {
if (!this._parseAuthPath) {
throw new Error('parseAuthPath is not set');
}
const redirectURI = `https://${cfDomain}/${this._parseAuthPath}`;
if (requestParams.code) {
if (this._csrfProtection) {
this._validateCSRFCookies(request);
}
const tokens = await this._fetchTokensFromCode(redirectURI, requestParams.code as string);
const location = this._getRedirectUriFromState(requestParams.state as string);
return this._getRedirectResponse(tokens, cfDomain, location);
} else {
this._logger.debug({msg: 'Code param not found', requestParams});
throw new Error('OAuth code parameter not found');
}
} catch (err) {
this._logger.debug({msg: 'Unable to exchange code for tokens', err});
return {
status: '400',
body: `${err}`,
};
}
}
/**
*
* Uses the refreshToken present in the cookies to get a new set of tokens
* from the authorization server. After fetching the tokens, they are sent
* back to the client as cookies.
*
* @param event Event that triggers this Lambda function
* @returns Lambda response
*/
async handleRefreshToken(event: CloudFrontRequestEvent): Promise<CloudFrontResultResponse> {
this._logger.debug({ msg: 'Handling Lambda@Edge event', event });
const { request } = event.Records[0].cf;
const cfDomain = request.headers.host[0].value;
const requestParams = parse(request.querystring);
const redirectURI = requestParams.redirect_uri as string || `https://${cfDomain}`;
try {
let tokens = this._getTokensFromCookie(request.headers.cookie);
this._logger.debug({ msg: 'Verifying token...', tokens });
const user = await this._jwtVerifier.verify(tokens.idToken as string);
this._logger.debug({ msg: 'Refreshing tokens...', tokens, user });
tokens = await this._fetchTokensFromRefreshToken(redirectURI, tokens.refreshToken as string);
this._logger.debug({ msg: 'Refreshed tokens...', tokens, user });
return this._getRedirectResponse(tokens, cfDomain, redirectURI);
} catch (err) {
this._logger.debug("User isn't authenticated: %s", err);
return this._getRedirectToCognitoUserPoolResponse(
request, this._parseAuthPath ? `https://${cfDomain}/${this._parseAuthPath}` : redirectURI,
);
}
}
/**
*
* Revokes the refreshToken (which also invalidates the accessToken obtained
* using that refreshToken) and clears the cookies. Even if the revoke
* operation fails, clear cookies based on the cookie names present in the
* request headers.
*
* @param event Event that triggers this Lambda function
* @returns Lambda response
*/
async handleSignOut(event: CloudFrontRequestEvent): Promise<CloudFrontResultResponse> {
this._logger.debug({ msg: 'Handling Lambda@Edge event', event });
const { request } = event.Records[0].cf;
const requestParams = parse(request.querystring);
const cfDomain = request.headers.host[0].value;
const redirectURI = requestParams.redirect_uri as string || `https://${cfDomain}`;
try {
const tokens = this._getTokensFromCookie(request.headers.cookie);
this._logger.info({ msg: 'Revoking tokens', tokens });
await this._revokeTokens(tokens);
this._logger.info({ msg: 'Revoked tokens. Clearing cookies...', tokens });
return this._clearCookies(event, tokens);
} catch (err) {
this._logger.info({ msg: 'Unable to revoke tokens. Clearing cookies...', path: redirectURI });
return this._clearCookies(event);
}
}
}