Skip to content

Commit 647d32c

Browse files
committed
Initial commit
0 parents  commit 647d32c

6 files changed

Lines changed: 117 additions & 0 deletions

File tree

.gitignore

Whitespace-only changes.

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
test:
3+
@./node_modules/.bin/mocha \
4+
--require should \
5+
--reporter dot \
6+
--bail
7+
8+
.PHONY: test

Readme.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
# name
3+
4+
Generic basic auth Authorization header field parser for whatever.
5+
6+
## Installation
7+
8+
```
9+
$ npm install basic-auth
10+
```
11+
12+
## Example
13+
14+
Pass a node request or koa Context object to the module exported. If
15+
parsing fails `undefined` is returned, otherwise an object with
16+
`.name` and `.pass`.
17+
18+
```js
19+
var auth = require('basic-auth');
20+
var user = auth(req);
21+
// => { name: 'something', pass: 'whatever' }
22+
23+
```
24+
25+
# License
26+
27+
MIT

index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
/**
3+
* Parse the Authorization header field of `req`.
4+
*
5+
* @param {Request} req
6+
* @return {Object} with .name and .pass
7+
* @api public
8+
*/
9+
10+
module.exports = function(req){
11+
req = req.req || req;
12+
13+
var auth = req.headers.authorization;
14+
if (!auth) return;
15+
16+
// malformed
17+
var parts = auth.split(' ');
18+
if ('basic' != parts[0].toLowerCase()) return;
19+
if (!parts[1]) return;
20+
auth = parts[1];
21+
22+
// credentials
23+
auth = new Buffer(auth, 'base64').toString();
24+
auth = auth.match(/^([^:]+):(.+)$/);
25+
if (!auth) return;
26+
27+
return { name: auth[1], pass: auth[2] };
28+
};

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "basic-auth",
3+
"version": "0.0.1",
4+
"repository": "visionmedia/node-basic-auth",
5+
"description": "generic basic auth parser",
6+
"keywords": ["basic", "auth", "authorization", "basicauth"],
7+
"dependencies": {},
8+
"devDependencies": {
9+
"mocha": "*",
10+
"should": "*"
11+
},
12+
"license": "MIT"
13+
}

test/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
var assert = require('assert');
3+
var auth = require('..');
4+
5+
function request(authorization) {
6+
return {
7+
headers: {
8+
authorization: authorization
9+
}
10+
}
11+
}
12+
13+
describe('auth(req)', function(){
14+
describe('with no Authorization field', function(){
15+
it('should return null', function(){
16+
var req = request();
17+
assert(null == auth(req));
18+
})
19+
})
20+
21+
describe('with malformed Authorization field', function(){
22+
it('should return null', function(){
23+
var req = request('Something');
24+
assert(null == auth(req));
25+
})
26+
})
27+
28+
describe('with malformed credentials', function(){
29+
it('should return nulll', function(){
30+
var req = request('basic Zm9vcgo=');
31+
assert(null == auth(req));
32+
})
33+
})
34+
35+
describe('with valid credentials', function(){
36+
it('should return .user and .pass', function(){
37+
var req = request('basic Zm9vOmJhcg==');
38+
auth(req).should.eql({ name: 'foo', pass: 'bar' });
39+
})
40+
})
41+
})

0 commit comments

Comments
 (0)