Skip to content
Myr

refactor(utils/jwt): remove some `any` (#2684)

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

* refactor(jwt): remove some `any` * refactor(types): remove type confusion * chore: denoify & lint * chore: remove eslint comment

authored by fzn0x · committed by GitHub · parent f7225ee · Browse files · 4 files+64 added−74 removed
This commit

State

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

Diff stats

4 files+64 added−74 removed

  • other:+64 added−74 removed

4 files+64 added−74 removed

modified:deno_dist/utils/jwt/jwt.ts+17 added-20 removed
modified:deno_dist/utils/jwt/types.ts+15 added-17 removed
modified:src/utils/jwt/jwt.ts+17 added-20 removed
1import { encodeBase64Url, decodeBase64Url } from '../../utils/encode'2import type { SignatureAlgorithm } from './jwa'3import { AlgorithmTypes } from './jwa'4import type { SignatureKey } from './jws'5import { signing, verifying } from './jws'2import { AlgorithmTypes, type SignatureAlgorithm } from './jwa'3import { signing, verifying, type SignatureKey } from './jws'4import { JwtHeaderInvalid, type JWTPayload } from './types'5import {6 JwtTokenInvalid,7 JwtTokenNotBefore,
6 unmodified lines
14const encodeJwtPart = (part: unknown): string =>15 encodeBase64Url(utf8Encoder.encode(JSON.stringify(part))).replace(/=/g, '')16const encodeSignaturePart = (buf: ArrayBufferLike): string => encodeBase64Url(buf).replace(/=/g, '')1720const decodeJwtPart = (part: string): unknown =>18const decodeJwtPart = (part: string): TokenHeader | JWTPayload | undefined =>19 JSON.parse(utf8Decoder.decode(decodeBase64Url(part)))2021export interface TokenHeader {22 alg: SignatureAlgorithm23 typ?: 'JWT'24}2528// eslint-disable-next-line29export function isTokenHeader(obj: any): obj is TokenHeader {30 return (31 typeof obj === 'object' &&32 obj !== null &&33 'alg' in obj &&34 Object.values(AlgorithmTypes).includes(obj.alg) &&35 (!('typ' in obj) || obj.typ === 'JWT')36 )26export function isTokenHeader(obj: unknown): obj is TokenHeader {27 if (typeof obj === 'object' && obj !== null) {28 const objWithAlg = obj as { [key: string]: unknown }29 return (30 'alg' in objWithAlg &&31 Object.values(AlgorithmTypes).includes(objWithAlg.alg as AlgorithmTypes) &&32 (!('typ' in objWithAlg) || objWithAlg.typ === 'JWT')33 )34 }35 return false36}3738export const sign = async (39 payload: JWTPayload,
14 unmodified lines
54export const verify = async (55 token: string,56 publicKey: SignatureKey,57 alg: SignatureAlgorithm = 'HS256'59 // eslint-disable-next-line @typescript-eslint/no-explicit-any60): Promise<any> => {58): Promise<JWTPayload> => {59 const tokenParts = token.split('.')60 if (tokenParts.length !== 3) {61 throw new JwtTokenInvalid(token)62 }
26 unmodified lines
8990 return payload91}9295// eslint-disable-next-line96export const decode = (token: string): { header: any; payload: any } => {93export const decode = (token: string): { header: TokenHeader; payload: JWTPayload } => {94 try {95 const [h, p] = token.split('.')99 const header = decodeJwtPart(h)100 const payload = decodeJwtPart(p)96 const header = decodeJwtPart(h) as TokenHeader97 const payload = decodeJwtPart(p) as JWTPayload98 return {99 header,100 payload,101 }
4 unmodified lines
modified:src/utils/jwt/types.ts+15 added-17 removed
59 unmodified lines
6061/**62 * JWT Payload63 */64export type JWTPayload =65 | (unknown & {})66 | {67 [key: string]: unknown68 /**69 * The token is checked to ensure it has not expired.70 */71 exp?: number72 /**73 * The token is checked to ensure it is not being used before a specified time.74 */75 nbf?: number76 /**77 * The token is checked to ensure it is not issued in the future.78 */79 iat?: number80 }64export type JWTPayload = {65 [key: string]: unknown66 /**67 * The token is checked to ensure it has not expired.68 */69 exp?: number70 /**71 * The token is checked to ensure it is not being used before a specified time.72 */73 nbf?: number74 /**75 * The token is checked to ensure it is not issued in the future.76 */77 iat?: number78}