Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Nov 29, 2013
0 parents commit 647d32c
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
Empty file added .gitignore
Empty file.
8 changes: 8 additions & 0 deletions Makefile
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
27 changes: 27 additions & 0 deletions Readme.md
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
28 changes: 28 additions & 0 deletions index.js
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] };
};
13 changes: 13 additions & 0 deletions package.json
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"
}
41 changes: 41 additions & 0 deletions test/index.js
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' });
})
})
})

0 comments on commit 647d32c

Please sign in to comment.