Skip to content
Myr

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

2 files+16 added−10 removed

modified:deno_dist/utils/jwt/jwt.ts+8 added-5 removed
modified:src/utils/jwt/jwt.ts+8 added-5 removed
1import { encodeBase64Url, decodeBase64Url } from '../../utils/encode'2import { AlgorithmTypes, JwtTokenIssuedAt } from './types'2import type { AlgorithmTypes } from './types'3import { JwtTokenIssuedAt } from './types'4import {5 JwtTokenInvalid,6 JwtTokenNotBefore,7 JwtTokenExpired,
26 unmodified lines
34 WrapKey = 'wrapKey',35 UnwrapKey = 'unwrapKey',36}3738type AlgorithmTypeName = keyof typeof AlgorithmTypes3940const utf8Encoder = new TextEncoder()41const utf8Decoder = new TextDecoder()4243const encodeJwtPart = (part: unknown): string =>
2 unmodified lines
4647const decodeJwtPart = (part: string): unknown =>48 JSON.parse(utf8Decoder.decode(decodeBase64Url(part)))4947const param = (name: AlgorithmTypes): AlgorithmParams => {50const param = (name: AlgorithmTypeName): AlgorithmParams => {51 switch (name.toUpperCase()) {52 case 'HS256':53 return {54 name: 'HMAC',
22 unmodified lines
7778const 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 lines
9798export 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 lines
113114export 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