xprezzo-basic-auth
Basic auth Authorization header field parser for xprezzo.
Philosophy of Xprezzo
Problems faced:
- Too many requires which creates problem when project grow
- The dependencies update are slow
- Test cases of difficult to design
How Xprezzo try to tackle those problems:
- Useful internal libraries/packages are exposed
- Merge small libraries into a larger one.
- Provide easy to use test framework
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 = require('xprezzo-basic-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 = require('xprezzo-basic-auth')
var user = auth(req)
A header string from any other location can also be parsed with
auth.parse
, for example a Proxy-Authorization
header:
var auth = require('xprezzo-basic-auth')
var user = auth.parse(req.getHeader('Proxy-Authorization'))
With vanilla node.js http server
var http = require('http')
var auth = require('xprezzo-basic-auth')
var compare = require('tsscmp')
var server = http.createServer(function (req, res) {
var credentials = auth(req)
if (!credentials || !check(credentials.name, credentials.pass)) {
res.statusCode = 401
res.setHeader('WWW-Authenticate', 'Basic realm="example"')
res.end('Access denied')
} else {
res.end('Access granted')
}
})
function check (name, pass) {
var valid = true
valid = compare(name, 'john') && valid
valid = compare(pass, 'secret') && valid
return valid
}
server.listen(3000)
Exposes
License
MIT