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
Provenance
- Commit
d87d996- Tree
4b56326- Author
fzn0x <fncolon@pm.me>- Committer
GitHub <noreply@github.com>- Parent
f7225ee
4 files changed+64 added−74 removed
modified:deno_dist/utils/jwt/jwt.ts+17 added-20 removed
123452345676 unmodified lines14151617201819202122232425282930313233343536262728293031323334353637383914 unmodified lines545556575960585960616226 unmodified lines89909192959693949599100969798991001014 unmodified linesimport { encodeBase64Url, decodeBase64Url } from '../../utils/encode.ts'import type { SignatureAlgorithm } from './jwa.ts'import { AlgorithmTypes } from './jwa.ts'import type { SignatureKey } from './jws.ts'import { signing, verifying } from './jws.ts'import { AlgorithmTypes, type SignatureAlgorithm } from './jwa.ts'import { signing, verifying, type SignatureKey } from './jws.ts'import { JwtHeaderInvalid, type JWTPayload } from './types.ts'import { JwtTokenInvalid, JwtTokenNotBefore,6 unmodified linesconst encodeJwtPart = (part: unknown): string => encodeBase64Url(utf8Encoder.encode(JSON.stringify(part))).replace(/=/g, '')const encodeSignaturePart = (buf: ArrayBufferLike): string => encodeBase64Url(buf).replace(/=/g, '')
const decodeJwtPart = (part: string): unknown =>const decodeJwtPart = (part: string): TokenHeader | JWTPayload | undefined => JSON.parse(utf8Decoder.decode(decodeBase64Url(part)))
export interface TokenHeader { alg: SignatureAlgorithm typ?: 'JWT'}
// eslint-disable-next-lineexport function isTokenHeader(obj: any): obj is TokenHeader { return ( typeof obj === 'object' && obj !== null && 'alg' in obj && Object.values(AlgorithmTypes).includes(obj.alg) && (!('typ' in obj) || obj.typ === 'JWT') )export function isTokenHeader(obj: unknown): obj is TokenHeader { if (typeof obj === 'object' && obj !== null) { const objWithAlg = obj as { [key: string]: unknown } return ( 'alg' in objWithAlg && Object.values(AlgorithmTypes).includes(objWithAlg.alg as AlgorithmTypes) && (!('typ' in objWithAlg) || objWithAlg.typ === 'JWT') ) } return false}
export const sign = async ( payload: JWTPayload,14 unmodified linesexport const verify = async ( token: string, publicKey: SignatureKey, alg: SignatureAlgorithm = 'HS256' // eslint-disable-next-line @typescript-eslint/no-explicit-any): Promise<any> => {): Promise<JWTPayload> => { const tokenParts = token.split('.') if (tokenParts.length !== 3) { throw new JwtTokenInvalid(token) }26 unmodified lines
return payload}
// eslint-disable-next-lineexport const decode = (token: string): { header: any; payload: any } => {export const decode = (token: string): { header: TokenHeader; payload: JWTPayload } => { try { const [h, p] = token.split('.') const header = decodeJwtPart(h) const payload = decodeJwtPart(p) const header = decodeJwtPart(h) as TokenHeader const payload = decodeJwtPart(p) as JWTPayload return { header, payload, }4 unmodified linesmodified:deno_dist/utils/jwt/types.ts+15 added-17 removed
59 unmodified lines60616263646566676869707172737475767778798064656667686970717273747576777859 unmodified lines
/** * JWT Payload */export type JWTPayload = | (unknown & {}) | { [key: string]: unknown /** * The token is checked to ensure it has not expired. */ exp?: number /** * The token is checked to ensure it is not being used before a specified time. */ nbf?: number /** * The token is checked to ensure it is not issued in the future. */ iat?: number }export type JWTPayload = { [key: string]: unknown /** * The token is checked to ensure it has not expired. */ exp?: number /** * The token is checked to ensure it is not being used before a specified time. */ nbf?: number /** * The token is checked to ensure it is not issued in the future. */ iat?: number}modified:src/utils/jwt/jwt.ts+17 added-20 removed
1 import { encodeBase64Url, decodeBase64Url } from '../../utils/encode'2-import type { SignatureAlgorithm } from './jwa'3-import { AlgorithmTypes } from './jwa'4-import type { SignatureKey } from './jws'5-import { signing, verifying } from './jws'2+import { AlgorithmTypes, type SignatureAlgorithm } from './jwa'3+import { signing, verifying, type SignatureKey } from './jws'4 import { JwtHeaderInvalid, type JWTPayload } from './types'5 import {6 JwtTokenInvalid,7 JwtTokenNotBefore,6 unmodified lines14 const encodeJwtPart = (part: unknown): string =>15 encodeBase64Url(utf8Encoder.encode(JSON.stringify(part))).replace(/=/g, '')16 const encodeSignaturePart = (buf: ArrayBufferLike): string => encodeBase64Url(buf).replace(/=/g, '')17 20-const decodeJwtPart = (part: string): unknown =>18+const decodeJwtPart = (part: string): TokenHeader | JWTPayload | undefined =>19 JSON.parse(utf8Decoder.decode(decodeBase64Url(part)))20 21 export interface TokenHeader {22 alg: SignatureAlgorithm23 typ?: 'JWT'24 }25 28-// eslint-disable-next-line29-export 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- )26+export 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 }37 38 export const sign = async (39 payload: JWTPayload,14 unmodified lines54 export 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 lines89 90 return payload91 }92 95-// eslint-disable-next-line96-export const decode = (token: string): { header: any; payload: any } => {93+export 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 linesmodified:src/utils/jwt/types.ts+15 added-17 removed
59 unmodified lines60 61 /**62 * JWT Payload63 */64-export 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- }64+export 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+}