Passport HTTP Header Token

1.1.0 · abandoned · verified Wed Apr 22

Passport HTTP Header Token is a Node.js authentication strategy for the Passport.js middleware, designed to authenticate users based on a raw token provided directly in an HTTP header. This strategy, currently at version 1.1.0, was last published in 2016 and has not received updates since, indicating it is an abandoned package. Its simple design requires a `verify` callback to validate the submitted token against a user store. Unlike the more commonly used `passport-http-bearer` strategy, `passport-http-header-token` expects a raw token value in the header rather than parsing a 'Bearer <token>' format, which can lead to confusion if standard RFC 6750 bearer tokens are expected. Due to its unmaintained status, developers should carefully consider potential security implications and evaluate more actively supported alternatives like `passport-http-bearer` or `passport-jwt` for modern applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up and use `passport-http-header-token` in an Express application to authenticate requests using a token provided in the 'Authorization' header.

const express = require('express');
const passport = require('passport');
const HTTPHeaderTokenStrategy = require('passport-http-header-token').Strategy;

const app = express();

// Mock User database for demonstration
const users = [{
  id: 1,
  username: 'testuser',
  token: 'mysecrettoken123'
}];

passport.use(new HTTPHeaderTokenStrategy(
  function(token, done) {
    // In a real application, you would query your database here
    // for a user associated with the provided token.
    console.log(`Attempting to authenticate with token: ${token}`);
    const user = users.find(u => u.token === token);

    if (!user) {
      return done(null, false, { message: 'Incorrect token.' });
    }
    return done(null, user);
  }
));

app.use(passport.initialize());

app.get('/api/protected', 
  passport.authenticate('http-header-token', { session: false, failureMessage: true }),
  function(req, res) {
    res.json({ message: `Access granted, user: ${req.user.username}` });
  }
);

app.get('/', (req, res) => {
  res.send('Welcome! Try GET /api/protected with an Authorization header like: Authorization: mysecrettoken123');
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
  console.log('Test with: curl -H "Authorization: mysecrettoken123" http://localhost:3000/api/protected');
  console.log('Test failure with: curl -H "Authorization: wrongtoken" http://localhost:3000/api/protected');
});

view raw JSON →