Skip to content

Commit

Permalink
Update regex to account for idToken being last (#33)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Bakkum <[email protected]>
  • Loading branch information
timbakkum and Tim Bakkum authored Apr 12, 2022
1 parent ee028bd commit 0eca9ac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ describe('private functions', () => {
value: `CognitoIdentityServiceProvider.5uka3k8840tap1g1i1617jh8pi.${appClientName}.idToken=wrong; CognitoIdentityServiceProvider.123456789qwertyuiop987abcd.${appClientName}.idToken=${tokenData.id_token}; CognitoIdentityServiceProvider.123456789qwertyuiop987abcd.${appClientName}.idToken=${tokenData.id_token}; CognitoIdentityServiceProvider.5ukasw8840tap1g1i1617jh8pi.${appClientName}.idToken=wrong;`,
}]),
).toBe(tokenData.id_token);

expect(
authenticator._getIdTokenFromCookie([{
key: 'Cookie',
value: `CognitoIdentityServiceProvider.5uka3k8840tap1g1i1617jh8pi.${appClientName}.accessToken=someValue; CognitoIdentityServiceProvider.123456789qwertyuiop987abcd.${appClientName}.idToken=${tokenData.id_token}`,
}]),
).toBe(tokenData.id_token);
});

test('should getIdTokenFromCookie throw on cookies', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class Authenticator {
_getIdTokenFromCookie(cookies) {
this._logger.debug({ msg: 'Extracting authentication token from request cookie', cookies });
// eslint-disable-next-line no-useless-escape
const regex = new RegExp(`${this._userPoolAppId}\..+?\.idToken=(.*?);`);
const regex = new RegExp(`${this._userPoolAppId}\..+?\.idToken=(.*?)(?:;|$)`);
if (cookies) {
for (let i = 0; i < cookies.length; i++) {
const matches = cookies[i].value.match(regex);
Expand Down

0 comments on commit 0eca9ac

Please sign in to comment.