Skip to content

Commit

Permalink
adding the ability to pass apikey in as a header (also added unit test)
Browse files Browse the repository at this point in the history
  • Loading branch information
brozeph committed May 13, 2013
1 parent 0e5bfc5 commit ba85b08
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 77 deletions.
20 changes: 11 additions & 9 deletions lib/passport-localapikey/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ function Strategy(options, verify) {
options = {};
}
if (!verify) throw new Error('local authentication strategy requires a verify function');

this._apiKeyField = options.apiKeyField || 'apikey';

passport.Strategy.call(this);
this.name = 'localapikey';
this._verify = verify;
Expand All @@ -66,26 +66,28 @@ util.inherits(Strategy, passport.Strategy);
*/
Strategy.prototype.authenticate = function(req, options) {
options = options || {};
var apikey = lookup(req.body, this._apiKeyField) || lookup(req.query, this._apiKeyField);

var apikey = lookup(req.body, this._apiKeyField)
|| lookup(req.query, this._apiKeyField)
|| lookup(req.headers, this._apiKeyField);

if (!apikey) {
return this.fail(new BadRequestError(options.badRequestMessage || 'Missing API Key'));
}

var self = this;

function verified(err, user, info) {
if (err) { return self.error(err); }
if (!user) { return self.fail(info); }
self.success(user, info);
}

if (self._passReqToCallback) {
this._verify(req, apikey, verified);
} else {
this._verify(apikey, verified);
}

function lookup(obj, field) {
if (!obj) { return null; }
var chain = field.split(']').join('').split('[');
Expand All @@ -102,5 +104,5 @@ Strategy.prototype.authenticate = function(req, options) {

/**
* Expose `Strategy`.
*/
*/
module.exports = Strategy;
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "passport-localapikey",
"version": "0.0.1",
"version": "0.0.2",
"description": "Local api key authentication strategy for Passport.",
"author": { "name": "Sudhakar Mani", "email": "mail4sudhakar@gmail.com", "url": "http://www.sudhakarmani.com/" },
"repository": {
"type": "git",
"url": "git://github.com/cholalabs/passport-localapikey.git"
},
"bugs": {
"bugs": {
"url": "https://github.com/cholalabs/passport-localapikey/issues"
},
"main": "./lib/passport-localapikey",
Expand All @@ -16,15 +16,15 @@
"passport": "~0.1.1"
},
"devDependencies": {
"vows": "0.6.x"
"vows": "0.7.x"
},
"scripts": {
"test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
},
"engines": { "node": ">= 0.4.0" },
"licenses": [ {
"type": "MIT",
"url": "http://www.opensource.org/licenses/MIT"
"url": "http://www.opensource.org/licenses/MIT"
} ],
"keywords": ["passport", "local apikey", "auth", "authn", "authentication"]
}
Loading

0 comments on commit ba85b08

Please sign in to comment.