The `passport-http-header-strategy` package provides a flexible HTTP header-based authentication strategy for the Passport.js framework. It enables developers to authenticate requests by extracting a token from a custom HTTP header (e.g., `X-API-Key`), or optionally from a request body or query parameter, rather than being limited to the standard `Authorization: Bearer` scheme. Currently at version 1.1.0, the package has not seen significant updates in several years, suggesting a stable but largely unmaintained status. It integrates seamlessly into existing Passport-based applications, allowing for stateless (session-less) authentication, which is common for API-driven services. Its key differentiator is the configurability of the header name and parameter name, offering more customization than some other token-based strategies.
npm install passport-http-header-strategyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up an Express application with Passport.js and the `passport-http-header-strategy` to protect a route using a custom `X-APP-TOKEN` header. It includes a mock user database and instructions for testing via cURL.
Always include `{ session: false }` in the `passport.authenticate()` options for token strategies.Ensure your `verify` callback signature matches `(req, token, done)` if `passReqToCallback: true`, or `(token, done)` if `passReqToCallback: false`.
Thoroughly test with your target Node.js and Passport.js versions. Consider migrating to a more actively maintained strategy or implementing a custom strategy if advanced features or recent updates are critical.
Implement proper token security measures: use strong cryptographic hashing (e.g., bcrypt) for API keys/static tokens, or verify JWTs using their signature and claims rather than simple database lookups.
Ensure you call `passport.use('your-strategy-name', new Strategy(...))` and then use `passport.authenticate('your-strategy-name', ...)` with the exact same string name.Provide a valid function as the second argument to `new Strategy(options, verifyCallback)`. This function is where you implement your token validation logic.
Ensure you have `app.use(passport.initialize());` (and optionally `app.use(passport.session());` if using sessions, though not recommended for this strategy) before any routes that use `passport.authenticate()`.
Carefully debug your `verify` callback. Ensure `done(null, user)` is called with a valid user object upon successful authentication. Check that the `header` or `param` options correctly match where your token is sent.