Skip to content
Myr

feat(bearer-auth): add `verifyToken` option (#2449)

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

* feat(bearer): add `verifyToken` option * denoify * allow not promise

authored by Yusuke Wada · committed by GitHub · parent 274191c · Browse files · 3 files+72 added−16 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+72 added−16 removed

  • tests:+32 added−0 removed
  • other:+40 added−16 removed

3 files+72 added−16 removed

modified:deno_dist/middleware/bearer-auth/index.ts+20 added-8 removed
modified:src/middleware/bearer-auth/index.test.ts+32 added
41 unmodified lines
42 app.get('/auths/*', (c) => {43 handlerExecuted = true44 return c.text('auths')45 })4647 app.use(48 '/auth-verify-token/*',49 bearerAuth({50 verifyToken: async (token, c) => {51 return c.req.path === '/auth-verify-token' && token === 'dynamic-token'52 },53 })54 )55 app.get('/auth-verify-token/*', (c) => {56 handlerExecuted = true57 return c.text('auth-verify-token')58 })59 })6061 it('Should authorize', async () => {62 const req = new Request('http://localhost/auth/a')
93 unmodified lines
156 expect(res2.status).toBe(200)157 expect(handlerExecuted).toBeTruthy()158 expect(await res2.text()).toBe('auths')159 })160161 it('Should authorize - verifyToken option', async () => {162 const res = await app.request('/auth-verify-token', {163 headers: { Authorization: 'Bearer dynamic-token' },164 })165 expect(res).not.toBeNull()166 expect(res.status).toBe(200)167 expect(handlerExecuted).toBeTruthy()168 expect(await res.text()).toBe('auth-verify-token')169 })170171 it('Should not authorize - verifyToken option', async () => {172 const res = await app.request('/auth-verify-token', {173 headers: { Authorization: 'Bearer invalid-token' },174 })175 expect(res).not.toBeNull()176 expect(handlerExecuted).toBeFalsy()177 expect(res.status).toBe(401)178 })179})
modified:src/middleware/bearer-auth/index.ts+20 added-8 removed