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 (or the custom header named
by config.apiKeyHeader).
config.apiKeyHeader
Custom header name to use for API key lookup instead of the default
X-Api-Key. Lookup is case-insensitive.
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.
Create an Express/Connect-compatible middleware adapter.
Verify a username/password pair with constant-time comparison.
Verify a Bearer token with constant-time comparison.
Verify an API key with constant-time comparison.
Get metadata describing the enabled auth methods.
Check whether Bearer token authentication is configured.
Check whether API key authentication is configured.
Check whether Basic authentication is configured.