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
Provenance
- Commit
7425ffb- Tree
908f421- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
274191c
3 files changed+72 added−16 removed
modified:deno_dist/middleware/bearer-auth/index.ts+20 added-8 removed
1234567898910111213141011121314151617181920212223242526272829 unmodified lines58596061626352646566676820 unmodified linesimport type { Context } from '../../context.ts'import { HTTPException } from '../../http-exception.ts'import type { MiddlewareHandler } from '../../types.ts'import { timingSafeEqual } from '../../utils/buffer.ts'
const TOKEN_STRINGS = '[A-Za-z0-9._~+/-]+=*'const PREFIX = 'Bearer'
type BearerAuthOptions =export const bearerAuth = (options: { token: string | string[] realm?: string prefix?: string hashFunction?: Function}): MiddlewareHandler => { if (!options.token) { | { token: string | string[] realm?: string prefix?: string hashFunction?: Function } | { realm?: string prefix?: string verifyToken: (token: string, c: Context) => boolean | Promise<boolean> hashFunction?: Function }
export const bearerAuth = (options: BearerAuthOptions): MiddlewareHandler => { if (!('token' in options || 'verifyToken' in options)) { throw new Error('bearer auth middleware requires options for "token"') } if (!options.realm) { options.realm = ''29 unmodified lines }) throw new HTTPException(400, { res }) } else { let equal = false if ('verifyToken' in options) { equal = await options.verifyToken(match[1], c) if (typeof options.token === 'string') { } else if (typeof options.token === 'string') { equal = await timingSafeEqual(options.token, match[1], options.hashFunction) } else if (Array.isArray(options.token) && options.token.length > 0) { for (const token of options.token) { if (await timingSafeEqual(token, match[1], options.hashFunction)) {20 unmodified linesmodified:src/middleware/bearer-auth/index.test.ts+32 added
41 unmodified lines42 app.get('/auths/*', (c) => {43 handlerExecuted = true44 return c.text('auths')45 })46+47+ 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 })60 61 it('Should authorize', async () => {62 const req = new Request('http://localhost/auth/a')93 unmodified lines156 expect(res2.status).toBe(200)157 expect(handlerExecuted).toBeTruthy()158 expect(await res2.text()).toBe('auths')159 })160+161+ 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+ })170+171+ 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
1234567898910111213141011121314151617181920212223242526272829 unmodified lines58596061626352646566676820 unmodified linesimport type { Context } from '../../context'import { HTTPException } from '../../http-exception'import type { MiddlewareHandler } from '../../types'import { timingSafeEqual } from '../../utils/buffer'
const TOKEN_STRINGS = '[A-Za-z0-9._~+/-]+=*'const PREFIX = 'Bearer'
type BearerAuthOptions =export const bearerAuth = (options: { token: string | string[] realm?: string prefix?: string hashFunction?: Function}): MiddlewareHandler => { if (!options.token) { | { token: string | string[] realm?: string prefix?: string hashFunction?: Function } | { realm?: string prefix?: string verifyToken: (token: string, c: Context) => boolean | Promise<boolean> hashFunction?: Function }
export const bearerAuth = (options: BearerAuthOptions): MiddlewareHandler => { if (!('token' in options || 'verifyToken' in options)) { throw new Error('bearer auth middleware requires options for "token"') } if (!options.realm) { options.realm = ''29 unmodified lines }) throw new HTTPException(400, { res }) } else { let equal = false if ('verifyToken' in options) { equal = await options.verifyToken(match[1], c) if (typeof options.token === 'string') { } else if (typeof options.token === 'string') { equal = await timingSafeEqual(options.token, match[1], options.hashFunction) } else if (Array.isArray(options.token) && options.token.length > 0) { for (const token of options.token) { if (await timingSafeEqual(token, match[1], options.hashFunction)) {20 unmodified lines