AuthHandler
AuthHandler provides a unified authentication layer supporting Bearer tokens,
API keys, and HTTP Basic Auth. All credential comparisons use constant-time
algorithms to prevent timing attacks. It can be used as Hono middleware or as a
standalone request validator.
Constructor
config
Authentication configuration object with the following optional fields:
config.bearerToken
Bearer token matched against the Authorization: Bearer <token> header.
config.apiKey
API key matched against the X-Api-Key header.
config.basicAuth
Basic auth credentials as a [username, password] tuple.
config.customValidator
Custom validator function. Return true to allow the request.
config.allowUnauthenticated
When explicitly set to false, deny requests if no auth methods are configured.
By default, unauthenticated access is allowed when no methods are set.
Methods
Validate request headers against all configured auth methods.
Create a Hono-compatible middleware that rejects unauthorized requests.
Check whether Bearer token authentication is configured.
Check whether API key authentication is configured.
Check whether Basic authentication is configured.