{"id":13527444,"url":"https://github.com/jshttp/basic-auth","last_synced_at":"2025-04-23T20:54:01.704Z","repository":{"id":12203645,"uuid":"14809373","full_name":"jshttp/basic-auth","owner":"jshttp","description":"Generic basic auth Authorization header field parser","archived":false,"fork":false,"pushed_at":"2025-01-02T07:01:34.000Z","size":68,"stargazers_count":703,"open_issues_count":11,"forks_count":87,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-04-23T20:53:54.864Z","etag":null,"topics":["basic-auth","http","javascript","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jshttp.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"express"}},"created_at":"2013-11-29T20:10:24.000Z","updated_at":"2025-04-06T06:36:24.000Z","dependencies_parsed_at":"2024-06-02T18:45:58.974Z","dependency_job_id":"9400010c-65fa-47b6-8dee-03d5321db234","html_url":"https://github.com/jshttp/basic-auth","commit_stats":{"total_commits":125,"total_committers":12,"mean_commits":"10.416666666666666","dds":"0.17600000000000005","last_synced_commit":"e8a29f94dc7c05b5858b08090386338af010ce49"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jshttp%2Fbasic-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jshttp%2Fbasic-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jshttp%2Fbasic-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jshttp%2Fbasic-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jshttp","download_url":"https://codeload.github.com/jshttp/basic-auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250514767,"owners_count":21443208,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["basic-auth","http","javascript","nodejs"],"created_at":"2024-08-01T06:01:48.112Z","updated_at":"2025-04-23T20:54:01.684Z","avatar_url":"https://github.com/jshttp.png","language":"JavaScript","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\u003c!-- eslint-disable no-unused-vars --\u003e\n\n```js\nvar auth = require('basic-auth')\n```\n\n### auth(req)\n\nGet the basic auth credentials from the given request. The `Authorization`\nheader is parsed and if the header is invalid, `undefined` is returned,\notherwise an object with `name` and `pass` properties.\n\n### auth.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## Example\n\nPass a Node.js request object to the module export. If parsing fails\n`undefined` is returned, otherwise an object with `.name` and `.pass`.\n\n\u003c!-- eslint-disable no-unused-vars, no-undef --\u003e\n\n```js\nvar auth = require('basic-auth')\nvar user = auth(req)\n// =\u003e { name: 'something', pass: 'whatever' }\n```\n\nA header string from any other location can also be parsed with\n`auth.parse`, for example a `Proxy-Authorization` header:\n\n\u003c!-- eslint-disable no-unused-vars, no-undef --\u003e\n\n```js\nvar auth = require('basic-auth')\nvar user = auth.parse(req.getHeader('Proxy-Authorization'))\n```\n\n### With vanilla node.js http server\n\n```js\nvar http = require('http')\nvar auth = require('basic-auth')\nvar compare = require('tsscmp')\n\n// Create server\nvar server = http.createServer(function (req, res) {\n  var credentials = auth(req)\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  var valid = true\n\n  // Simple method to prevent short-circuit and use timing-safe compare\n  valid = compare(name, 'john') \u0026\u0026 valid\n  valid = compare(pass, 'secret') \u0026\u0026 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","funding_links":["https://opencollective.com/express"],"categories":["Repository","JavaScript"],"sub_categories":["Authorization"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjshttp%2Fbasic-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjshttp%2Fbasic-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjshttp%2Fbasic-auth/lists"}