HTTP Basic Authentication header parser for Node.js applications
npx @tessl/cli install tessl/npm-basic-auth@1.1.0Basic Auth provides HTTP Basic Authentication header parsing for Node.js applications. It implements the RFC 7617 Basic Authentication scheme with two primary functions: parsing Authorization headers from HTTP request objects and parsing basic auth strings directly.
npm install basic-authvar auth = require('basic-auth');For ES modules:
import auth from 'basic-auth';var auth = require('basic-auth');
var http = require('http');
// Parse from HTTP request
var server = http.createServer(function (req, res) {
var credentials = auth(req);
if (!credentials) {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="example"');
res.end('Access denied');
return;
}
console.log('User:', credentials.name);
console.log('Pass:', credentials.pass);
res.end('Access granted');
});
// Parse from header string directly
var headerString = 'Basic Zm9vOmJhcg=='; // "foo:bar" encoded
var user = auth.parse(headerString);
console.log(user); // { name: 'foo', pass: 'bar' }Parses the Authorization header field from HTTP request objects, supporting both Node.js HTTP requests and Koa context objects.
/**
* Parse the Authorization header field of a request.
* @param {object} req - HTTP request object or Koa context object
* @returns {object|undefined} Object with .name and .pass properties, or undefined if parsing fails
* @throws {TypeError} When req is missing, not an object, or lacks headers property
*/
function auth(req);The function accepts:
headers.authorization)req.headers.authorization)Returns an object with:
name (string): Username from basic auth credentialspass (string): Password from basic auth credentialsReturns undefined for:
Throws TypeError for:
req argumentreq argumentreq.headers propertyParses basic auth authorization header strings directly, useful for custom header processing or non-standard authorization sources.
/**
* Parse basic auth authorization header string.
* @param {string} string - Authorization header string (e.g., "Basic Zm9vOmJhcg==")
* @returns {object|undefined} Object with .name and .pass properties, or undefined if string is invalid
*/
auth.parse = function parse(string);The function accepts authorization header strings in the format:
"Basic <base64-encoded-credentials>"username:password credentialsReturns the same credential object format as the main auth() function, or undefined for invalid inputs.
/**
* Credentials object returned by both auth() and auth.parse()
*/
interface Credentials {
/** Username from basic auth credentials */
name: string;
/** Password from basic auth credentials */
pass: string;
}The library follows these error handling patterns:
TypeError with descriptive messagesundefined (never throws for malformed headers)undefined (normal case, not an error)Common error messages:
"argument req is required" - Missing req parameter"argument req is required to be an object" - Invalid req type"argument req is required to have headers property" - Missing/invalid headers