From 3ba73862283b2c1e859782f67a9b77326b6659e6 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Wed, 15 May 2024 21:50:13 +0900 Subject: [PATCH] Basic Authentication Middleware --- deno_dist/middleware/basic-auth/index.ts | 31 ++++++++++++++++++++++++ src/middleware/basic-auth/index.ts | 31 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/deno_dist/middleware/basic-auth/index.ts b/deno_dist/middleware/basic-auth/index.ts index cd05874d2a..2edae25dcb 100644 --- a/deno_dist/middleware/basic-auth/index.ts +++ b/deno_dist/middleware/basic-auth/index.ts @@ -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 }[] diff --git a/src/middleware/basic-auth/index.ts b/src/middleware/basic-auth/index.ts index a830219c18..94d07e50ea 100644 --- a/src/middleware/basic-auth/index.ts +++ b/src/middleware/basic-auth/index.ts @@ -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 }[]