Skip to content

Commit 70d02d4

Browse files
Throw an explicit exception if encountering id token parse error
1 parent f4b7e9f commit 70d02d4

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/AuthenticationResponse.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,20 @@ class AuthenticationResponse {
360360
* @param response.params {object}
361361
* @param response.params.id_token {string} IDToken encoded as a JWT
362362
*
363-
* @returns {Promise<AuthenticationResponse>} Chainable
363+
* @returns {AuthenticationResponse} Chainable
364364
*/
365365
static decodeIDToken (response) {
366366
let jwt = response.params.id_token
367367

368368
if (jwt) {
369-
response.decoded = IDToken.decode(jwt)
369+
try {
370+
response.decoded = IDToken.decode(jwt)
371+
} catch (decodeError) {
372+
const error = new HttpError(400, 'Error decoding ID Token')
373+
error.cause = decodeError
374+
error.info = { id_token: jwt }
375+
throw error
376+
}
370377
}
371378

372379
return response

test/AuthenticationResponseSpec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,13 @@ describe('AuthenticationResponse', () => {
494494
.to.be.undefined()
495495
})
496496

497+
it('should throw an error on invalid id_token', () => {
498+
response.params.id_token = 'inva1id'
499+
expect(() => {
500+
AuthenticationResponse.decodeIDToken(response)
501+
}).to.throw('Error decoding ID Token')
502+
})
503+
497504
it('should return its argument', () => {
498505
AuthenticationResponse.decodeIDToken(response).should.equal(response)
499506
})

0 commit comments

Comments
 (0)