Skip to content

Commit

Permalink
Basic Authentication Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe committed May 15, 2024
1 parent e709821 commit 3ba7386
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
31 changes: 31 additions & 0 deletions deno_dist/middleware/basic-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ type BasicAuthOptions =
hashFunction?: Function
}

/**
* Basic authentication middleware for Hono.
*
* @see {@link https://hono.dev/middleware/builtin/basic-auth}
*
* @param {BasicAuthOptions} options - The options for the basic authentication middleware.
* @param {string} [options.username] - The username for authentication.
* @param {string} [options.password] - The password for authentication.
* @param {string} [options.realm="Secure Area"] - The realm attribute for the WWW-Authenticate header.
* @param {Function} [options.hashFunction=] - The hash function used for secure comparison.
* @param {Function} [options.verifyUser=] - The function to verify user credentials.
* @returns {MiddlewareHandler} - The middleware handler function.
* @throws {HTTPException} If neither "username and password" nor "verifyUser" options are provided.
*
* @example
* ```ts
* const app = new Hono()
*
* app.use(
* '/auth/*',
* basicAuth({
* username: 'hono',
* password: 'acoolproject',
* })
* )
*
* app.get('/auth/page', (c) => {
* return c.text('You are authorized')
* })
* ```
*/
export const basicAuth = (
options: BasicAuthOptions,
...users: { username: string; password: string }[]
Expand Down
31 changes: 31 additions & 0 deletions src/middleware/basic-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ type BasicAuthOptions =
hashFunction?: Function
}

/**
* Basic authentication middleware for Hono.
*
* @see {@link https://hono.dev/middleware/builtin/basic-auth}
*
* @param {BasicAuthOptions} options - The options for the basic authentication middleware.
* @param {string} [options.username] - The username for authentication.
* @param {string} [options.password] - The password for authentication.
* @param {string} [options.realm="Secure Area"] - The realm attribute for the WWW-Authenticate header.
* @param {Function} [options.hashFunction=] - The hash function used for secure comparison.
* @param {Function} [options.verifyUser=] - The function to verify user credentials.
* @returns {MiddlewareHandler} - The middleware handler function.
* @throws {HTTPException} If neither "username and password" nor "verifyUser" options are provided.
*
* @example
* ```ts
* const app = new Hono()
*
* app.use(
* '/auth/*',
* basicAuth({
* username: 'hono',
* password: 'acoolproject',
* })
* )
*
* app.get('/auth/page', (c) => {
* return c.text('You are authorized')
* })
* ```
*/
export const basicAuth = (
options: BasicAuthOptions,
...users: { username: string; password: string }[]
Expand Down

0 comments on commit 3ba7386

Please sign in to comment.