Skip to content

Commit

Permalink
remove: dependency on HonoRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
sugar-cat7 committed Sep 2, 2024
1 parent 24bd2dd commit 989a0db
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/middleware/basic-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const basicAuth = (
}

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)) {
Expand Down
18 changes: 6 additions & 12 deletions src/utils/basic-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,55 @@ import { auth } from './basic-auth'

describe('auth', () => {
it('auth() - not include Authorization Header', () => {
const req = new HonoRequest(new Request('http://localhost/auth'))
const res = auth(req)
const res = auth(new Request('http://localhost/auth'))
expect(res).toBeUndefined()
})

it('auth() - invalid Authorization Header format', () => {
const req = new HonoRequest(
const res = auth(
new Request('http://localhost/auth', {
headers: { Authorization: 'InvalidAuthHeader' },
})
)
const res = auth(req)
expect(res).toBeUndefined()
})

it('auth() - invalid Base64 string in Authorization Header', () => {
const req = new HonoRequest(
const res = auth(
new Request('http://localhost/auth', {
headers: { Authorization: 'Basic InvalidBase64' },
})
)
const res = auth(req)
expect(res).toBeUndefined()
})

it('auth() - valid Authorization Header', () => {
const validBase64 = btoa('username:password')
const req = new HonoRequest(
const res = auth(
new Request('http://localhost/auth', {
headers: { Authorization: `Basic ${validBase64}` },
})
)
const res = auth(req)
expect(res).toEqual({ username: 'username', password: 'password' })
})

it('auth() - empty username', () => {
const validBase64 = btoa(':password')
const req = new HonoRequest(
const res = auth(
new Request('http://localhost/auth', {
headers: { Authorization: `Basic ${validBase64}` },
})
)
const res = auth(req)
expect(res).toEqual({ username: '', password: 'password' })
})

it('auth() - empty password', () => {
const validBase64 = btoa('username:')
const req = new HonoRequest(
const res = auth(
new Request('http://localhost/auth', {
headers: { Authorization: `Basic ${validBase64}` },
})
)
const res = auth(req)
expect(res).toEqual({ username: 'username', password: '' })
})
})
7 changes: 3 additions & 4 deletions src/utils/basic-auth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type { HonoRequest } from '../request'
import { 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: HonoRequest) => { username: string; password: string } | undefined
export type Auth = (req: Request) => { username: string; password: string } | undefined

export const auth: Auth = (req: HonoRequest) => {
const match = CREDENTIALS_REGEXP.exec(req.header('Authorization') || '')
export const auth: Auth = (req: Request) => {
const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization') || '')
if (!match) {
return undefined
}
Expand Down

0 comments on commit 989a0db

Please sign in to comment.