basic-auth
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -0,1 +1,6 @@ | ||
1.1.0 / 2016-11-18 | ||
================== | ||
* Add `auth.parse` for low-level string parsing | ||
1.0.4 / 2016-05-10 | ||
@@ -2,0 +7,0 @@ ================== |
62
index.js
@@ -5,3 +5,3 @@ /*! | ||
* Copyright(c) 2014 Jonathan Ong | ||
* Copyright(c) 2015 Douglas Christopher Wilson | ||
* Copyright(c) 2015-2016 Douglas Christopher Wilson | ||
* MIT Licensed | ||
@@ -18,2 +18,3 @@ */ | ||
module.exports = auth | ||
module.exports.parse = parse | ||
@@ -29,3 +30,3 @@ /** | ||
var credentialsRegExp = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9\-\._~\+\/]+=*) *$/ | ||
var CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/ | ||
@@ -41,3 +42,3 @@ /** | ||
var userPassRegExp = /^([^:]*):(.*)$/ | ||
var USER_PASS_REGEXP = /^([^:]*):(.*)$/ | ||
@@ -52,3 +53,3 @@ /** | ||
function auth(req) { | ||
function auth (req) { | ||
if (!req) { | ||
@@ -66,17 +67,3 @@ throw new TypeError('argument req is required') | ||
// parse header | ||
var match = credentialsRegExp.exec(header || '') | ||
if (!match) { | ||
return | ||
} | ||
// decode user pass | ||
var userPass = userPassRegExp.exec(decodeBase64(match[1])) | ||
if (!userPass) { | ||
return | ||
} | ||
// return credentials object | ||
return new Credentials(userPass[1], userPass[2]) | ||
return parse(header) | ||
} | ||
@@ -89,3 +76,3 @@ | ||
function decodeBase64(str) { | ||
function decodeBase64 (str) { | ||
return new Buffer(str, 'base64').toString() | ||
@@ -99,3 +86,3 @@ } | ||
function getAuthorization(req) { | ||
function getAuthorization (req) { | ||
if (!req.headers || typeof req.headers !== 'object') { | ||
@@ -109,2 +96,33 @@ throw new TypeError('argument req is required to have headers property') | ||
/** | ||
* Parse basic auth to object. | ||
* | ||
* @param {string} string | ||
* @return {object} | ||
* @public | ||
*/ | ||
function parse (string) { | ||
if (typeof string !== 'string') { | ||
return undefined | ||
} | ||
// parse header | ||
var match = CREDENTIALS_REGEXP.exec(string) | ||
if (!match) { | ||
return undefined | ||
} | ||
// decode user pass | ||
var userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1])) | ||
if (!userPass) { | ||
return undefined | ||
} | ||
// return credentials object | ||
return new Credentials(userPass[1], userPass[2]) | ||
} | ||
/** | ||
* Object to represent user credentials. | ||
@@ -114,5 +132,5 @@ * @private | ||
function Credentials(name, pass) { | ||
function Credentials (name, pass) { | ||
this.name = name | ||
this.pass = pass | ||
} |
{ | ||
"name": "basic-auth", | ||
"description": "node.js basic auth parser", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -14,3 +14,8 @@ "keywords": [ | ||
"devDependencies": { | ||
"istanbul": "0.4.3", | ||
"eslint": "3.10.2", | ||
"eslint-config-standard": "6.2.1", | ||
"eslint-plugin-markdown": "1.0.0-beta.3", | ||
"eslint-plugin-promise": "3.4.0", | ||
"eslint-plugin-standard": "2.0.1", | ||
"istanbul": "0.4.5", | ||
"mocha": "1.21.5" | ||
@@ -27,2 +32,3 @@ }, | ||
"scripts": { | ||
"lint": "eslint --plugin markdown --ext js,md .", | ||
"test": "mocha --check-leaks --reporter spec --bail", | ||
@@ -29,0 +35,0 @@ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", |
@@ -13,2 +13,6 @@ # basic-auth | ||
This is a [Node.js](https://nodejs.org/en/) module available through the | ||
[npm registry](https://www.npmjs.com/). Installation is done using the | ||
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): | ||
``` | ||
@@ -20,2 +24,4 @@ $ npm install basic-auth | ||
<!-- eslint-disable no-unused-vars --> | ||
```js | ||
@@ -31,2 +37,7 @@ var auth = require('basic-auth') | ||
### 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 | ||
@@ -38,7 +49,18 @@ | ||
<!-- eslint-disable no-unused-vars, no-undef --> | ||
```js | ||
var auth = require('basic-auth'); | ||
var user = auth(req); | ||
var auth = require('basic-auth') | ||
var user = auth(req) | ||
// => { name: 'something', pass: 'whatever' } | ||
``` | ||
A header string from any other location can also be parsed with | ||
`auth.parse`, for example a `Proxy-Authorization` header: | ||
<!-- eslint-disable no-unused-vars, no-undef --> | ||
```js | ||
var auth = require('basic-auth') | ||
var user = auth.parse(req.getHeader('Proxy-Authorization')) | ||
``` | ||
@@ -45,0 +67,0 @@ |
Sorry, the diff of this file is not supported yet
8089
100
101
7