Skip to content
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: support for validating issuer from a list of values #91

Merged
merged 5 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/jwt/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ const validateOptions = ({

isOptionString(maxTokenAge, 'options.maxTokenAge')
isOptionString(subject, 'options.subject')
isOptionString(issuer, 'options.issuer')
isOptionString(maxAuthAge, 'options.maxAuthAge')
isOptionString(jti, 'options.jti')
isOptionString(clockTolerance, 'options.clockTolerance')
isOptionString(typ, 'options.typ')

if (issuer !== undefined && (isNotString(issuer) && isNotArrayOfStrings(issuer))) {
throw new TypeError('options.issuer must be a string or an array of strings')
}

if (audience !== undefined && (isNotString(audience) && isNotArrayOfStrings(audience))) {
throw new TypeError('options.audience must be a string or an array of strings')
}
Expand Down Expand Up @@ -161,7 +164,7 @@ const validateTypes = ({ header, payload }, profile, options) => {
isPayloadString(payload.jti, '"jti" claim', 'jti', profile === LOGOUTTOKEN || profile === ATJWT || !!options.jti)
isPayloadString(payload.acr, '"acr" claim', 'acr')
isPayloadString(payload.nonce, '"nonce" claim', 'nonce', !!options.nonce)
isPayloadString(payload.iss, '"iss" claim', 'iss', !!options.issuer)
isStringOrArrayOfStrings(payload.iss, 'iss', !!options.issuer)
isPayloadString(payload.sub, '"sub" claim', 'sub', profile === IDTOKEN || profile === ATJWT || !!options.subject)
isStringOrArrayOfStrings(payload.aud, 'aud', !!options.audience)
isPayloadString(payload.azp, '"azp" claim', 'azp', profile === IDTOKEN && Array.isArray(payload.aud) && payload.aud.length > 1)
Expand Down Expand Up @@ -235,7 +238,7 @@ module.exports = (token, key, options = {}) => {
const unix = epoch(now)
validateTypes(decoded, profile, options)

if (issuer && decoded.payload.iss !== issuer) {
if (issuer && (typeof decoded.payload.iss !== 'string' || !((typeof issuer === 'string' ? [issuer] : issuer).includes(decoded.payload.iss)))) {
sboys3 marked this conversation as resolved.
Show resolved Hide resolved
throw new JWTClaimInvalid('unexpected "iss" claim value', 'iss', 'check_failed')
}

Expand Down
16 changes: 13 additions & 3 deletions test/jwt/verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ test('options must be an object', t => {
})

test('options.clockTolerance must be a string', string, 'clockTolerance')
test('options.issuer must be a string', string, 'issuer')
test('options.jti must be a string', string, 'jti')
test('options.profile must be a string', string, 'profile')
test('options.maxAuthAge must be a string', string, 'maxAuthAge')
Expand All @@ -55,6 +54,17 @@ test('options.ignoreExp must be boolean', boolean, 'ignoreExp')
test('options.ignoreNbf must be boolean', boolean, 'ignoreNbf')
test('options.ignoreIat must be boolean', boolean, 'ignoreIat')

test('options.issuer must be string or array of strings', t => {
;['', false, [], Buffer, Buffer.from('foo'), 0, Infinity].forEach((val) => {
t.throws(() => {
JWT.verify(token, key, { issuer: val })
}, { instanceOf: TypeError, message: 'options.issuer must be a string or an array of strings' })
t.throws(() => {
JWT.verify(token, key, { issuer: [val] })
}, { instanceOf: TypeError, message: 'options.issuer must be a string or an array of strings' })
})
})

test('options.audience must be string or array of strings', t => {
;['', false, [], Buffer, Buffer.from('foo'), 0, Infinity].forEach((val) => {
t.throws(() => {
Expand Down Expand Up @@ -116,7 +126,7 @@ test('options.ignoreIat & options.maxTokenAge may not be used together', t => {
})
})

;['jti', 'acr', 'iss', 'nonce', 'sub', 'azp'].forEach((claim) => {
;['jti', 'acr', 'nonce', 'sub', 'azp'].forEach((claim) => {
test(`"${claim} must be a string when provided"`, t => {
;['', 0, 1, true, null, [], {}].forEach((val) => {
const err = t.throws(() => {
Expand All @@ -130,7 +140,7 @@ test('options.ignoreIat & options.maxTokenAge may not be used together', t => {
})
})

;['aud', 'amr'].forEach((claim) => {
;['aud', 'amr', 'iss'].forEach((claim) => {
test(`"${claim} must be a string when provided"`, t => {
;['', 0, 1, true, null, [], {}].forEach((val) => {
let err
Expand Down