Skip to content

Commit c5770b6

Browse files
fix: validate token before validating against allow list (#163)
* test: failing test cases * fix: validate token before attempting to validate against allow list
1 parent 020c7e5 commit c5770b6

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/ims.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,11 @@ class Ims {
481481
let validationResponse = await this._validateToken(token)
482482

483483
// Validate token against the allow list
484-
const tokenData = getTokenData(token)
485-
const clientId = tokenData.client_id
486-
if (allowList) {
484+
if (validationResponse.imsValidation.valid && allowList) {
487485
aioLogger.debug('validateTokenAllowList (allowList): (%s)', allowList.join(', '))
486+
const tokenData = getTokenData(token)
487+
const clientId = tokenData.client_id
488488
if (allowList.indexOf(clientId) === -1) {
489-
console.log(`${clientId} not in allow list: ${allowList.join(', ')}`)
490489
validationResponse = {
491490
status: 403,
492491
imsValidation: {

test/ims.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,36 @@ test('Ims.validateTokenAllowList(token, allowList) clientId not in allow list, w
505505
expect(mockExponentialBackoff).toHaveBeenCalledTimes(1)
506506
})
507507

508+
test('Ims.validateTokenAllowList(token, allowList) bad token, with cache', async () => {
509+
const cache = new ValidationCache(1, 2, 3)
510+
const ims = new Ims('stage', cache)
511+
const clientId = 'some-client-id-2'
512+
const token = 'fake'
513+
514+
await expect(ims.validateTokenAllowList(token, [clientId]))
515+
.resolves.toEqual({
516+
valid: false,
517+
reason: 'bad payload'
518+
})
519+
520+
expect(mockExponentialBackoff).toHaveBeenCalledTimes(0)
521+
})
522+
523+
test('Ims.validateTokenAllowList(token, allowList) bad token in jwt format, with cache', async () => {
524+
const cache = new ValidationCache(1, 2, 3)
525+
const ims = new Ims('stage', cache)
526+
const clientId = 'some-client-id-2'
527+
const token = 'fake.fake'
528+
529+
await expect(ims.validateTokenAllowList(token, [clientId]))
530+
.resolves.toEqual({
531+
valid: false,
532+
reason: 'bad payload'
533+
})
534+
535+
expect(mockExponentialBackoff).toHaveBeenCalledTimes(0)
536+
})
537+
508538
test('Ims.getOrganizations(token)', async () => {
509539
const ims = new Ims()
510540

0 commit comments

Comments
 (0)