Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jwt): Pass Cookie Options from jwt Middleware to getCookie/getSignedCookie #2403

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/middleware/jwt/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,54 @@ describe('JWT', () => {
expect(handlerExecuted).toBeFalsy()
})
})
describe('Credentials in signed cookie', () => {
let handlerExecuted: boolean

beforeEach(() => {
handlerExecuted = false
})

const app = new Hono()

app.use(
'/auth/*',
jwt({
secret: 'a-secret',
cookie: {
key: 'cookie_name',
secret: 'cookie_secret',
},
})
)

app.get('/auth/*', async (c) => {
handlerExecuted = true
const payload = c.get('jwtPayload')
return c.json(payload)
})

it('Should not authorize', async () => {
const req = new Request('http://localhost/auth/a')
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(401)
expect(await res.text()).toBe('Unauthorized')
expect(handlerExecuted).toBeFalsy()
})

it('Should authorize', async () => {
const url = 'http://localhost/auth/a'
const req = new Request(url, {
headers: new Headers({
Cookie:
'cookie_name=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE.i2NSvtJOXOPS9NDL1u8dqTYmMrzcD4mNSws6P6qmeV0%3D; Path=/',
}),
})
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ message: 'hello world' })
expect(handlerExecuted).toBeTruthy()
})
})
})
13 changes: 10 additions & 3 deletions src/middleware/jwt/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Context } from '../../context'
import { getCookie } from '../../helper/cookie'
import { getCookie, getSignedCookie } from '../../helper/cookie'
import type { CookiePrefixOptions } from '../../utils/cookie'
import { HTTPException } from '../../http-exception'
import type { MiddlewareHandler } from '../../types'
import { Jwt } from '../../utils/jwt'
Expand All @@ -15,7 +16,7 @@ declare module '../../context' {

export const jwt = (options: {
secret: string
cookie?: string
cookie?: string | { key: string, secret?: string | BufferSource, prefixOptions?: CookiePrefixOptions }
alg?: string
}): MiddlewareHandler => {
if (!options) {
Expand Down Expand Up @@ -43,7 +44,13 @@ export const jwt = (options: {
token = parts[1]
}
} else if (options.cookie) {
token = getCookie(ctx)[options.cookie]
if (typeof options.cookie == 'string') {
token = getCookie(ctx, options.cookie)
} else if (options.cookie.secret) {
token = await getSignedCookie(ctx, options.cookie.secret, options.cookie.key, options.cookie.prefixOptions)
} else {
token = getCookie(ctx, options.cookie.key, options.cookie.prefixOptions)
}
}

if (!token) {
Expand Down