Passport HTTP Bearer Strategy (SuperLogin Fork)
passport-http-bearer-sl is an HTTP Bearer authentication strategy specifically for the Passport.js middleware, forked from the original `passport-http-bearer` package. It enables Node.js applications to authenticate requests using bearer tokens, typically for protecting API endpoints and often in conjunction with OAuth 2.0. The key differentiation of this fork (version 1.0.4, last published in 2013) is the change in the expected query parameter for the token from 'access_token' to 'bearer_token'. This modification was made to prevent conflicts with reserved 'access_token' parameters used by certain OAuth providers, particularly within the context of the SuperLogin project. Due to its age and lack of recent updates (last GitHub commit in 2017), it is largely considered abandoned, with no active development or defined release cadence, making it suitable only for legacy systems or specific SuperLogin environments where this exact behavior is required. The original `passport-http-bearer` (actively maintained) or other `passport-http-custom-bearer` forks are generally preferred for new projects.
Common errors
-
TypeError: HTTPBearerStrategy requires a verify callback
cause The `BearerStrategy` constructor was called without providing the essential `verify` callback function.fixPass a function as the second argument (or first, if no options object) to the `BearerStrategy` constructor: `new BearerStrategy(function(token, done) { /* ... */ })`. -
ReferenceError: BearerStrategy is not defined
cause The `BearerStrategy` class was used without being correctly imported or required from the package.fixFor CommonJS, use `const { Strategy: BearerStrategy } = require('passport-http-bearer-sl');`. For ESM (if transpiled), use `import { Strategy as BearerStrategy } from 'passport-http-bearer-sl';`. -
Unauthorized / 401 response when providing `access_token` in URL query
cause This fork expects `bearer_token` in the query string or `Authorization` header, not `access_token`, due to its specific modification for SuperLogin.fixSend the token via the `Authorization: Bearer <token>` header or use the `bearer_token` query/body parameter instead of `access_token`.
Warnings
- breaking This fork changes the expected query parameter for the bearer token from `access_token` (used by the original `passport-http-bearer`) to `bearer_token` to avoid conflicts with OAuth providers. Code expecting `access_token` in query parameters will break.
- gotcha The package is effectively abandoned with no recent updates or maintenance since 2017. This implies potential compatibility issues with newer Node.js versions or security vulnerabilities that will not be patched.
- gotcha Like all Passport strategies, `passport-http-bearer-sl` requires a `verify` callback function. Failing to provide this callback will result in a `TypeError` during strategy initialization.
- gotcha Passport's core `session` vulnerability (CVE-2022-25896) affects Passport versions prior to 0.6.0. While this package is a strategy, its usage with an outdated Passport core could expose applications to session fixation attacks.
Install
-
npm install passport-http-bearer-sl -
yarn add passport-http-bearer-sl -
pnpm add passport-http-bearer-sl
Imports
- BearerStrategy
import BearerStrategy from 'passport-http-bearer-sl'; // or: const BearerStrategy = require('passport-http-bearer-sl');import { Strategy as BearerStrategy } from 'passport-http-bearer-sl'; // or for CommonJS: const BearerStrategy = require('passport-http-bearer-sl').Strategy; - passport.authenticate
app.get('/api/resource', passport.authenticate('bearer'), (req, res) => { /* ... */ });app.get('/api/resource', passport.authenticate('bearer', { session: false }), (req, res) => { /* ... */ }); - passport.use
passport.use('bearer-sl', new BearerStrategy(function(token, done) { /* ... */ }));passport.use(new BearerStrategy(function(token, done) { /* ... */ }));
Quickstart
const express = require('express');
const passport = require('passport');
const { Strategy: BearerStrategy } = require('passport-http-bearer-sl');
const app = express();
app.use(passport.initialize());
// Simulate a User database
const users = [{
id: 1,
username: 'testuser',
token: 'supersecrettoken123',
scope: ['read', 'write']
}];
passport.use(new BearerStrategy(
function(token, done) {
// In a real application, you would fetch the user from a database
// based on the provided bearer token.
const user = users.find(u => u.token === token);
if (!user) { return done(null, false); }
// Optional info can be passed, typically including associated scope
return done(null, user, { scope: user.scope });
}
));
app.get('/profile',
passport.authenticate('bearer', { session: false }),
function(req, res) {
// req.user contains the authenticated user
// req.authInfo contains the optional info from the strategy (e.g., scope)
res.json({
message: `Welcome, ${req.user.username}!`,
user: req.user,
authInfo: req.authInfo
});
}
);
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
console.log('Test with: curl -H "Authorization: Bearer supersecrettoken123" http://localhost:3000/profile');
console.log('Test with invalid token: curl -H "Authorization: Bearer wrongtoken" http://localhost:3000/profile');
});