Skip to content

Commit

Permalink
Sketch out dynamic configuration middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudson committed Sep 7, 2017
1 parent 2c7c684 commit 2475f79
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/authenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ Authenticator.prototype.authenticate = function(strategy, options, callback) {
return this._framework.authenticate(this, strategy, options, callback);
};


Authenticator.prototype.authenticateWith = function(strategy, options, callback) {
return this._framework.authenticateWith(this, strategy, options, callback);
};


/**
* Middleware that will authorize a third-party account using the given
* `strategy` name, with optional `options`.
Expand Down
6 changes: 4 additions & 2 deletions lib/framework/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* Module dependencies.
*/
var initialize = require('../middleware/initialize')
, authenticate = require('../middleware/authenticate');
, authenticate = require('../middleware/authenticate')
, authenticateWith = require('../middleware/authenticateWith');

/**
* Framework support for Connect/Express.
Expand All @@ -22,7 +23,8 @@ exports = module.exports = function() {

return {
initialize: initialize,
authenticate: authenticate
authenticate: authenticate,
authenticateWith: authenticateWith
};
};

Expand Down
39 changes: 39 additions & 0 deletions lib/middleware/authenticateWith.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Module dependencies.
*/
var authenticate = require('./authenticate')
, Strategy = require('passport-strategy')


module.exports = function authenticateWith(passport, configurator, options, callback) {
if (typeof options == 'function') {
callback = options;
options = {};
}
options = options || {};

if (!configurator) { throw new TypeError('passport.authenticateWith middleware requires a configuration function'); }

return function authenticateWith(req, res, next) {

function configured(err, method) {
if (err) { return next(err); }
if (!method) {
return next(new Error('Dynamic configuration did not return a valid passport strategy'));
}

return authenticate(passport, method, options, callback)(req, res, next);
}

try {
var arity = configurator.length;
if (arity == 3) {
configurator(req, options, configured);
} else { // arity == 2
configurator(req, configured);
}
} catch (ex) {
return next(ex);
}
};
};

0 comments on commit 2475f79

Please sign in to comment.