diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 91ef941..43fde33 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -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', () => { diff --git a/src/index.ts b/src/index.ts index 2bfdd18..1fc3482 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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);