Skip to content
Myr

Merge commit from fork

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat(jwt): add audience validation for JWT tokens * remove unused import * fix(jwt): update to support verify aud string[] * fix(jwt): return boolean in verify function * fix(jwt): rename JwtPayloadRequireAud to JwtPayloadRequiresAud

authored by Kazuma Okada · committed by GitHub · parent 4cbad8b · Browse files · 3 files+317 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+317 added−0 removed

  • tests:+261 added−0 removed
  • other:+56 added−0 removed

3 files+317 added−0 removed

modified:src/utils/jwt/jwt.test.ts+261 added
5 unmodified lines
6import * as JWT from './jwt'7import { verifyWithJwks } from './jwt'8import {9 JwtAlgorithmNotImplemented,10 JwtPayloadRequiresAud,11 JwtTokenAudience,12 JwtTokenExpired,13 JwtTokenInvalid,14 JwtTokenIssuedAt,15 JwtTokenIssuer,
211 unmodified lines
227 expect(err).toBeUndefined()228 expect(authorized?.iss).toEqual('hello')229 })230231 it('JwtPayloadRequireAud', async () => {232 const tok =233 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiaWF0IjoxfQ.3Yd0dDicCKA6zu_G6AvxMX_fRH5wMz9gMCedOsYNAGc'234 const secret = 'a-secret'235 let err236 let authorized237 try {238 authorized = await JWT.verify(tok, secret, {239 alg: AlgorithmTypes.HS256,240 aud: 'correct-audience',241 })242 } catch (e) {243 err = e244 }245 expect(err).toEqual(new JwtPayloadRequiresAud({ iss: 'https://issuer.example', iat: 1 }))246 expect(authorized).toBeUndefined()247 })248249 it('JwtTokenAudience(correct string - string)', async () => {250 const tok =251 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjoiY29ycmVjdC1hdWRpZW5jZSIsImlhdCI6MX0.z8T6szX-k66de4xB9OFbpWAOfx0RTqKSUPBcdpSY5nk'252 const secret = 'a-secret'253 let err254 let authorized255 try {256 authorized = await JWT.verify(tok, secret, {257 alg: AlgorithmTypes.HS256,258 aud: 'correct-audience',259 })260 } catch (e) {261 err = e262 }263 expect(err).toBeUndefined()264 expect(authorized?.aud).toEqual('correct-audience')265 })266267 it('JwtTokenAudience(correct string - string[])', async () => {268 const tok =269 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjoiY29ycmVjdC1hdWRpZW5jZSIsImlhdCI6MX0.z8T6szX-k66de4xB9OFbpWAOfx0RTqKSUPBcdpSY5nk'270 const secret = 'a-secret'271 let err272 let authorized273 try {274 authorized = await JWT.verify(tok, secret, {275 alg: AlgorithmTypes.HS256,276 aud: ['correct-audience', 'other-audience'],277 })278 } catch (e) {279 err = e280 }281 expect(err).toBeUndefined()282 expect(authorized?.aud).toEqual('correct-audience')283 })284285 it('JwtTokenAudience(correct string - RegExp)', async () => {286 const tok =287 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjoiY29ycmVjdC1hdWRpZW5jZSIsImlhdCI6MX0.z8T6szX-k66de4xB9OFbpWAOfx0RTqKSUPBcdpSY5nk'288 const secret = 'a-secret'289 let err290 let authorized291 try {292 authorized = await JWT.verify(tok, secret, {293 alg: AlgorithmTypes.HS256,294 aud: /^correct-audience$/,295 })296 } catch (e) {297 err = e298 }299 expect(err).toBeUndefined()300 expect(authorized?.aud).toEqual('correct-audience')301 })302303 it('JwtTokenAudience(correct string[] - string)', async () => {304 const tok =305 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjpbImNvcnJlY3QtYXVkaWVuY2UiLCJvdGhlci1hdWRpZW5jZSJdLCJpYXQiOjF9.l73pNR5zMMAyuoN3f32hKtRJkoxZNzgTcVBZ2A2EsJY'306 const secret = 'a-secret'307 let err308 let authorized309 try {310 authorized = await JWT.verify(tok, secret, {311 alg: AlgorithmTypes.HS256,312 aud: 'correct-audience',313 })314 } catch (e) {315 err = e316 }317 expect(err).toBeUndefined()318 expect(authorized?.aud).toEqual(['correct-audience', 'other-audience'])319 })320321 it('JwtTokenAudience(correct string[] - string[])', async () => {322 const tok =323 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjpbImNvcnJlY3QtYXVkaWVuY2UiLCJvdGhlci1hdWRpZW5jZSJdLCJpYXQiOjF9.l73pNR5zMMAyuoN3f32hKtRJkoxZNzgTcVBZ2A2EsJY'324 const secret = 'a-secret'325 let err326 let authorized327 try {328 authorized = await JWT.verify(tok, secret, {329 alg: AlgorithmTypes.HS256,330 aud: ['correct-audience', 'test'],331 })332 } catch (e) {333 err = e334 }335 expect(err).toBeUndefined()336 expect(authorized?.aud).toEqual(['correct-audience', 'other-audience'])337 })338339 it('JwtTokenAudience(correct string[] - RegExp)', async () => {340 const tok =341 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjpbImNvcnJlY3QtYXVkaWVuY2UiLCJvdGhlci1hdWRpZW5jZSJdLCJpYXQiOjF9.l73pNR5zMMAyuoN3f32hKtRJkoxZNzgTcVBZ2A2EsJY'342 const secret = 'a-secret'343 let err344 let authorized345 try {346 authorized = await JWT.verify(tok, secret, {347 alg: AlgorithmTypes.HS256,348 aud: /^correct-audience$/,349 })350 } catch (e) {351 err = e352 }353 expect(err).toBeUndefined()354 expect(authorized?.aud).toEqual(['correct-audience', 'other-audience'])355 })356357 it('JwtTokenAudience(wrong string - string)', async () => {358 const tok =359 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjoid3JvbmctYXVkaWVuY2UiLCJpYXQiOjF9.2vTYLiYL5r6qN-iRQ0VSfXh4ioLFtNzo0qc-OoPZmow'360 const secret = 'a-secret'361 let err362 let authorized363 try {364 authorized = await JWT.verify(tok, secret, {365 alg: AlgorithmTypes.HS256,366 aud: 'correct-audience',367 })368 } catch (e) {369 err = e370 }371 expect(err).toEqual(new JwtTokenAudience('correct-audience', 'wrong-audience'))372 expect(authorized).toBeUndefined()373 })374375 it('JwtTokenAudience(wrong string - string[])', async () => {376 const tok =377 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjoid3JvbmctYXVkaWVuY2UiLCJpYXQiOjF9.2vTYLiYL5r6qN-iRQ0VSfXh4ioLFtNzo0qc-OoPZmow'378 const secret = 'a-secret'379 let err380 let authorized381 try {382 authorized = await JWT.verify(tok, secret, {383 alg: AlgorithmTypes.HS256,384 aud: ['correct-audience', 'other-audience'],385 })386 } catch (e) {387 err = e388 }389 expect(err).toEqual(390 new JwtTokenAudience(['correct-audience', 'other-audience'], 'wrong-audience')391 )392 expect(authorized).toBeUndefined()393 })394395 it('JwtTokenAudience(wrong string - RegExp)', async () => {396 const tok =397 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjoid3JvbmctYXVkaWVuY2UiLCJpYXQiOjF9.2vTYLiYL5r6qN-iRQ0VSfXh4ioLFtNzo0qc-OoPZmow'398 const secret = 'a-secret'399 let err400 let authorized401 try {402 authorized = await JWT.verify(tok, secret, {403 alg: AlgorithmTypes.HS256,404 aud: /^correct-audience$/,405 })406 } catch (e) {407 err = e408 }409 expect(err).toEqual(new JwtTokenAudience(/^correct-audience$/, 'wrong-audience'))410 expect(authorized).toBeUndefined()411 })412413 it('JwtTokenAudience(wrong string[] - string)', async () => {414 const tok =415 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjpbIndyb25nLWF1ZGllbmNlIiwib3RoZXItYXVkaWVuY2UiXSwiaWF0IjoxfQ.YTAM1xtKP4AeEeQSFQ81rcJM1leW_uDayQcTE6LxoP0'416 const secret = 'a-secret'417 let err418 let authorized419 try {420 authorized = await JWT.verify(tok, secret, {421 alg: AlgorithmTypes.HS256,422 aud: 'correct-audience',423 })424 } catch (e) {425 err = e426 }427 expect(err).toEqual(428 new JwtTokenAudience('correct-audience', ['wrong-audience', 'other-audience'])429 )430 expect(authorized).toBeUndefined()431 })432433 it('JwtTokenAudience(wrong string[] - string[])', async () => {434 const tok =435 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjpbIndyb25nLWF1ZGllbmNlIiwib3RoZXItYXVkaWVuY2UiXSwiaWF0IjoxfQ.YTAM1xtKP4AeEeQSFQ81rcJM1leW_uDayQcTE6LxoP0'436 const secret = 'a-secret'437 let err438 let authorized439 try {440 authorized = await JWT.verify(tok, secret, {441 alg: AlgorithmTypes.HS256,442 aud: ['correct-audience', 'test'],443 })444 } catch (e) {445 err = e446 }447 expect(err).toEqual(448 new JwtTokenAudience(['correct-audience', 'test'], ['wrong-audience', 'other-audience'])449 )450 expect(authorized).toBeUndefined()451 })452453 it('JwtTokenAudience(wrong string[] - RegExp)', async () => {454 const tok =455 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjpbIndyb25nLWF1ZGllbmNlIiwib3RoZXItYXVkaWVuY2UiXSwiaWF0IjoxfQ.YTAM1xtKP4AeEeQSFQ81rcJM1leW_uDayQcTE6LxoP0'456 const secret = 'a-secret'457 let err458 let authorized459 try {460 authorized = await JWT.verify(tok, secret, {461 alg: AlgorithmTypes.HS256,462 aud: /^correct-audience$/,463 })464 } catch (e) {465 err = e466 }467 expect(err).toEqual(468 new JwtTokenAudience(/^correct-audience$/, ['wrong-audience', 'other-audience'])469 )470 expect(authorized).toBeUndefined()471 })472473 it('JwtTokenAudience (no aud option and wrong aud in payload)', async () => {474 const tok =475 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlIiwiYXVkIjoid3JvbmctYXVkaWVuY2UiLCJpYXQiOjF9.2vTYLiYL5r6qN-iRQ0VSfXh4ioLFtNzo0qc-OoPZmow'476 const secret = 'a-secret'477 let err478 let authorized479 try {480 authorized = await JWT.verify(tok, secret, {481 alg: AlgorithmTypes.HS256,482 })483 } catch (e) {484 err = e485 }486 expect(err).toBeUndefined()487 expect(authorized?.aud).toEqual('wrong-audience')488 })489490 it('HS256 sign & verify & decode', async () => {491 const payload = { message: 'hello world' }492 const secret = 'a-secret'493 const tok = await JWT.sign(payload, secret, AlgorithmTypes.HS256)
440 unmodified lines
modified:src/utils/jwt/jwt.ts+33 added
10 unmodified lines
11import type { HonoJsonWebKey, SignatureKey } from './jws'12import {13 JwtHeaderInvalid,14 JwtHeaderRequiresKid,15 JwtPayloadRequiresAud,16 JwtTokenAudience,17 JwtTokenExpired,18 JwtTokenInvalid,19 JwtTokenIssuedAt,20 JwtTokenIssuer,
58 unmodified lines
79 /** Verify the `exp` claim (default: `true`) */80 exp?: boolean81 /** Verify the `iat` claim (default: `true`) */82 iat?: boolean83 /** Acceptable audience(s) for the token */84 aud?: string | string[] | RegExp85}8687export type VerifyOptionsWithAlg = {88 /** The algorithm used for decoding the token */
4 unmodified lines
93 iss?: string | RegExp94 nbf: boolean95 exp: boolean96 iat: boolean97 aud?: string | string[] | RegExp98}99100type StrictVerifyOptionsWithAlg = {101 alg: SignatureAlgorithm
10 unmodified lines
112 iss: optsIn.iss,113 nbf: optsIn.nbf ?? true,114 exp: optsIn.exp ?? true,115 iat: optsIn.iat ?? true,116 aud: optsIn.aud,117 }118119 const tokenParts = token.split('.')120 if (tokenParts.length !== 3) {
25 unmodified lines
146 throw new JwtTokenIssuer(opts.iss, payload.iss)147 }148 }149150 if (opts.aud) {151 if (!payload.aud) {152 throw new JwtPayloadRequiresAud(payload)153 }154 }155156 if (payload.aud) {157 const audiences = Array.isArray(payload.aud) ? payload.aud : [payload.aud]158 const matched = audiences.some((aud): boolean => {159 if (opts.aud instanceof RegExp && opts.aud.test(aud)) {160 return true161 } else if (typeof opts.aud === 'string') {162 if (aud === opts.aud) {163 return true164 }165 } else if (Array.isArray(opts.aud)) {166 if (opts.aud.includes(aud)) {167 return true168 }169 }170 return false171 })172 if (opts.aud && !matched) {173 throw new JwtTokenAudience(opts.aud, payload.aud)174 }175 }176177 const headerPayload = token.substring(0, token.lastIndexOf('.'))178 const verified = await verifying(179 publicKey,180 opts.alg,
84 unmodified lines
modified:src/utils/jwt/types.ts+23 added