Skip to content

Commit

Permalink
Merge pull request #3 from KilianB/main
Browse files Browse the repository at this point in the history
Fixes #1 addError is undefined when added as middleware to Elysia 0.8.9
  • Loading branch information
eelkevdbos committed Feb 6, 2024
2 parents 7887e1a + 590e2ff commit 119f332
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
},
"peerDependencies": {
"typescript": "^5.0.0",
"elysia": "^0.8.1"
"elysia": "^0.8.16"
}
}
58 changes: 31 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export type BasicAuthOptions = {
}

class BasicAuthError extends Error {
public code = 'BASIC_AUTH_ERROR'

constructor(
readonly message: string,
readonly realm: string
Expand Down Expand Up @@ -183,33 +185,35 @@ export function basicAuth(userOptions: Partial<BasicAuthOptions> = {}) {
const skipRequest = (request: Request) =>
options.skipCorsPreflight && isCORSPreflightRequest(request)

return (app: Elysia) =>
app
.state('basicAuthRealm', null as string | null)
.state('basicAuthUser', null as string | null)
.addError({ BASIC_AUTH_ERROR: BasicAuthError })
.onError(({ code, error }) => {
if (code === 'BASIC_AUTH_ERROR' && error.realm === options.realm) {
return new Response(options.unauthorizedMessage, {
status: options.unauthorizedStatus,
headers: { 'WWW-Authenticate': `Basic realm="${options.realm}"` },
})
return new Elysia({
name: 'elysia-basic-auth',
seed: options,
})
.state('basicAuthRealm', null as string | null)
.state('basicAuthUser', null as string | null)
.error({ BASIC_AUTH_ERROR: BasicAuthError })
.onError(({ code, error }) => {
if (code === 'BASIC_AUTH_ERROR' && error.realm === options.realm) {
return new Response(options.unauthorizedMessage, {
status: options.unauthorizedStatus,
headers: { 'WWW-Authenticate': `Basic realm="${options.realm}"` },
})
}
})
.onRequest(ctx => {
if (options.enabled && inScope(ctx) && !skipRequest(ctx.request)) {
const authHeader = ctx.request.headers.get(options.header)
if (!authHeader || !authHeader.toLowerCase().startsWith('basic ')) {
throw new BasicAuthError('Invalid header', options.realm)
}
})
.onRequest(ctx => {
if (options.enabled && inScope(ctx) && !skipRequest(ctx.request)) {
const authHeader = ctx.request.headers.get(options.header)
if (!authHeader || !authHeader.toLowerCase().startsWith('basic ')) {
throw new BasicAuthError('Invalid header', options.realm)
}

const credentials = getCredentials(authHeader)
if (!checkCredentials(credentials, credentialsMap)) {
throw new BasicAuthError('Invalid credentials', options.realm)
}

ctx.store.basicAuthRealm = options.realm
ctx.store.basicAuthUser = credentials.username

const credentials = getCredentials(authHeader)
if (!checkCredentials(credentials, credentialsMap)) {
throw new BasicAuthError('Invalid credentials', options.realm)
}
})

ctx.store.basicAuthRealm = options.realm
ctx.store.basicAuthUser = credentials.username
}
})
}

0 comments on commit 119f332

Please sign in to comment.