Skip to content

Commit

Permalink
refactor to use basic-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Mar 8, 2014
1 parent b443da4 commit 3cf7b2e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
32 changes: 13 additions & 19 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

var accepts = require('accepts');
var typeis = require('type-is');
var http = require('http')
, utils = require('./utils')
, fresh = require('fresh')
, parseRange = require('range-parser')
, parse = utils.parseUrl
var basicAuth = require('basic-auth');
var http = require('http');
var fresh = require('fresh');
var parseRange = require('range-parser');
var utils = require('./utils');
var parse = utils.parseUrl;

/**
* Request prototype.
Expand Down Expand Up @@ -318,20 +319,13 @@ req.__defineGetter__('ips', function(){
*/

req.__defineGetter__('auth', function(){
// missing
var auth = this.get('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().match(/^([^:]*):(.*)$/);
if (!auth) return;
return { username: auth[1], password: auth[2] };
var auth = basicAuth(this);
if (auth) {
// aliases for backwards compatibilty
auth.username = auth.name;
auth.password = auth.pass;
}
return auth;
});

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
}
],
"dependencies": {
"basic-auth": "0.0.1",
"accepts": "1.0.0",
"type-is": "1.0.0",
"range-parser": "1.0.0",
Expand Down
10 changes: 8 additions & 2 deletions test/req.auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ describe('req', function(){
var app = express();

app.get('/', function(req, res){
res.send(req.auth || 'none');
res.send({
username: req.auth.username,
password: req.auth.password,
});
});

request(app)
Expand All @@ -82,7 +85,10 @@ describe('req', function(){
var app = express();

app.get('/', function(req, res){
res.send(req.auth || 'none');
res.send({
username: req.auth.username,
password: req.auth.password,
});
});

request(app)
Expand Down

0 comments on commit 3cf7b2e

Please sign in to comment.