diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9987df8 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ + +test: + @./node_modules/.bin/mocha \ + --require should \ + --reporter dot \ + --bail + +.PHONY: test \ No newline at end of file diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..e2232b4 --- /dev/null +++ b/Readme.md @@ -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 \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..a3d3324 --- /dev/null +++ b/index.js @@ -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] }; +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..f6a130c --- /dev/null +++ b/package.json @@ -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" +} \ No newline at end of file diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..6c62178 --- /dev/null +++ b/test/index.js @@ -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' }); + }) + }) +}) \ No newline at end of file