Passport.js Strategy for HTTP Basic/Digest Authentication via http-auth

1.0.7 · maintenance · verified Wed Apr 22

http-auth-passport provides an integration layer that allows the use of the `http-auth` module's HTTP Basic and Digest access authentication within the Passport.js framework. This package enables developers to easily implement traditional HTTP authentication schemes in their Node.js applications, particularly those built with Express.js, leveraging Passport's robust strategy pattern. The current stable version is 1.0.7, with its last known release in 2021. The package itself has received minimal updates since then, indicating a slow maintenance cadence primarily focused on critical bug fixes or essential dependency alignments rather than active feature development. It serves a niche by bridging `http-auth`'s specific capabilities with the broader Passport ecosystem, offering an alternative to direct implementations like `passport-http` when `http-auth`'s features are preferred.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up an Express server that utilizes `http-auth-passport` to protect a route with HTTP Basic Authentication. It demonstrates defining a basic authentication realm using a `.htpasswd` file, integrating this realm as a Passport strategy, and securing an endpoint without maintaining user sessions. The example includes the creation of a temporary `.htpasswd` file for immediate testing.

const express = require('express');
const auth = require('http-auth');
const authPassport = require('http-auth-passport');
const passport = require('passport');
const fs = require('fs');

// Create a dummy .htpasswd file for demonstration
const htpasswdContent = 'testuser:testpass\nadmin:securepass';
const htpasswdPath = `${__dirname}/users.htpasswd`;
fs.writeFileSync(htpasswdPath, htpasswdContent);

const basic = auth.basic({
  realm: 'Secure Area',
  file: htpasswdPath // Path to your .htpasswd file
});

passport.use(authPassport(basic));

const app = express();

app.get('/', passport.authenticate('http', { session: false }), (req, res) => {
  res.end(`Welcome, ${req.user}! You are authenticated with HTTP Basic.`);
});

app.listen(1337, () => {
  console.log('Server running at http://127.0.0.1:1337/');
  console.log('Try accessing http://127.0.0.1:1337/ with username: testuser, password: testpass');
  console.log('Or with username: admin, password: securepass');
});

view raw JSON →