|
| 1 | +import { DefaultIDTokenValidator, IDTokenValidator, JWT } from 'src/jwt'; |
| 2 | +import { JWTError } from 'src/errors'; |
| 3 | +import { b64u, buf } from 'src/crypto'; |
| 4 | + |
| 5 | +const writeJWT = (header: Record<string, unknown>, claims: Record<string, unknown>) => { |
| 6 | + const head = b64u(buf(JSON.stringify(header))); |
| 7 | + const body = b64u(buf(JSON.stringify(claims))); |
| 8 | + return `${head}.${body}.fakesignature`; |
| 9 | +}; |
| 10 | + |
| 11 | +// wrapper around `DefaultIDTokenValidator.validate` with default param values |
| 12 | +const defaultParams = { |
| 13 | + issuer: new URL('https://fake.okta.com'), |
| 14 | + clientId: 'someclientid', |
| 15 | + context: { supportedAlgs: ['RS256'] } |
| 16 | +}; |
| 17 | +const validate = ( |
| 18 | + jwt: JWT, |
| 19 | + params: { issuer?: URL, clientId?: string, context?: Record<string, unknown> } = {} |
| 20 | +): void => { |
| 21 | + const { issuer, clientId, context } = {...defaultParams, ...params}; |
| 22 | + return DefaultIDTokenValidator.validate(jwt, issuer, clientId, context); |
| 23 | +}; |
| 24 | + |
| 25 | +describe('DefaultIDTokenValidator', () => { |
| 26 | + const context: any = {}; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + const header = { |
| 30 | + kid: 'somerandomid', |
| 31 | + alg: 'RS256' |
| 32 | + }; |
| 33 | + |
| 34 | + const now = Date.now() / 1000; |
| 35 | + |
| 36 | + const payload = { |
| 37 | + sub: 'someclientid', |
| 38 | + aud: 'someclientid', |
| 39 | + ver: 1, |
| 40 | + iss: 'https://fake.okta.com', |
| 41 | + iat: now, |
| 42 | + exp: now + 300, |
| 43 | + nonce: 'nonceuponatime' |
| 44 | + }; |
| 45 | + |
| 46 | + context.header = header; |
| 47 | + context.payload = payload; |
| 48 | + }); |
| 49 | + |
| 50 | + it('validates a valid ID Token', () => { |
| 51 | + const { header, payload } = context; |
| 52 | + const jwt = new JWT(writeJWT(header, payload)); |
| 53 | + |
| 54 | + expect(() => validate(jwt)).not.toThrow(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('will run a subset of validation checks when configured', () => { |
| 58 | + const { header, payload } = context; |
| 59 | + const jwt = new JWT(writeJWT(header, payload)); |
| 60 | + |
| 61 | + const allChecks = DefaultIDTokenValidator.checks; |
| 62 | + const [_, ...otherChecks] = allChecks; |
| 63 | + DefaultIDTokenValidator.checks = otherChecks; |
| 64 | + |
| 65 | + expect(() => validate(jwt, { issuer: new URL('https://foo.okta.com') })) |
| 66 | + .not.toThrow(new JWTError('Invalid issuer (iss) claim')); |
| 67 | + |
| 68 | + DefaultIDTokenValidator.checks = allChecks; // resets validation checks |
| 69 | + expect(() => validate(jwt, { issuer: new URL('https://foo.okta.com') })) |
| 70 | + .toThrow(new JWTError('Invalid issuer (iss) claim')); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('throws when ID Token contains inconsistent claim', () => { |
| 74 | + it('issuer (iss)', () => { |
| 75 | + const { header, payload } = context; |
| 76 | + const jwt1 = new JWT(writeJWT(header, payload)); |
| 77 | + expect(() => validate(jwt1, { issuer: new URL('https://foo.okta.com') })) |
| 78 | + .toThrow(new JWTError('Invalid issuer (iss) claim')); |
| 79 | + |
| 80 | + payload.iss = 'https://foo.okta.com'; |
| 81 | + const jwt2 = new JWT(writeJWT(header, payload)); |
| 82 | + expect(() => validate(jwt2)).toThrow(new JWTError('Invalid issuer (iss) claim')); |
| 83 | + }); |
| 84 | + |
| 85 | + it('audience (aud)', () => { |
| 86 | + const { header, payload } = context; |
| 87 | + const jwt1 = new JWT(writeJWT(header, payload)); |
| 88 | + expect(() => validate(jwt1, { clientId: 'foobar' })) |
| 89 | + .toThrow(new JWTError('invalid audience (aud) claim')); |
| 90 | + |
| 91 | + payload.aud = 'foobar'; |
| 92 | + const jwt2 = new JWT(writeJWT(header, payload)); |
| 93 | + expect(() => validate(jwt2)).toThrow(new JWTError('invalid audience (aud) claim')); |
| 94 | + }); |
| 95 | + |
| 96 | + it('scheme (iss)', () => { |
| 97 | + const { header, payload } = context; |
| 98 | + payload.iss = 'http://fake.okta.com'; |
| 99 | + const jwt = new JWT(writeJWT(header, payload)); |
| 100 | + expect(() => validate(jwt, { issuer: new URL('http://fake.okta.com') })) |
| 101 | + .toThrow(new JWTError('issuer (iss) claim requires HTTPS')); |
| 102 | + }); |
| 103 | + |
| 104 | + it('algorithm (header.alg)', () => { |
| 105 | + const { header, payload } = context; |
| 106 | + const jwt1 = new JWT(writeJWT(header, payload)); |
| 107 | + expect(() => validate(jwt1, { context: { supportedAlgs: [] } })).not.toThrow(); |
| 108 | + expect(() => validate(jwt1, { context: {} })).not.toThrow(); |
| 109 | + expect(() => validate(jwt1, { context: undefined })).not.toThrow(); |
| 110 | + |
| 111 | + expect(() => validate(jwt1, { context: { supportedAlgs: ['HS256'] } })) |
| 112 | + .toThrow(new JWTError('Unsupported jwt signing algorithm')); |
| 113 | + |
| 114 | + header.alg = 'HS256'; |
| 115 | + const jwt2 = new JWT(writeJWT(header, payload)); |
| 116 | + expect(() => validate(jwt2)).toThrow(new JWTError('Unsupported jwt signing algorithm')); |
| 117 | + expect(() => validate(jwt2, { context: { supportedAlgs: ['RS256', 'HS256'] } })).not.toThrow(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('expirationTime (exp)', () => { |
| 121 | + const { header, payload } = context; |
| 122 | + payload.iat -= 600; // required to avoid different validation error |
| 123 | + payload.exp -= 600; |
| 124 | + const jwt = new JWT(writeJWT(header, payload)); |
| 125 | + expect(() => validate(jwt)).toThrow(new JWTError('jwt has expired')); |
| 126 | + }); |
| 127 | + |
| 128 | + it('issuedAtTime', () => { |
| 129 | + const { header, payload } = context; |
| 130 | + payload.iat -= 600; |
| 131 | + const jwt = new JWT(writeJWT(header, payload)); |
| 132 | + expect(() => validate(jwt)).toThrow(new JWTError('issuedAtTime (iat) exceeds grace interval')); |
| 133 | + |
| 134 | + const graceInterval = DefaultIDTokenValidator.issuedAtGraceInterval; |
| 135 | + DefaultIDTokenValidator.issuedAtGraceInterval = 1000; |
| 136 | + expect(() => validate(jwt)).not.toThrow(new JWTError('issuedAtTime (iat) exceeds grace interval')); |
| 137 | + DefaultIDTokenValidator.issuedAtGraceInterval = graceInterval; // restore graceInterval |
| 138 | + }); |
| 139 | + |
| 140 | + it('nonce', () => { |
| 141 | + const { header, payload } = context; |
| 142 | + const jwt = new JWT(writeJWT(header, payload)); |
| 143 | + expect(() => validate(jwt)).not.toThrow(new JWTError('nonce mismatch')); |
| 144 | + expect(() => validate(jwt, { context: { nonce: 'foo' } })).toThrow(new JWTError('nonce mismatch')); |
| 145 | + expect(() => validate(jwt, { context: { nonce: 'nonceuponatime' } })).not.toThrow(new JWTError('nonce mismatch')); |
| 146 | + }); |
| 147 | + |
| 148 | + it('maxAge', () => { |
| 149 | + const { header, payload } = context; |
| 150 | + const jwt1 = new JWT(writeJWT(header, payload)); |
| 151 | + expect(() => validate(jwt1)).not.toThrow(); |
| 152 | + expect(() => validate(jwt1, { context: { maxAge: 300 } })).toThrow(new JWTError('Invalid Authentication Time')); |
| 153 | + |
| 154 | + payload.auth_time = 1 / 0; |
| 155 | + const jwt2 = new JWT(writeJWT(header, payload)); |
| 156 | + expect(() => validate(jwt2, { context: { maxAge: 300 } })).toThrow(new JWTError('Invalid Authentication Time')); |
| 157 | + |
| 158 | + payload.auth_time = 'foobar'; |
| 159 | + const jwt3 = new JWT(writeJWT(header, payload)); |
| 160 | + expect(() => validate(jwt3, { context: { maxAge: 300 } })).toThrow(new JWTError('Invalid Authentication Time')); |
| 161 | + |
| 162 | + payload.auth_time = payload.iat - 300; |
| 163 | + const jwt4 = new JWT(writeJWT(header, payload)); |
| 164 | + expect(() => validate(jwt4, { context: { maxAge: 300 } })).toThrow(new JWTError('exceeds maxAge')); |
| 165 | + |
| 166 | + delete payload.iat; |
| 167 | + DefaultIDTokenValidator.checks = ['maxAge']; // isolates `maxAge` for test to avoid failing other validations |
| 168 | + const jwt5 = new JWT(writeJWT(header, payload)); |
| 169 | + expect(() => validate(jwt5, { context: { maxAge: 300 } })).toThrow(new JWTError('exceeds maxAge')); |
| 170 | + DefaultIDTokenValidator.checks = [...IDTokenValidator.allValidationChecks]; // resets validation checks |
| 171 | + }); |
| 172 | + |
| 173 | + it('subject', () => { |
| 174 | + const { header, payload } = context; |
| 175 | + payload.sub = ''; |
| 176 | + const jwt1 = new JWT(writeJWT(header, payload)); |
| 177 | + expect(() => validate(jwt1)).toThrow(new JWTError('Invalid subject (sub) claim')); |
| 178 | + |
| 179 | + delete payload.sub; |
| 180 | + const jwt2 = new JWT(writeJWT(header, payload)); |
| 181 | + expect(() => validate(jwt2)).not.toThrow(new JWTError('Invalid subject (sub) claim')); |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments