Connect Basic Authentication Middleware

1.1.0 · maintenance · verified Tue Apr 21

`basic-auth-connect` is a Connect/Express middleware that implements HTTP Basic Authentication, providing a straightforward way to secure web routes. It allows for user verification using either static username/password pairs or through synchronous or asynchronous callback functions for more dynamic authentication logic. The package is currently on version 1.1.0, with its most recent updates focusing primarily on security patches, notably addressing CVE-2024-47178. While functional and easy to use for common Basic Auth scenarios, the package's own documentation suggests that for more complex or highly custom authentication requirements, developers should consider using the underlying `basic-auth` package directly to build their own middleware. Its release cadence appears to be driven by critical security fixes rather than feature development, indicating it is in a maintenance status. Its key differentiator is its simplicity for direct integration into the Connect middleware stack.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic HTTP authentication for a Connect application using both static credentials and an asynchronous callback function with a timing-safe comparison.

const connect = require('connect');
const basicAuth = require('basic-auth-connect');
const http = require('http');
const crypto = require('crypto');

const app = connect();

// Simulate a database user check with timing-safe comparison
const users = {
  'tj': 'wahoo',
  'admin': 'secret'
};

function verifyUser(user, pass, done) {
  setTimeout(() => {
    const storedPass = users[user];
    if (storedPass) {
      // Crucial for security: timing-safe comparison
      const userBuffer = Buffer.from(pass);
      const storedBuffer = Buffer.from(storedPass);
      if (userBuffer.length === storedBuffer.length && crypto.timingSafeEqual(userBuffer, storedBuffer)) {
        console.log(`User '${user}' authenticated successfully.`);
        return done(null, user);
      }
    }
    console.log(`Failed authentication for user '${user}'.`);
    done(null, false); // Failed authentication
  }, 100);
}

// Basic auth with static username/password
app.use('/protected-static', basicAuth('staticuser', 'staticpass'));

// Basic auth with async callback verification
app.use('/protected-async', basicAuth(verifyUser));

app.use('/protected-static', (req, res) => {
  res.end('Accessed protected static route!');
});

app.use('/protected-async', (req, res) => {
  res.end('Accessed protected async route!');
});

app.use('/', (req, res) => {
  res.end('Welcome! Try /protected-static or /protected-async');
});

http.createServer(app).listen(3000, () => {
  console.log('Server running on http://localhost:3000');
  console.log('Try accessing http://localhost:3000/protected-static with staticuser/staticpass');
  console.log('Try accessing http://localhost:3000/protected-async with tj/wahoo');
});

view raw JSON →