refactor: jwt (#2011)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* refactor jwt * refactor change to camel case naming
authored by Aris Kemper · committed by GitHub · parent 8a1d8fb · Browse files · 2 files·+70 added−38 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+70 added−38 removed
- other:+70 added−38 removed
Provenance
- Commit
5fa3185- Tree
f8cc8e3- Author
Aris Kemper <aris.github@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
8a1d8fb
2 files changed+70 added−38 removed
modified:deno_dist/middleware/jwt/index.ts+35 added-19 removed
1234524 unmodified lines3031323333343536373435363738394039414243444546474848495051524950515253545554565758592 unmodified lines626364656566676869706667686970717273727475767778798081828384858687888990919293949596979899import type { Context } from '../../context.ts'import { HTTPException } from '../../http-exception.ts'import type { MiddlewareHandler } from '../../types.ts'import { Jwt } from '../../utils/jwt/index.ts'import type { AlgorithmTypes } from '../../utils/jwt/types.ts'24 unmodified lines let token if (credentials) { const parts = credentials.split(/\s+/) if (parts.length !== 2) { const res = new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="invalid credentials structure"`, }, throw new HTTPException(401, { res: unauthorizedResponse({ ctx, error: 'invalid_request', errDescription: 'invalid credentials structure', }), }) throw new HTTPException(401, { res }) } else { token = parts[1] } } else if (options.cookie) { token = ctx.req.cookie(options.cookie) }
if (!token) { const res = new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="no authorization included in request"`, }, throw new HTTPException(401, { res: unauthorizedResponse({ ctx, error: 'invalid_request', errDescription: 'no authorization included in request', }), }) throw new HTTPException(401, { res }) }
let payload let msg = ''2 unmodified lines } catch (e) { msg = `${e}` } if (!payload) { const res = new Response('Unauthorized', { status: 401, statusText: msg, headers: { 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_token",error_description="token verification failure"`, }, throw new HTTPException(401, { res: unauthorizedResponse({ ctx, error: 'invalid_token', statusText: msg, errDescription: 'token verification failure', }), }) throw new HTTPException(401, { res }) }
ctx.set('jwtPayload', payload)
await next() }}
function unauthorizedResponse(opts: { ctx: Context error: string errDescription: string statusText?: string}) { return new Response('Unauthorized', { status: 401, statusText: opts.statusText, headers: { 'WWW-Authenticate': `Bearer realm="${opts.ctx.req.url}",error="${opts.error}",error_description="${opts.errDescription}"`, }, })}
export const verify = Jwt.verifyexport const decode = Jwt.decodeexport const sign = Jwt.signmodified:src/middleware/jwt/index.ts+35 added-19 removed
1234524 unmodified lines3031323333343536373435363738394039414243444546474848495051524950515253545554565758592 unmodified lines626364656566676869706667686970717273727475767778798081828384858687888990919293949596979899import type { Context } from '../../context'import { HTTPException } from '../../http-exception'import type { MiddlewareHandler } from '../../types'import { Jwt } from '../../utils/jwt'import type { AlgorithmTypes } from '../../utils/jwt/types'24 unmodified lines let token if (credentials) { const parts = credentials.split(/\s+/) if (parts.length !== 2) { const res = new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="invalid credentials structure"`, }, throw new HTTPException(401, { res: unauthorizedResponse({ ctx, error: 'invalid_request', errDescription: 'invalid credentials structure', }), }) throw new HTTPException(401, { res }) } else { token = parts[1] } } else if (options.cookie) { token = ctx.req.cookie(options.cookie) }
if (!token) { const res = new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="no authorization included in request"`, }, throw new HTTPException(401, { res: unauthorizedResponse({ ctx, error: 'invalid_request', errDescription: 'no authorization included in request', }), }) throw new HTTPException(401, { res }) }
let payload let msg = ''2 unmodified lines } catch (e) { msg = `${e}` } if (!payload) { const res = new Response('Unauthorized', { status: 401, statusText: msg, headers: { 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_token",error_description="token verification failure"`, }, throw new HTTPException(401, { res: unauthorizedResponse({ ctx, error: 'invalid_token', statusText: msg, errDescription: 'token verification failure', }), }) throw new HTTPException(401, { res }) }
ctx.set('jwtPayload', payload)
await next() }}
function unauthorizedResponse(opts: { ctx: Context error: string errDescription: string statusText?: string}) { return new Response('Unauthorized', { status: 401, statusText: opts.statusText, headers: { 'WWW-Authenticate': `Bearer realm="${opts.ctx.req.url}",error="${opts.error}",error_description="${opts.errDescription}"`, }, })}
export const verify = Jwt.verifyexport const decode = Jwt.decodeexport const sign = Jwt.sign