basic-auth
Generic basic auth Authorization header field parser for whatever.
Installation
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install basic-auth
API
var auth =
auth(req)
Get the basic auth credentials from the given request. The Authorization
header is parsed and if the header is invalid, undefined
is returned,
otherwise an object with name
and pass
properties.
auth.parse(string)
Parse a basic auth authorization header string. This will return an object
with name
and pass
properties, or undefined
if the string is invalid.
Example
Pass a Node.js request object to the module export. If parsing fails
undefined
is returned, otherwise an object with .name
and .pass
.
var auth = var user = // => { name: 'something', pass: 'whatever' }
A header string from any other location can also be parsed with
auth.parse
, for example a Proxy-Authorization
header:
var auth = var user = auth
With vanilla node.js http server
var http = var auth = var compare = // Create servervar server = http // Basic function to validate credentials for example { var valid = true // Simple method to prevent short-circut and use timing-safe compare valid = && valid valid = && valid return valid} // Listenserver