A complete HTTP Digest Authentication server implementation in Node.js with support for RFC 2617 digest authentication.
# Clone or download the project
npm init -y # Initialize package.json if needednode server.jsThe server will start on port 4444 and display:
Server is running on http://localhost:4444
Test users: admin/password, user/secret
Using nonce: nonce
# Test with admin user
curl -v --digest -u admin:password http://localhost:4444
# Test with user account
curl -v --digest -u user:secret http://localhost:4444
# Test with wrong credentials (should fail)
curl -v --digest -u admin:wrongpass http://localhost:4444Successful Authentication:
HTTP/1.1 200 OK
Authorized - Welcome admin!
Failed Authentication:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Digest realm="example.com", qop="auth", nonce="nonce", opaque="someopaquevalue"
Authentication required
Edit the users object in server.js to add/modify credentials:
const users = {
'admin': 'password',
'user': 'secret',
'newuser': 'newpassword' // Add more users here
};Key configuration variables in server.js:
const realm = 'example.com'; // Authentication realm
const nonce = 'nonce'; // Server nonce
const opaque = 'someopaquevalue'; // Opaque valueChange the server port:
server.listen(4444, () => { // Change 4444 to desired port- Initial Request: Client makes request without credentials
- Challenge: Server responds with 401 + WWW-Authenticate header containing:
realm: Authentication realmnonce: Server-generated random valueqop: Quality of protection ("auth")opaque: Server-specific string
- Response: Client calculates digest and sends Authorization header
- Validation: Server validates the digest response
The server calculates the expected response using:
HA1 = MD5(username:realm:password)
HA2 = MD5(method:uri)
response = MD5(HA1:nonce:nc:cnonce:qop:HA2)
Where:
nc: Nonce count (hex)cnonce: Client nonceqop: Quality of protection
The implementation includes robust header parsing that handles:
- Quoted and unquoted parameter values
- Proper comma separation
- Whitespace normalization
- Escape sequence handling
# Test authentication flow
curl -v --digest -u admin:password http://localhost:4444
# View detailed server logs for debugging
node server.js
# Then make requests in another terminalVisit http://localhost:4444 in your browser. You'll see a browser authentication dialog. Use:
- Username:
admin, Password:password - Username:
user, Password:secret
- Uses hardcoded credentials (use a database in production)
- Uses a static nonce (should be dynamic and expire)
- No HTTPS (digest auth should use HTTPS in production)
- Limited input validation
# Kill existing server
pkill -f "node server.js"
# Or use a different port in server.jsCheck the server console for debug output showing:
- Parsed authentication parameters
- Expected vs actual digest values
- Username/nonce validation results
The server logs detailed parsing information. Common issues:
- Malformed Authorization header
- Missing required parameters (username, realm, nonce, response)
- Nonce mismatch
MIT License - Feel free to use and modify.