Skip to content

Commit

Permalink
Added req.auth
Browse files Browse the repository at this point in the history
tests to come
  • Loading branch information
tj committed Jun 22, 2012
1 parent a934929 commit b400814
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,33 @@ req.__defineGetter__('ips', function(){
: [];
});

/**
* Return basic auth credentials.
*
* Examples:
*
* // http://tobi:hello@example.com
* req.auth
* // => { username: 'tobi', password: 'hello' }
*
* @return {Object}
* @api public
*/

req.__defineGetter__('auth', function(){
// missing
var auth = this.get('Authorization');
if (!auth) return {};

// malformed
auth = auth.split(' ')[1];
if (!auth) return {};

// credentials
auth = new Buffer(auth, 'base64').toString().split(':');
return { username: auth[0], password: auth[1] };
});

/**
* Return subdomains as an array.
*
Expand Down

0 comments on commit b400814

Please sign in to comment.