fix(jwt): avoid using `enum` (#1485)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(jwt): avoid using `enum` * denoify
authored by Yusuke Wada · committed by GitHub · parent 94786a7 · Browse files · 2 files·+16 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+16 added−10 removed
- other:+16 added−10 removed
Provenance
- Commit
11f2fe5- Tree
5d82388- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
94786a7
2 files changed+16 added−10 removed
modified:deno_dist/utils/jwt/jwt.ts+8 added-5 removed
1223456726 unmodified lines343536373839404142432 unmodified lines4647484947505152535422 unmodified lines7778798078818283848511 unmodified lines979899100981011021031041057 unmodified lines11311411511611411711811912012139 unmodified linesimport { encodeBase64Url, decodeBase64Url } from '../../utils/encode.ts'import { AlgorithmTypes, JwtTokenIssuedAt } from './types.ts'import type { AlgorithmTypes } from './types.ts'import { JwtTokenIssuedAt } from './types.ts'import { JwtTokenInvalid, JwtTokenNotBefore, JwtTokenExpired,26 unmodified lines WrapKey = 'wrapKey', UnwrapKey = 'unwrapKey',}
type AlgorithmTypeName = keyof typeof AlgorithmTypes
const utf8Encoder = new TextEncoder()const utf8Decoder = new TextDecoder()
const encodeJwtPart = (part: unknown): string =>2 unmodified lines
const decodeJwtPart = (part: string): unknown => JSON.parse(utf8Decoder.decode(decodeBase64Url(part)))
const param = (name: AlgorithmTypes): AlgorithmParams => {const param = (name: AlgorithmTypeName): AlgorithmParams => { switch (name.toUpperCase()) { case 'HS256': return { name: 'HMAC',22 unmodified lines
const signing = async ( data: string, secret: string, alg: AlgorithmTypes = AlgorithmTypes.HS256 alg: AlgorithmTypeName = 'HS256'): Promise<ArrayBuffer> => { if (!crypto.subtle || !crypto.subtle.importKey) { throw new Error('`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.') }11 unmodified lines
export const sign = async ( payload: unknown, secret: string, alg: AlgorithmTypes = AlgorithmTypes.HS256 alg: AlgorithmTypeName = 'HS256'): Promise<string> => { const encodedPayload = encodeJwtPart(payload) const encodedHeader = encodeJwtPart({ alg, typ: 'JWT' })
7 unmodified lines
export const verify = async ( token: string, secret: string, alg: AlgorithmTypes = AlgorithmTypes.HS256 alg: AlgorithmTypeName = 'HS256' // eslint-disable-next-line @typescript-eslint/no-explicit-any): Promise<any> => { const tokenParts = token.split('.') if (tokenParts.length !== 3) {39 unmodified linesmodified:src/utils/jwt/jwt.ts+8 added-5 removed
1 import { encodeBase64Url, decodeBase64Url } from '../../utils/encode'2-import { AlgorithmTypes, JwtTokenIssuedAt } from './types'2+import type { AlgorithmTypes } from './types'3+import { JwtTokenIssuedAt } from './types'4 import {5 JwtTokenInvalid,6 JwtTokenNotBefore,7 JwtTokenExpired,26 unmodified lines34 WrapKey = 'wrapKey',35 UnwrapKey = 'unwrapKey',36 }37 38+type AlgorithmTypeName = keyof typeof AlgorithmTypes39+40 const utf8Encoder = new TextEncoder()41 const utf8Decoder = new TextDecoder()42 43 const encodeJwtPart = (part: unknown): string =>2 unmodified lines46 47 const decodeJwtPart = (part: string): unknown =>48 JSON.parse(utf8Decoder.decode(decodeBase64Url(part)))49 47-const param = (name: AlgorithmTypes): AlgorithmParams => {50+const param = (name: AlgorithmTypeName): AlgorithmParams => {51 switch (name.toUpperCase()) {52 case 'HS256':53 return {54 name: 'HMAC',22 unmodified lines77 78 const signing = async (79 data: string,80 secret: string,78- alg: AlgorithmTypes = AlgorithmTypes.HS25681+ alg: AlgorithmTypeName = 'HS256'82 ): Promise<ArrayBuffer> => {83 if (!crypto.subtle || !crypto.subtle.importKey) {84 throw new Error('`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.')85 }11 unmodified lines97 98 export const sign = async (99 payload: unknown,100 secret: string,98- alg: AlgorithmTypes = AlgorithmTypes.HS256101+ alg: AlgorithmTypeName = 'HS256'102 ): Promise<string> => {103 const encodedPayload = encodeJwtPart(payload)104 const encodedHeader = encodeJwtPart({ alg, typ: 'JWT' })105 7 unmodified lines113 114 export const verify = async (115 token: string,116 secret: string,114- alg: AlgorithmTypes = AlgorithmTypes.HS256117+ alg: AlgorithmTypeName = 'HS256'118 // eslint-disable-next-line @typescript-eslint/no-explicit-any119 ): Promise<any> => {120 const tokenParts = token.split('.')121 if (tokenParts.length !== 3) {39 unmodified lines