Registry / auth-security / http-auth

http-auth

JSON →
library4.2.1jsnpmunverified

The `http-auth` package provides robust HTTP basic and digest access authentication capabilities for Node.js applications. Currently stable at version 4.2.1, it receives infrequent but consistent updates, addressing security and dependency concerns (e.g., uuid updates, security fixes in 4.1.3). It differentiates itself by offering built-in support for both basic and digest authentication schemes, configurable realms, and flexible user credential storage, including file-based methods (e.g., `.htpasswd` format). While primarily designed for CommonJS environments, it offers a straightforward API for integrating authentication into standard Node.js HTTP servers, allowing developers to define custom user stores via file paths or callback functions, and customize authentication parameters like algorithm (MD5, MD5-sess) and Quality of Protection (QOP) for digest authentication.

npm install http-auth
INSTALL
IMPORT
SIG · HTTP-AUTH
H
http-auth
auth-securityjavascriptv4.2.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

auth
const auth = require('http-auth');
import auth from 'http-auth';
This package is CommonJS-only. Direct ESM imports will fail without a transpilation step or a CommonJS wrapper.
basic
const basic = auth.basic({...});
import { basic } from 'http-auth';
The `basic` authentication strategy is a method on the `auth` object, not a direct export.
digest
const digest = auth.digest({...});
import { digest } from 'http-auth';
The `digest` authentication strategy is a method on the `auth` object, not a direct export.

This quickstart sets up a basic HTTP server with HTTP Basic Authentication, reading user credentials from a temporary `.htpasswd` file. It demonstrates how to initialize the `basic` authentication middleware and integrate it into a standard Node.js `http.createServer` callback.

const http = require("http"); const auth = require("http-auth"); const path = require('path'); const fs = require('fs'); // Create a dummy .htpasswd file for the example const htpasswdPath = path.join(__dirname, "users.htpasswd"); fs.writeFileSync(htpasswdPath, "gevorg:gpass\nSarah:testpass"); const basicAuth = auth.basic({ realm: "Protected Area.", file: htpasswdPath // gevorg:gpass, Sarah:testpass }); http.createServer( basicAuth.check((req, res) => { res.end(`Welcome to the private area - ${req.user} (${req.method} ${req.url})!`); }) ) .listen(1337, () => { console.log("Server running at http://127.0.0.1:1337/"); console.log("Try accessing with 'gevorg' and 'gpass' or 'Sarah' and 'testpass'."); console.log("To stop the server, press Ctrl+C. The users.htpasswd file will be removed."); }); // Clean up the dummy file on exit process.on('exit', () => { if (fs.existsSync(htpasswdPath)) { fs.unlinkSync(htpasswdPath); console.log("Cleaned up users.htpasswd"); } });
Debug
Known issues
breakingThe package is strictly CommonJS. Attempting to use `import` statements directly in a pure ESM Node.js project will result in a `TypeError: require is not defined` or similar errors. It is not designed for direct ESM consumption.
fix
Ensure your project is configured for CommonJS, or use a build tool like Webpack/Rollup that can transpile CommonJS modules for ESM environments. If using Node.js ESM, you must use `createRequire` or a dynamic import (`await import()`) if absolutely necessary, but it's generally recommended for CJS-only packages to stick to `require()` environments.
affects: >=4.0.0
breakingOlder versions of `http-auth` (prior to 4.1.3) contained unspecified security vulnerabilities. Users on these versions are at risk and should upgrade immediately.
fix
Upgrade to `http-auth` version 4.1.3 or newer to patch known security issues: `npm install http-auth@latest`.
affects: <4.1.3
gotchaThe `http-auth` package does not ship with official TypeScript declaration files (`.d.ts`). Developers using TypeScript will need to either create their own declaration files or use `@ts-ignore` directives, leading to a less type-safe development experience.
fix
Consider contributing `d.ts` files to the project, providing a `types/http-auth/index.d.ts` file in your project, or looking for community-maintained types (e.g., `@types/http-auth`, though none exist currently).
affects: >=4.0.0
gotchaUsing file-based authentication (e.g., `.htpasswd` files) for storing user credentials, especially with plaintext or basic hashes, is generally not recommended for production applications due to security risks. Without strong file system permissions and hashing algorithms, credentials can be easily compromised.
fix
For production, integrate with more secure authentication backends such as databases with strong hashing (e.g., bcrypt), external identity providers, or OAuth/OIDC systems. If file-based is unavoidable, ensure robust file system permissions and use strong hashing algorithms provided by utilities like `htpasswd` or `htdigest` with modern secure options.
affects: >=4.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use `require()` in a Node.js project configured as an ES Module (`"type": "module"` in `package.json`).
fix
Change your project's `package.json` to `"type": "commonjs"` or rename your script file to have a `.cjs` extension. If you must use ESM, consider using a dynamic import: `const auth = await import('http-auth').then(m => m.default || m);` (though this package exports directly, not a default).
TypeError: auth.basic is not a function
The `auth` object was not correctly imported or is undefined when attempting to call `auth.basic()`.
fix
Ensure that `const auth = require("http-auth");` is present and executed correctly before you try to call `auth.basic` or `auth.digest`. This often happens if the `require` statement is conditional or placed incorrectly.
Error: ENOENT: no such file or directory, open '/path/to/users.htpasswd'
The file specified in the `file` option for basic or digest authentication does not exist at the given path or the Node.js process lacks read permissions for it.
fix
Verify the `file` path is correct and absolute. Use `path.join(__dirname, 'data', 'users.htpasswd')` for relative paths within your project. Ensure the Node.js process has read permissions for the file.
Upgrade
Version history
4.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources