{"_id":"basic-auth@3.1.0","bugs":{"url":"https://github.com/jshttp/basic-auth/issues"},"dist":{"shasum":"719aaa7d0451a7861e13fc38880b943a0e4c33b5","tarball":"http://registry-npm.ywwl.com/basic-auth/download/basic-auth-3.1.0.tgz","integrity":"sha512-E87x8DEyyFLN1EeP5p6j8b/g7gynoV+L6G2zLXAoL1CkkXe7HjNU7JIAf4JozCXJgl5SzxREQq/HzSF/MqhPLw==","fileCount":12,"unpackedSize":15896,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/basic-auth@3.1.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIF9u6yrhM5UgtT0d/qEBtlA/SDQdK5ANnRAMcnO1csOtAiBlZ8u/IdOkhuQRSoj9zK3yGREsU5t9JTrxi+Kc2sznYA=="}],"size":5260,"noattachment":false,"key":"/basic-auth/-/basic-auth-3.1.0.tgz"},"name":"basic-auth","type":"module","engines":{"node":">=22"},"exports":"./dist/index.js","gitHead":"d7375e84e0cafbe46ddde2e07272d2bebba69472","license":"MIT","scripts":{"lint":"ts-scripts lint","size":"size-limit","test":"ts-scripts test && npm run size","bench":"vitest bench","build":"ts-scripts build","specs":"ts-scripts specs","format":"ts-scripts format","prepare":"ts-scripts install && npm run build"},"version":"3.1.0","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"887ffc00-b876-4f93-8807-04f14639ad68"},"approver":{"name":"blakeembrey","email":"hello@blakeembrey.com"}},"homepage":"https://github.com/jshttp/basic-auth#readme","keywords":["basic","auth","authorization","basicauth"],"repository":{"url":"git+https://github.com/jshttp/basic-auth.git","type":"git"},"size-limit":[{"path":"dist/index.js","limit":"1 kB"}],"ts-scripts":{"dist":["dist"],"project":["tsconfig.build.json"]},"_npmVersion":"12.1.0","description":"Basic auth parser","directories":{},"maintainers":[{"name":"ulisesgascon","email":"ulisesgascondev@gmail.com"},{"name":"blakeembrey","email":"hello@blakeembrey.com"},{"name":"tjholowaychuk","email":"tj@vision-media.ca"},{"name":"dougwilson","email":"doug@somethingdoug.com"},{"name":"jonathanong","email":"jonathanrichardong@gmail.com"},{"name":"jongleberry","email":"jonathanrichardong@gmail.com"}],"_nodeVersion":"27.0.0-alpha.0.0.0","devDependencies":{"vitest":"^5.1.0","size-limit":"^14.3.0","typescript":"^7.0.2","@types/node":"^26.6.6","@vitest/coverage-v8":"^5.1.0","@borderless/ts-scripts":"^0.15.0","@size-limit/preset-small-lib":"^14.3.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/basic-auth_3.1.0_1793814940702_0.8474268689172411"},"_hasShrinkwrap":false,"readme":"# basic-auth\n\n[![NPM Version][npm-version-image]][npm-url]\n[![NPM Downloads][npm-downloads-image]][npm-url]\n[![Node.js Version][node-image]][node-url]\n[![Build Status][ci-image]][ci-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n\nGeneric basic auth Authorization header field parser for whatever.\n\n## Installation\n\nThis is a [Node.js](https://nodejs.org/en/) module available through the\n[npm registry](https://www.npmjs.com/). Installation is done using the\n[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):\n\n```\n$ npm install basic-auth\n```\n\n## API\n\n```js\nconst { parse } = require('basic-auth');\n```\n\n### parse(string)\n\nParse a basic auth authorization header string. This will return an object\nwith `name` and `pass` properties, or `undefined` if the string is invalid.\n\n### format(credentials)\n\nFormat a credentials object with `name` and `pass` properties as a basic\nauth authorization header string.\n\n## Example\n\nPass a Basic auth header to the `parse()` method. If parsing fails\n`undefined` is returned, otherwise an object with `.name` and `.pass`.\n\n```js\nconst { parse } = require('basic-auth');\nconst user = parse(req.headers.authorization ?? '');\n// => { name: 'something', pass: 'whatever' }\n```\n\nA header string from any other location can also be parsed for example a `Proxy-Authorization` header:\n\n```js\nconst { parse } = require('basic-auth');\nconst user = parse(req.getHeader('Proxy-Authorization') ?? '');\n```\n\nA credentials object can be formatted with `auth.format` as\nbasic auth header string.\n\n```js\nconst { format } = require('basic-auth');\nconst credentials = { name: 'foo', pass: 'bar' };\nconst authHeader = format(credentials);\n// => \"Basic Zm9vOmJhcg==\"\n```\n\n### With vanilla node.js http server\n\n```js\nconst http = require('node:http');\nconst { parse } = require('basic-auth');\nconst compare = require('tsscmp');\n\n// Create server\nconst server = http.createServer(function (req, res) {\n  const credentials = parse(req.headers.authorization ?? '');\n\n  // Check credentials\n  // The \"check\" function will typically be against your user store\n  if (!credentials || !check(credentials.name, credentials.pass)) {\n    res.statusCode = 401;\n    res.setHeader('WWW-Authenticate', 'Basic realm=\"example\"');\n    res.end('Access denied');\n  } else {\n    res.end('Access granted');\n  }\n});\n\n// Basic function to validate credentials for example\nfunction check(name, pass) {\n  let valid = true;\n\n  // Simple method to prevent short-circuit and use timing-safe compare\n  valid = compare(name, 'john') && valid;\n  valid = compare(pass, 'secret') && valid;\n\n  return valid;\n}\n\n// Listen\nserver.listen(3000);\n```\n\n# License\n\n[MIT](LICENSE)\n\n[ci-image]: https://badgen.net/github/checks/jshttp/basic-auth/master?label=ci\n[ci-url]: https://github.com/jshttp/basic-auth/actions/workflows/ci.yml\n[coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/basic-auth/master\n[coveralls-url]: https://coveralls.io/r/jshttp/basic-auth?branch=master\n[node-image]: https://badgen.net/npm/node/basic-auth\n[node-url]: https://nodejs.org/en/download\n[npm-downloads-image]: https://badgen.net/npm/dm/basic-auth\n[npm-url]: https://npmjs.org/package/basic-auth\n[npm-version-image]: https://badgen.net/npm/v/basic-auth\n","publish_time":1793814940794,"_cnpm_publish_time":1793814940794,"dist-tags":{"latest":"3.1.0"}}