Passport HTTP Basic & Digest Strategies

0.3.0 · abandoned · verified Tue Apr 21

This package provides authentication strategies for HTTP Basic and HTTP Digest schemes, designed to integrate with the Passport.js authentication middleware for Node.js. It allows applications to secure endpoints using standard HTTP authentication headers, often used for API access or intranet applications. The current stable version is 0.3.0, last published nine years ago. This package is part of the original Passport ecosystem and differentiates itself by offering direct implementations of these fundamental HTTP authentication methods, enabling their use with any Connect/Express-style middleware. Its release cadence is non-existent, suggesting a mature but abandoned state, with focus on core functionality without frequent updates. While functional, developers should consider its age and lack of recent security patches.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates configuring and using both HTTP Basic and HTTP Digest authentication strategies with Passport.js and Express, showcasing how to protect routes without requiring session management.

import express from 'express';
import passport from 'passport';
import { BasicStrategy, DigestStrategy } from 'passport-http';

const app = express();
const PORT = process.env.PORT || 3000;

// A mock user database for demonstration
const users = [
  { id: 1, username: 'john', password: 'password', secret: 'shared-secret' },
  { id: 2, username: 'jane', password: 'secure', secret: 'another-secret' }
];

// Basic Strategy Configuration
passport.use(new BasicStrategy(
  function(userid, password, done) {
    const user = users.find(u => u.username === userid);
    if (!user) { return done(null, false); }
    if (user.password !== password) { return done(null, false); }
    return done(null, user);
  }
));

// Digest Strategy Configuration
passport.use(new DigestStrategy({ qop: 'auth' },
  function(username, done) {
    const user = users.find(u => u.username === username);
    if (!user) { return done(null, false); }
    // For Digest, 'done' needs to provide the user and the shared secret (password)
    return done(null, user, user.secret);
  },
  function(params, done) {
    // Optional: Validate nonce and other parameters to prevent replay attacks
    // For simplicity, we just accept for this example.
    done(null, true);
  }
));

app.use(passport.initialize());

// Routes for HTTP Basic Authentication
app.get('/basic-private', 
  passport.authenticate('basic', { session: false }),
  function(req, res) {
    res.json({ message: 'Welcome to the basic private area!', user: req.user.username });
  }
);

// Routes for HTTP Digest Authentication
app.get('/digest-private', 
  passport.authenticate('digest', { session: false }),
  function(req, res) {
    res.json({ message: 'Welcome to the digest private area!', user: req.user.username });
  }
);

app.get('/', (req, res) => {
  res.send('Hello! Try accessing /basic-private or /digest-private with auth.');
});

app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
  console.log('Test Basic Auth with: curl -u john:password http://localhost:3000/basic-private');
  console.log('Test Digest Auth with: curl --digest -u jane:another-secret http://localhost:3000/digest-private');
});

view raw JSON →