Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
91 lines
4.5 KiB
Plaintext
91 lines
4.5 KiB
Plaintext
import { Env, MiddlewareHandler } from "../../types.js";
|
|
import { Context } from "../../context.js";
|
|
//#region src/middleware/bearer-auth/index.d.ts
|
|
type MessageFunction = (c: Context) => string | object | Promise<string | object>;
|
|
type CustomizedErrorResponseOptions = {
|
|
wwwAuthenticateHeader?: string | object | MessageFunction;
|
|
message?: string | object | MessageFunction;
|
|
};
|
|
type BearerAuthOptions<E extends Env = Env> = {
|
|
token: string | string[];
|
|
realm?: string;
|
|
prefix?: string;
|
|
headerName?: string;
|
|
hashFunction?: Function;
|
|
/**
|
|
* @deprecated Use noAuthenticationHeader.message instead
|
|
*/
|
|
noAuthenticationHeaderMessage?: string | object | MessageFunction;
|
|
noAuthenticationHeader?: CustomizedErrorResponseOptions;
|
|
/**
|
|
* @deprecated Use invalidAuthenticationHeader.message instead
|
|
*/
|
|
invalidAuthenticationHeaderMessage?: string | object | MessageFunction;
|
|
invalidAuthenticationHeader?: CustomizedErrorResponseOptions;
|
|
/**
|
|
* @deprecated Use invalidToken.message instead
|
|
*/
|
|
invalidTokenMessage?: string | object | MessageFunction;
|
|
invalidToken?: CustomizedErrorResponseOptions;
|
|
} | {
|
|
realm?: string;
|
|
prefix?: string;
|
|
headerName?: string;
|
|
verifyToken: (token: string, c: Context<E>) => boolean | Promise<boolean>;
|
|
hashFunction?: Function;
|
|
/**
|
|
* @deprecated Use noAuthenticationHeader.message instead
|
|
*/
|
|
noAuthenticationHeaderMessage?: string | object | MessageFunction;
|
|
noAuthenticationHeader?: CustomizedErrorResponseOptions;
|
|
/**
|
|
* @deprecated Use invalidAuthenticationHeader.message instead
|
|
*/
|
|
invalidAuthenticationHeaderMessage?: string | object | MessageFunction;
|
|
invalidAuthenticationHeader?: CustomizedErrorResponseOptions;
|
|
/**
|
|
* @deprecated Use invalidToken.message instead
|
|
*/
|
|
invalidTokenMessage?: string | object | MessageFunction;
|
|
invalidToken?: CustomizedErrorResponseOptions;
|
|
};
|
|
/**
|
|
* Bearer Auth Middleware for Hono.
|
|
*
|
|
* @see {@link https://hono.dev/docs/middleware/builtin/bearer-auth}
|
|
*
|
|
* @template E - The environment type.
|
|
* @param {BearerAuthOptions} options - The options for the bearer authentication middleware.
|
|
* @param {string | string[]} [options.token] - The string or array of strings to validate the incoming bearer token against.
|
|
* @param {Function} [options.verifyToken] - The function to verify the token.
|
|
* @param {string} [options.realm=""] - The domain name of the realm, as part of the returned WWW-Authenticate challenge header.
|
|
* @param {string} [options.prefix="Bearer"] - The prefix (or known as `schema`) for the Authorization header value. If set to the empty string, no prefix is expected.
|
|
* @param {string} [options.headerName=Authorization] - The header name.
|
|
* @param {Function} [options.hashFunction] - A function to handle hashing for safe comparison of authentication tokens.
|
|
* @param {string | object | MessageFunction} [options.noAuthenticationHeader.message="Unauthorized"] - The no authentication header message.
|
|
* @param {string | object | MessageFunction} [options.noAuthenticationHeader.wwwAuthenticateHeader="Bearer realm=\"\""] - The response header value for the WWW-Authenticate header when no authentication header is provided.
|
|
* @param {string | object | MessageFunction} [options.invalidAuthenticationHeader.message="Bad Request"] - The invalid authentication header message.
|
|
* @param {string | object | MessageFunction} [options.invalidAuthenticationHeader.wwwAuthenticateHeader="Bearer error=\"invalid_request\""] - The response header value for the WWW-Authenticate header when authentication header is invalid.
|
|
* @param {string | object | MessageFunction} [options.invalidToken.message="Unauthorized"] - The invalid token message.
|
|
* @param {string | object | MessageFunction} [options.invalidToken.wwwAuthenticateHeader="Bearer error=\"invalid_token\""] - The response header value for the WWW-Authenticate header when token is invalid.
|
|
* @returns {MiddlewareHandler<E>} The middleware handler function.
|
|
* @throws {Error} If neither "token" nor "verifyToken" options are provided.
|
|
* @throws {HTTPException} If authentication fails, with 401 status code for missing or invalid token, or 400 status code for invalid request.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const app = new Hono()
|
|
*
|
|
* const token = 'honoishot'
|
|
*
|
|
* app.use('/api/*', bearerAuth({ token }))
|
|
*
|
|
* app.get('/api/page', (c) => {
|
|
* return c.json({ message: 'You are authorized' })
|
|
* })
|
|
* ```
|
|
*/
|
|
export declare const bearerAuth: <E extends Env = Env>(options: BearerAuthOptions<E>) => MiddlewareHandler<E>;
|
|
//#endregion
|
|
export {};
|