fix(jwt): the alg option should be same as the one in options (#180)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Minghe · committed by GitHub · parent 1149729 · Browse files · 3 files·+78 added−2 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+78 added−2 removed
- tests:+76 added−1 removed
- other:+2 added−1 removed
Provenance
- Commit
201873e- Tree
c79feb5- Author
Minghe <h.minghe@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
1149729
3 files changed+78 added−2 removed
modified:src/middleware/jwt/index.ts+2 added-1 removed
1234567824 unmodified lines3334353636373839404112 unmodified linesimport type { Context } from '@/context'import type { Next } from '@/hono'import { Jwt } from '@/utils/jwt'import type { AlgorithmTypes } from '@/utils/jwt/types'
export const jwt = (options: { secret: string; alg?: string }) => { if (!options) { throw new Error('JWT auth middleware requires options for "secret')24 unmodified lines
let authorized = false let msg = '' try { authorized = await Jwt.verify(parts[1], options.secret) authorized = await Jwt.verify(parts[1], options.secret, options.alg as AlgorithmTypes) } catch (e) { msg = `${e}` } if (authorized) {12 unmodified linesmodified:src/utils/jwt/jwt.test.ts+65 added-1 removed
122345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717295 unmodified linesimport * as JWT from './jwt'import { AlgorithmTypes, JwtTokenSignatureMismatched } from './types'import { AlgorithmTypes, JwtTokenSignatureMismatched, JwtAlorithmNotImplemented, JwtTokenInvalid, JwtTokenNotBefore, JwtTokenExpired,} from './types'
describe('JWT', () => { it('JwtAlorithmNotImplemented', async () => { const payload = { message: 'hello world' } const secret = 'a-secret' const alg = '' let tok = '' let err: JwtAlorithmNotImplemented try { tok = await JWT.sign(payload, secret, alg as AlgorithmTypes) } catch (e) { err = e as JwtAlorithmNotImplemented } expect(tok).toBe('') expect(err).toEqual(new JwtAlorithmNotImplemented(alg)) })
it('JwtTokenInvalid', async () => { const tok = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ' const secret = 'a-secret' let err: JwtTokenInvalid let authorized = false try { authorized = await JWT.verify(tok, secret, AlgorithmTypes.HS256) } catch (e) { err = e as JwtTokenInvalid } expect(err).toEqual(new JwtTokenInvalid(tok)) expect(authorized).toBe(false) })
it.only('JwtTokenNotBefore', async () => { const tok = 'eyJraWQiOiJFemF7bVZWbnd0TUpUNEFveFVtT0dILWJ0Y2VUVFM3djBYcEJuMm5ZZ2VjIiwiYWxnIjoiSFMyNTYifQ.eyJyb2xlIjoiYXBpX3JvbGUiLCJuYmYiOjE2NjQ1ODI0MDB9.BatadbUj5e41OZ33odEFTAndQFzX0w9aAgpQPgO-zaQ' const secret = 'a-secret' let err: JwtTokenNotBefore let authorized = false try { authorized = await JWT.verify(tok, secret, AlgorithmTypes.HS256) } catch (e) { err = e as JwtTokenNotBefore } expect(err).toEqual(new JwtTokenNotBefore(tok)) expect(authorized).toBe(false) })
it('JwtTokenExpired', async () => { const tok = 'eyJraWQiOiJFemF6bVZWbnd0TUpUNEFveFVtT0dILWJ0Y2VUVFM3djBYcEJuMm5ZZ2VjIiwiYWxnIjoiSFMyNTYifQ.eyJyb2xlIjoiYXBpX3JvbGUiLCJleHAiOjE2MzMwNDY0MDB9.Gmq_dozOnwzqkMUMEm7uny7cMZuF1d0QkCnmRXAbTEk' const secret = 'a-secret' let err let authorized = false try { authorized = await JWT.verify(tok, secret, AlgorithmTypes.HS256) } catch (e) { err = e } expect(err).toEqual(new JwtTokenExpired(tok)) expect(authorized).toBe(false) })
it('HS256 sign & verify & decode', async () => { const payload = { message: 'hello world' } const secret = 'a-secret' const tok = await JWT.sign(payload, secret, AlgorithmTypes.HS256)95 unmodified linesadded:src/utils/jwt/types.test.ts+11 added
1234567891011import { AlgorithmTypes } from './types'
describe('Types', () => { it('AlgorithmTypes', () => { expect('HS256' as AlgorithmTypes).toBe(AlgorithmTypes.HS256) expect('HS384' as AlgorithmTypes).toBe(AlgorithmTypes.HS384) expect('HS512' as AlgorithmTypes).toBe(AlgorithmTypes.HS512) expect(undefined as AlgorithmTypes).toBe(undefined) expect('' as AlgorithmTypes).toBe('') })})