Skip to content
Myr

refactor(jwt): reduce the size of the code generated by minification (#4480)

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

* refactor(jwt): removes redundant condition checks * refactor(jwt): Use destructuring assignment instead of object * refactor(jwt): simplify conditional branching with ternary operators

authored by Taku Amano · committed by GitHub · parent d9b8b4b · Browse files · 1 file+28 added−42 removed
This commit

State

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

Diff stats

1 file+28 added−42 removed

  • other:+28 added−42 removed

1 file+28 added−42 removed

modified:src/utils/jwt/jwt.ts+28 added-42 removed
95 unmodified lines
96 iat: boolean97 aud?: string | string[] | RegExp98}99100type StrictVerifyOptionsWithAlg = {101 alg: SignatureAlgorithm102} & StrictVerifyOptions103100export const verify = async (101 token: string,102 publicKey: SignatureKey,103 algOrOptions?: SignatureAlgorithm | VerifyOptionsWithAlg104): Promise<JWTPayload> => {109 const optsIn = typeof algOrOptions === 'string' ? { alg: algOrOptions } : algOrOptions || {}110 const opts: StrictVerifyOptionsWithAlg = {111 alg: optsIn.alg ?? 'HS256',112 iss: optsIn.iss,113 nbf: optsIn.nbf ?? true,114 exp: optsIn.exp ?? true,115 iat: optsIn.iat ?? true,116 aud: optsIn.aud,117 }105 const {106 alg = 'HS256',107 iss,108 nbf = true,109 exp = true,110 iat = true,111 aud,112 } = typeof algOrOptions === 'string' ? { alg: algOrOptions } : algOrOptions || {}113114 const tokenParts = token.split('.')115 if (tokenParts.length !== 3) {116 throw new JwtTokenInvalid(token)
3 unmodified lines
120 if (!isTokenHeader(header)) {121 throw new JwtHeaderInvalid(header)122 }123 const now = (Date.now() / 1000) | 0129 if (opts.nbf && payload.nbf && payload.nbf > now) {124 if (nbf && payload.nbf && payload.nbf > now) {125 throw new JwtTokenNotBefore(token)126 }132 if (opts.exp && payload.exp && payload.exp <= now) {127 if (exp && payload.exp && payload.exp <= now) {128 throw new JwtTokenExpired(token)129 }135 if (opts.iat && payload.iat && now < payload.iat) {130 if (iat && payload.iat && now < payload.iat) {131 throw new JwtTokenIssuedAt(now, payload.iat)132 }138 if (opts.iss) {133 if (iss) {134 if (!payload.iss) {140 throw new JwtTokenIssuer(opts.iss, null)135 throw new JwtTokenIssuer(iss, null)136 }142 if (typeof opts.iss === 'string' && payload.iss !== opts.iss) {143 throw new JwtTokenIssuer(opts.iss, payload.iss)137 if (typeof iss === 'string' && payload.iss !== iss) {138 throw new JwtTokenIssuer(iss, payload.iss)139 }145 if (opts.iss instanceof RegExp && !opts.iss.test(payload.iss)) {146 throw new JwtTokenIssuer(opts.iss, payload.iss)140 if (iss instanceof RegExp && !iss.test(payload.iss)) {141 throw new JwtTokenIssuer(iss, payload.iss)142 }143 }144150 if (opts.aud) {145 if (aud) {146 if (!payload.aud) {147 throw new JwtPayloadRequiresAud(payload)148 }154 }149156 if (payload.aud) {150 const audiences = Array.isArray(payload.aud) ? payload.aud : [payload.aud]158 const matched = audiences.some((aud): boolean => {159 if (opts.aud instanceof RegExp && opts.aud.test(aud)) {160 return true161 } else if (typeof opts.aud === 'string') {162 if (aud === opts.aud) {163 return true164 }165 } else if (Array.isArray(opts.aud)) {166 if (opts.aud.includes(aud)) {167 return true168 }169 }170 return false171 })172 if (opts.aud && !matched) {173 throw new JwtTokenAudience(opts.aud, payload.aud)151 const matched = audiences.some((payloadAud): boolean =>152 aud instanceof RegExp153 ? aud.test(payloadAud)154 : typeof aud === 'string'155 ? payloadAud === aud156 : Array.isArray(aud) && aud.includes(payloadAud)157 )158 if (!matched) {159 throw new JwtTokenAudience(aud, payload.aud)160 }161 }162163 const headerPayload = token.substring(0, token.lastIndexOf('.'))164 const verified = await verifying(165 publicKey,180 opts.alg,166 alg,167 decodeBase64Url(tokenParts[2]),168 utf8Encoder.encode(headerPayload)169 )170 if (!verified) {
80 unmodified lines