-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 647d32c
Showing
6 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
test: | ||
@./node_modules/.bin/mocha \ | ||
--require should \ | ||
--reporter dot \ | ||
--bail | ||
|
||
.PHONY: test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
# name | ||
|
||
Generic basic auth Authorization header field parser for whatever. | ||
|
||
## Installation | ||
|
||
``` | ||
$ npm install basic-auth | ||
``` | ||
|
||
## 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`. | ||
|
||
```js | ||
var auth = require('basic-auth'); | ||
var user = auth(req); | ||
// => { name: 'something', pass: 'whatever' } | ||
|
||
``` | ||
|
||
# License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
/** | ||
* Parse the Authorization header field of `req`. | ||
* | ||
* @param {Request} req | ||
* @return {Object} with .name and .pass | ||
* @api public | ||
*/ | ||
|
||
module.exports = function(req){ | ||
req = req.req || req; | ||
|
||
var auth = req.headers.authorization; | ||
if (!auth) return; | ||
|
||
// malformed | ||
var parts = auth.split(' '); | ||
if ('basic' != parts[0].toLowerCase()) return; | ||
if (!parts[1]) return; | ||
auth = parts[1]; | ||
|
||
// credentials | ||
auth = new Buffer(auth, 'base64').toString(); | ||
auth = auth.match(/^([^:]+):(.+)$/); | ||
if (!auth) return; | ||
|
||
return { name: auth[1], pass: auth[2] }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "basic-auth", | ||
"version": "0.0.1", | ||
"repository": "visionmedia/node-basic-auth", | ||
"description": "generic basic auth parser", | ||
"keywords": ["basic", "auth", "authorization", "basicauth"], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "*" | ||
}, | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
var assert = require('assert'); | ||
var auth = require('..'); | ||
|
||
function request(authorization) { | ||
return { | ||
headers: { | ||
authorization: authorization | ||
} | ||
} | ||
} | ||
|
||
describe('auth(req)', function(){ | ||
describe('with no Authorization field', function(){ | ||
it('should return null', function(){ | ||
var req = request(); | ||
assert(null == auth(req)); | ||
}) | ||
}) | ||
|
||
describe('with malformed Authorization field', function(){ | ||
it('should return null', function(){ | ||
var req = request('Something'); | ||
assert(null == auth(req)); | ||
}) | ||
}) | ||
|
||
describe('with malformed credentials', function(){ | ||
it('should return nulll', function(){ | ||
var req = request('basic Zm9vcgo='); | ||
assert(null == auth(req)); | ||
}) | ||
}) | ||
|
||
describe('with valid credentials', function(){ | ||
it('should return .user and .pass', function(){ | ||
var req = request('basic Zm9vOmJhcg=='); | ||
auth(req).should.eql({ name: 'foo', pass: 'bar' }); | ||
}) | ||
}) | ||
}) |