Passport HTTP Bearer Strategy (SuperLogin Fork)

1.0.4 · abandoned · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure and use `passport-http-bearer-sl` with Express.js to protect an API endpoint using a bearer token, showing token verification and access to user data.

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');
});

view raw JSON →