plex-web-downloader/node_modules/basic-auth
2015-10-13 21:47:32 +02:00
..
HISTORY.md version app + node.exe + nw.exe 2015-10-13 21:47:32 +02:00
index.js version app + node.exe + nw.exe 2015-10-13 21:47:32 +02:00
LICENSE version app + node.exe + nw.exe 2015-10-13 21:47:32 +02:00
package.json version app + node.exe + nw.exe 2015-10-13 21:47:32 +02:00
README.md version app + node.exe + nw.exe 2015-10-13 21:47:32 +02:00

basic-auth

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Generic basic auth Authorization header field parser for whatever.

Installation

$ npm install basic-auth

API

var auth = require('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.

Example

Pass a node request or koa Context object to the module exported. If parsing fails undefined is returned, otherwise an object with .name and .pass.

var auth = require('basic-auth');
var user = auth(req);
// => { name: 'something', pass: 'whatever' }

With vanilla node.js http server

var http = require('http')
var auth = require('basic-auth')

// Create server
var server = http.createServer(function (req, res) {
  var credentials = auth(req)

  if (!credentials || credentials.name !== 'john' || credentials.pass !== 'secret') {
    res.statusCode = 401
    res.setHeader('WWW-Authenticate', 'Basic realm="example"')
    res.end('Access denied')
  } else {
    res.end('Access granted')
  }
})

// Listen
server.listen(3000)

License

MIT