{"library":"simple-auth-basic","title":"Basic HTTP Authentication Parser","description":"simple-auth-basic is a lightweight Node.js module designed for parsing HTTP Basic Authorization headers. It extracts the username and password from the 'Authorization' header in an incoming request or a raw header string, returning an object with `name` and `pass` properties. If the header is invalid or missing, it returns `undefined`. The current stable version is 2.0.8. As a focused utility for a well-established standard, its release cadence is generally slow, primarily for maintenance and compatibility updates rather than new features. Its key differentiator is its simplicity and singular focus on parsing the header, leaving credential validation to the application logic, often paired with a timing-safe string comparison library like `tsscmp` for security.","language":"javascript","status":"maintenance","last_verified":"Wed Apr 22","install":{"commands":["npm install simple-auth-basic"],"cli":null},"imports":["import auth from 'simple-auth-basic'","import auth from 'simple-auth-basic'; auth.parse(headerString)"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import http from 'http';\nimport auth from 'simple-auth-basic';\nimport compare from 'tsscmp'; // Often used for timing-safe comparisons\n\nconst server = http.createServer((req, res) => {\n  const credentials = auth(req);\n\n  // Basic function to validate credentials (against a user store in real apps)\n  function check (name, pass) {\n    let valid = true;\n    // Using tsscmp to prevent timing attacks\n    valid = compare(name, 'john') && valid;\n    valid = compare(compare(pass, 'secret') && valid);\n    return valid;\n  }\n\n  if (!credentials || !check(credentials.name, credentials.pass)) {\n    res.statusCode = 401;\n    res.setHeader('WWW-Authenticate', 'Basic realm=\"Secure Area\"');\n    res.end('Access denied');\n  } else {\n    res.end(`Welcome, ${credentials.name}! Access granted.`);\n  }\n});\n\nconst port = 3000;\nserver.listen(port, () => {\n  console.log(`Server listening on http://localhost:${port}`);\n  console.log('Try accessing with \"john:secret\" basic auth.');\n});","lang":"javascript","description":"Demonstrates setting up a basic Node.js HTTP server that uses simple-auth-basic to parse Authorization headers and perform credential validation. It includes an example of using `tsscmp` for secure password comparison.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}