Digest Authentication Header Generator

0.4.0 · active · verified Wed Apr 22

Indigestion is a focused Node.js library designed to generate HTTP Digest Authentication strings. It specifically takes the `WWW-Authenticate` header from a 401 response and provides the `Authorization` header content. The current stable version is v0.4.0, with releases appearing to be on-demand for maintenance or minor feature additions rather than a strict schedule, as indicated by the most recent release in February 2026. This library differentiates itself from other digest authentication solutions (like `axios-digest` or `node-digest-auth-client`) by offering only the header generation functionality, allowing developers full control over the HTTP request client and lifecycle. It aims to fill a niche where only the calculated `Digest` header string is needed, without the library dictating how the actual HTTP request is made.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a typical Digest Authentication flow using `axios` and `indigestion`. It first attempts a request, catches a 401 response, extracts the `WWW-Authenticate` header, generates the `Authorization` header using `generateDigestAuth`, and then retries the request with the generated header.

import axios from "axios";
import { generateDigestAuth } from 'indigestion';

async function makeDigestAuthenticatedRequest() {
  const username = process.env.DIGEST_USERNAME ?? 'testuser';
  const password = process.env.DIGEST_PASSWORD ?? 'testpass';
  const targetUrl = 'http://www.example.com/protected/resource'; // Replace with your target URL
  const method = 'GET';

  try {
    // First attempt: Expect a 401 Unauthorized response
    await axios.get(targetUrl);
  } catch (error: any) {
    if (error.response && error.response.status === 401) {
      const authenticateHeader = error.response.headers['www-authenticate'];
      if (!authenticateHeader) {
        throw new Error('WWW-Authenticate header not found in 401 response.');
      }

      // Generate the Digest Authorization header
      const authorizationHeader = generateDigestAuth({
        authenticateHeader,
        username,
        password,
        uri: new URL(targetUrl).pathname,
        method
      });

      // Retry the request with the generated Authorization header
      const result = await axios.get(targetUrl, {
        headers: {
          Authorization: authorizationHeader
        }
      });
      console.log('Successfully made authenticated request:', result.data);
      return result.data;
    } else {
      throw error; // Re-throw other errors
    }
  }
}

makeDigestAuthenticatedRequest().catch(console.error);

view raw JSON →