The `passport-http-bearer` module provides an authentication strategy for Passport.js, specifically designed to handle HTTP Bearer tokens as defined by RFC 6750. This module allows Node.js applications, particularly those using Connect-style middleware like Express, to easily integrate token-based authentication for API endpoints. Bearer tokens are a common mechanism for securing REST APIs and are frequently issued in conjunction with OAuth 2.0. The current stable version is 1.0.1, last published in 2013, indicating a mature and stable codebase with a very low release cadence, focusing on reliability rather than frequent feature additions. It differentiates itself by providing a robust, battle-tested solution for a core authentication pattern within the Passport.js framework, leveraging its pluggable middleware architecture. TypeScript definitions are available via `@types/passport-http-bearer` for enhanced developer experience.
npm install passport-http-bearerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `passport-http-bearer` with Express and Passport to protect an API endpoint using a static bearer token. It includes a simple in-memory user store for token verification.
Ensure your application always uses HTTPS/TLS. Implement secure storage practices for tokens if they are persistent (e.g., client-side storage, secure databases).
Always include `{ session: false }` in the options object for `passport.authenticate('bearer', { session: false })` when authenticating API requests.If your verify callback expects `(req, token, done)`, you *must* initialize the strategy with `new BearerStrategy({ passReqToCallback: true }, verifyCallback)`. If it expects `(token, done)`, ensure `passReqToCallback` is `false` (default) or omitted.Install `@types/passport-http-bearer` and `@types/passport`. Explicitly import `VerifyFunction` or `VerifyFunctionWithRequest` from `passport-http-bearer` to correctly type your callback parameters (e.g., `async (token: string, done: VerifyCallback) => { ... }` or `async (req: Request, token: string, done: VerifyCallback) => { ... }`).Ensure the `BearerStrategy` constructor receives a valid function as its second argument (or first, if no options object is provided). Example: `new BearerStrategy(function(token, done) { /* ... */ })`.Verify the client is sending the token correctly in the `Authorization: Bearer <token>` header or as `access_token` in query/body. Debug the `verify` callback to ensure it correctly finds and validates the token against your user/token store. Custom error messages can be returned via `done(null, false, { message: 'Custom message' })`.Ensure `passport.authenticate('bearer', { session: false })` is correctly placed as middleware in your route. If you are handling authentication outcome manually, ensure `passport.authenticate('bearer', function(err, user, info) { /* ... */ })(req, res, next)` uses a valid callback and immediately invokes it with `(req, res, next)`.