fix(basic/bearer): support "nested" middleware (#610)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent dc82384 · Browse files · 3 files·+50 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+50 added−22 removed
- tests:+49 added−21 removed
- other:+1 added−1 removed
Provenance
- Commit
6d0da56- Tree
a916eb0- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
dc82384
3 files changed+50 added−22 removed
modified:src/middleware/basic-auth/index.test.ts+19 added-21 removed
10 unmodified lines1112131415151617181950 unmodified lines70717273747576747578797677787982838081828386878485868790918889909194959293949598991001019697989910010110210339 unmodified lines14314414514614914714814915015114 unmodified lines16616716816917217017117217317413 unmodified lines18818919019119419219310 unmodified lines afterAll(() => { global.crypto = crypto }) beforeEach(() => { handlerExecuted = false handlerExecuted = false })
const app = new Hono()
50 unmodified lines hashFunction: (data: string) => SHA256(data).toString(), }) )
// To Hono maintainers: I don't think we can support nested middleware when the middleware returns early // since `next()` is never called in this case. Feel free to correct me /* app.use('/nested/*', async (c, next) => { app.use('/nested/*', async (c, next) => { const auth = basicAuth({ username: username, password: password }) await auth(c, next) }) */ return auth(c, next) })
app.get('/auth/*', (c) => { handlerExecuted = true return c.text('auth') handlerExecuted = true return c.text('auth') }) app.get('/auth-unicode/*', (c) => { handlerExecuted = true return c.text('auth') handlerExecuted = true return c.text('auth') }) app.get('/auth-multi/*', (c) => { handlerExecuted = true return c.text('auth') handlerExecuted = true return c.text('auth') }) app.get('/auth-override-func/*', (c) => { handlerExecuted = true return c.text('auth') handlerExecuted = true return c.text('auth') })
/* app.get('/nested/*', (c) => { handlerExecuted = true return c.text('nested') }) */ app.get('/nested/*', (c) => { handlerExecuted = true return c.text('nested') })
it('Should not authorize', async () => { const req = new Request('http://localhost/auth/a') const res = await app.request(req)39 unmodified lines expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('auth')
handlerExecuted = false; handlerExecuted = false credential = Buffer.from(usernameC + ':' + passwordC).toString('base64') req = new Request('http://localhost/auth-multi/c') req.headers.set('Authorization', `Basic ${credential}`) res = await app.request(req)14 unmodified lines expect(res.status).toBe(200) expect(await res.text()).toBe('auth') })
/* it('Should authorize - nested', async () => { it('Should authorize - nested', async () => { const credential = Buffer.from(username + ':' + password).toString('base64')
const req = new Request('http://localhost/nested') req.headers.set('Authorization', `Basic ${credential}`)13 unmodified lines expect(handlerExecuted).toBeFalsy() expect(res).not.toBeNull() expect(res.status).toBe(401) expect(await res.text()).toBe('Unauthorized') }) */ })})modified:src/middleware/bearer-auth/index.test.ts+30 added
8 unmodified lines9 beforeEach(async () => {10 app = new Hono()11 handlerExecuted = false12 token = 'abcdefg12345-._~+/='13+14 app.use('/auth/*', bearerAuth({ token }))15 app.use('/auth/*', async (c, next) => {16 c.header('x-custom', 'foo')17 await next()7 unmodified lines25 app.get('/authBot/*', (c) => {26 handlerExecuted = true27 return c.text('auth bot')28 })29+30+ app.use('/nested/*', async (c, next) => {31+ const auth = bearerAuth({ token })32+ return auth(c, next)33+ })34+ app.get('/nested/*', (c) => {35+ handlerExecuted = true36+ return c.text('auth nested')37+ })38 })39 40 it('Should authorize', async () => {41 const req = new Request('http://localhost/auth/a')55 unmodified lines97 expect(handlerExecuted).toBeFalsy()98 expect(res.status).toBe(400)99 expect(await res.text()).toBe('Bad Request')100 })101+102+ it('Should authorize - nested', async () => {103+ const req = new Request('http://localhost/nested/a')104+ req.headers.set('Authorization', 'Bearer abcdefg12345-._~+/=')105+ const res = await app.request(req)106+ expect(res).not.toBeNull()107+ expect(res.status).toBe(200)108+ expect(handlerExecuted).toBeTruthy()109+ expect(await res.text()).toBe('auth nested')110+ })111+112+ it('Should not authorize - nested', async () => {113+ const req = new Request('http://localhost/nested/a')114+ req.headers.set('Authorization', 'Bearer invalid-token')115+ const res = await app.request(req)116+ expect(res).not.toBeNull()117+ expect(handlerExecuted).toBeFalsy()118+ expect(res.status).toBe(401)119+ expect(await res.text()).toBe('Unauthorized')120+ })121 })modified:src/hono.ts+1 added-1 removed
27 unmodified lines28 D extends ValidatedData = ValidatedData29 > = (30 c: Context<RequestParamKeyType, E, D>,31 next: Next32-) => Response | Promise<Response> | Promise<void> | Promise<Response | undefined>32+) => Response | Promise<Response> | Promise<void> | Promise<Response | undefined | void>33 34 export type MiddlewareHandler = <E extends Partial<Environment> = Environment>(35 c: Context<string, E>,36 next: Next250 unmodified lines