Skip to content

Commit

Permalink
refactor: jwt (#2011)
Browse files Browse the repository at this point in the history
* refactor jwt

* refactor change to camel case naming
  • Loading branch information
ariskemper authored Jan 17, 2024
1 parent 8a1d8fb commit 5fa3185
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 38 deletions.
54 changes: 35 additions & 19 deletions deno_dist/middleware/jwt/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Context } from '../../context.ts'
import { HTTPException } from '../../http-exception.ts'
import type { MiddlewareHandler } from '../../types.ts'
import { Jwt } from '../../utils/jwt/index.ts'
Expand Down Expand Up @@ -30,13 +31,13 @@ export const jwt = (options: {
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]
}
Expand All @@ -45,13 +46,13 @@ export const jwt = (options: {
}

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
Expand All @@ -62,14 +63,14 @@ export const jwt = (options: {
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)
Expand All @@ -78,6 +79,21 @@ export const jwt = (options: {
}
}

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.verify
export const decode = Jwt.decode
export const sign = Jwt.sign
54 changes: 35 additions & 19 deletions src/middleware/jwt/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Context } from '../../context'
import { HTTPException } from '../../http-exception'
import type { MiddlewareHandler } from '../../types'
import { Jwt } from '../../utils/jwt'
Expand Down Expand Up @@ -30,13 +31,13 @@ export const jwt = (options: {
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]
}
Expand All @@ -45,13 +46,13 @@ export const jwt = (options: {
}

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
Expand All @@ -62,14 +63,14 @@ export const jwt = (options: {
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)
Expand All @@ -78,6 +79,21 @@ export const jwt = (options: {
}
}

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.verify
export const decode = Jwt.decode
export const sign = Jwt.sign

0 comments on commit 5fa3185

Please # to comment.