refactor(utils/basic-auth): Moved Internal function to utils (#3359)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* refactor(utils/basic-auth): Split the code into utils * remove: dependency on HonoRequest
authored by sugar · committed by GitHub · parent fa19540 · Browse files · 3 files·+85 added−25 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+85 added−25 removed
- tests:+57 added−0 removed
- other:+28 added−25 removed
Provenance
- Commit
1db161e- Tree
214defb- Author
sugar <69241625+sugar-cat7@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
fa19540
3 files changed+85 added−25 removed
modified:src/middleware/basic-auth/index.ts+2 added-25 removed
3 unmodified lines4567889101111131415161718192021222324252627282930313233341213141558 unmodified lines74757677101787980818224 unmodified lines3 unmodified lines */
import type { Context } from '../../context'import { HTTPException } from '../../http-exception'import type { HonoRequest } from '../../request'import type { MiddlewareHandler } from '../../types'import { auth } from '../../utils/basic-auth'import { timingSafeEqual } from '../../utils/buffer'import { decodeBase64 } from '../../utils/encode'
const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/const USER_PASS_REGEXP = /^([^:]*):(.*)$/const utf8Decoder = new TextDecoder()const auth = (req: HonoRequest) => { const match = CREDENTIALS_REGEXP.exec(req.header('Authorization') || '') if (!match) { return undefined }
let userPass = undefined // If an invalid string is passed to atob(), it throws a `DOMException`. try { userPass = USER_PASS_REGEXP.exec(utf8Decoder.decode(decodeBase64(match[1]))) } catch {} // Do nothing
if (!userPass) { return undefined }
return { username: userPass[1], password: userPass[2] }}
type BasicAuthOptions = | { username: string password: string58 unmodified lines users.unshift({ username: options.username, password: options.password }) }
return async function basicAuth(ctx, next) { const requestUser = auth(ctx.req) const requestUser = auth(ctx.req.raw) if (requestUser) { if (verifyUserInOptions) { if (await options.verifyUser(requestUser.username, requestUser.password, ctx)) { await next()24 unmodified linesadded:src/utils/basic-auth.test.ts+57 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import { HonoRequest } from '../request'import { auth } from './basic-auth'
describe('auth', () => { it('auth() - not include Authorization Header', () => { const res = auth(new Request('http://localhost/auth')) expect(res).toBeUndefined() })
it('auth() - invalid Authorization Header format', () => { const res = auth( new Request('http://localhost/auth', { headers: { Authorization: 'InvalidAuthHeader' }, }) ) expect(res).toBeUndefined() })
it('auth() - invalid Base64 string in Authorization Header', () => { const res = auth( new Request('http://localhost/auth', { headers: { Authorization: 'Basic InvalidBase64' }, }) ) expect(res).toBeUndefined() })
it('auth() - valid Authorization Header', () => { const validBase64 = btoa('username:password') const res = auth( new Request('http://localhost/auth', { headers: { Authorization: `Basic ${validBase64}` }, }) ) expect(res).toEqual({ username: 'username', password: 'password' }) })
it('auth() - empty username', () => { const validBase64 = btoa(':password') const res = auth( new Request('http://localhost/auth', { headers: { Authorization: `Basic ${validBase64}` }, }) ) expect(res).toEqual({ username: '', password: 'password' }) })
it('auth() - empty password', () => { const validBase64 = btoa('username:') const res = auth( new Request('http://localhost/auth', { headers: { Authorization: `Basic ${validBase64}` }, }) ) expect(res).toEqual({ username: 'username', password: '' }) })})added:src/utils/basic-auth.ts+26 added
1234567891011121314151617181920212223242526import { decodeBase64 } from './encode'
const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/const USER_PASS_REGEXP = /^([^:]*):(.*)$/const utf8Decoder = new TextDecoder()
export type Auth = (req: Request) => { username: string; password: string } | undefined
export const auth: Auth = (req: Request) => { const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization') || '') if (!match) { return undefined }
let userPass = undefined // If an invalid string is passed to atob(), it throws a `DOMException`. try { userPass = USER_PASS_REGEXP.exec(utf8Decoder.decode(decodeBase64(match[1]))) } catch {} // Do nothing
if (!userPass) { return undefined }
return { username: userPass[1], password: userPass[2] }}