-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): validate if access and id tokens are valid cognito tokens #13385
Changes from all commits
8bad71e
6c053e0
f007450
703394f
e741faa
d149005
7970a41
2b300b4
afb3ba2
dffa3cb
27f25d7
008eb27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { isValidCognitoToken } from '@aws-amplify/core/internals/utils'; | ||
|
||
import { createTokenValidator } from '../../src/utils/createTokenValidator'; | ||
|
||
jest.mock('@aws-amplify/core/internals/utils', () => ({ | ||
...jest.requireActual('@aws-amplify/core/internals/utils'), | ||
isValidCognitoToken: jest.fn(), | ||
})); | ||
const mockIsValidCognitoToken = isValidCognitoToken as jest.Mock; | ||
|
||
const userPoolId = 'userPoolId'; | ||
const userPoolClientId = 'clientId'; | ||
const tokenValidatorInput = { | ||
userPoolId, | ||
userPoolClientId, | ||
}; | ||
const accessToken = { | ||
key: 'CognitoIdentityServiceProvider.clientId.usersub.accessToken', | ||
value: | ||
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMTEiLCJpc3MiOiJodHRwc', | ||
}; | ||
const idToken = { | ||
key: 'CognitoIdentityServiceProvider.clientId.usersub.idToken', | ||
value: 'eyJzdWIiOiIxMTEiLCJpc3MiOiJodHRwc.XAiOiJKV1QiLCJhbGciOiJIUzI1NiJ', | ||
}; | ||
|
||
const tokenValidator = createTokenValidator({ | ||
userPoolId, | ||
userPoolClientId, | ||
}); | ||
|
||
describe('Validator', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
it('should return a validator', () => { | ||
expect(createTokenValidator(tokenValidatorInput)).toBeDefined(); | ||
}); | ||
|
||
it('should return true for non-token keys', async () => { | ||
const result = await tokenValidator.getItem?.('mockKey', 'mockValue'); | ||
expect(result).toBe(true); | ||
expect(mockIsValidCognitoToken).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should return true for valid accessToken', async () => { | ||
mockIsValidCognitoToken.mockImplementation(() => Promise.resolve(true)); | ||
|
||
const result = await tokenValidator.getItem?.( | ||
accessToken.key, | ||
accessToken.value, | ||
); | ||
|
||
expect(result).toBe(true); | ||
expect(mockIsValidCognitoToken).toHaveBeenCalledTimes(1); | ||
expect(mockIsValidCognitoToken).toHaveBeenCalledWith({ | ||
userPoolId, | ||
clientId: userPoolClientId, | ||
token: accessToken.value, | ||
tokenType: 'access', | ||
}); | ||
}); | ||
|
||
it('should return true for valid idToken', async () => { | ||
mockIsValidCognitoToken.mockImplementation(() => Promise.resolve(true)); | ||
|
||
const result = await tokenValidator.getItem?.(idToken.key, idToken.value); | ||
expect(result).toBe(true); | ||
expect(mockIsValidCognitoToken).toHaveBeenCalledTimes(1); | ||
expect(mockIsValidCognitoToken).toHaveBeenCalledWith({ | ||
userPoolId, | ||
clientId: userPoolClientId, | ||
token: idToken.value, | ||
tokenType: 'id', | ||
}); | ||
}); | ||
|
||
it('should return false if invalid tokenType is access', async () => { | ||
mockIsValidCognitoToken.mockImplementation(() => Promise.resolve(false)); | ||
|
||
const result = await tokenValidator.getItem?.(idToken.key, idToken.value); | ||
expect(result).toBe(false); | ||
expect(mockIsValidCognitoToken).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { isValidCognitoToken } from '@aws-amplify/core/internals/utils'; | ||
import { KeyValueStorageMethodValidator } from '@aws-amplify/core/internals/adapter-core'; | ||
|
||
interface CreateTokenValidatorInput { | ||
userPoolId?: string; | ||
userPoolClientId?: string; | ||
} | ||
/** | ||
* Creates a validator object for validating methods in a KeyValueStorage. | ||
*/ | ||
export const createTokenValidator = ({ | ||
userPoolId, | ||
userPoolClientId: clientId, | ||
}: CreateTokenValidatorInput): KeyValueStorageMethodValidator => { | ||
return { | ||
// validate access, id tokens | ||
getItem: async (key: string, value: string): Promise<boolean> => { | ||
const tokenType = key.includes('.accessToken') | ||
? 'access' | ||
: key.includes('.idToken') | ||
? 'id' | ||
: null; | ||
if (!tokenType) return true; | ||
|
||
if (!userPoolId || !clientId) return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we can require these two fields from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we make userPoolId and clientId required fields in this function, we would need make sure these are passed by it's consumer It's a bit tricky because if we add assertions in |
||
|
||
return isValidCognitoToken({ | ||
clientId, | ||
userPoolId, | ||
tokenType, | ||
token: value, | ||
}); | ||
}, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getItem
method implies that it will get something but the return type is a boolean. We could either return the actualitem
/token
and returnnull
if it is invalid. The other option is to change the method name to something more deterministic that implies the validation of the token.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point 🤔,
This function returns a validationMap where the key is name of the function in KeyValueStorage that we want to add validation for (
getItem
in this case) and the value is the validation function that returns a bool if the validation was successful or nowWe could maybe improve the function name to clarity that