{"action":"created","issue":{"url":"https://api.github.com/repos/jaredhanson/passport/issues/606","repository_url":"https://api.github.com/repos/jaredhanson/passport","labels_url":"https://api.github.com/repos/jaredhanson/passport/issues/606/labels{/name}","comments_url":"https://api.github.com/repos/jaredhanson/passport/issues/606/comments","events_url":"https://api.github.com/repos/jaredhanson/passport/issues/606/events","html_url":"https://github.com/jaredhanson/passport/pull/606","id":256105823,"number":606,"title":"[WIP] Dynamic Configuration ","user":{"login":"sjudson","id":20324824,"avatar_url":"https://avatars0.githubusercontent.com/u/20324824?v=4","gravatar_id":"","url":"https://api.github.com/users/sjudson","html_url":"https://github.com/sjudson","followers_url":"https://api.github.com/users/sjudson/followers","following_url":"https://api.github.com/users/sjudson/following{/other_user}","gists_url":"https://api.github.com/users/sjudson/gists{/gist_id}","starred_url":"https://api.github.com/users/sjudson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sjudson/subscriptions","organizations_url":"https://api.github.com/users/sjudson/orgs","repos_url":"https://api.github.com/users/sjudson/repos","events_url":"https://api.github.com/users/sjudson/events{/privacy}","received_events_url":"https://api.github.com/users/sjudson/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2017-09-08T00:10:51Z","updated_at":"2018-03-03T00:10:28Z","closed_at":null,"author_association":"NONE","pull_request":{"url":"https://api.github.com/repos/jaredhanson/passport/pulls/606","html_url":"https://github.com/jaredhanson/passport/pull/606","diff_url":"https://github.com/jaredhanson/passport/pull/606.diff","patch_url":"https://github.com/jaredhanson/passport/pull/606.patch"},"body":"This is a work in progress sketch of dynamic configuration of passport strategies, to get high-level feedback from @jaredhanson on the design before committing to writing tests.\r\n\r\nTwo changes are made, first, the standard `authenticate` middleware is altered so that it may be called with an instance of the [`Strategy` class or subclass](https://github.com/jaredhanson/passport-strategy/blob/master/lib/strategy.js). So you could write middleware like:\r\n\r\n```js\r\napp.post('/login', \r\n  function authWrap(req, res, next) {\r\n    var lstrategy = new LocalStrategy(function(username, password, done) {\r\n      var db = dbs[req.params.directory];\r\n      db.users.verify(username, password, function(err, user) {\r\n        if (err) { return next(err); }\r\n        if (!user) { return done(null, false); }\r\n        return done(null, user);\r\n      });\r\n    }\r\n\r\n    return passport.authenticate(lstrategy)(req, res, next);\r\n  },\r\n  function(req, res) {\r\n    res.redirect('/');\r\n  });\r\n```\r\n\r\nThe second change is the introduction of an `authenticateWith` middleware, that acts as syntactic sugar for the above approach. With it, you can use a cleaner model like:\r\n\r\n\r\n```js\r\nfunction config(req, done) {\r\n  var lstrategy = new LocalStrategy(function(username, password, done) {\r\n    var db = dbs[req.params.directory];\r\n    db.users.verify(username, password, function(err, user) {\r\n      if (err) { return next(err); }\r\n      if (!user) { return done(null, false); }\r\n      return done(null, user);\r\n    });\r\n  }\r\n\r\n  return done(null, lstrategy);\r\n}\r\n\r\napp.post('/login', \r\n  passport.authenticateWith(config),\r\n  function(req, res) {\r\n    res.redirect('/');\r\n  });\r\n```\r\n\r\nThe new configurator callback can be of either of the following definitions:\r\n\r\n```js\r\nfunction configurator(req, options, done) {};\r\nfunction configurator(req, done) {};\r\n```\r\n\r\nIt can also return a string or array of strings to use strategies loaded with `passport.use`, e.g. if you dynamically want to limit the valid strategies on a route.\r\n\r\nAdditionally, this approach would obsolete the unmerged branch https://github.com/jaredhanson/passport/pull/379."},"comment":{"url":"https://api.github.com/repos/jaredhanson/passport/issues/comments/370094029","html_url":"https://github.com/jaredhanson/passport/pull/606#issuecomment-370094029","issue_url":"https://api.github.com/repos/jaredhanson/passport/issues/606","id":370094029,"user":{"login":"sjudson","id":20324824,"avatar_url":"https://avatars0.githubusercontent.com/u/20324824?v=4","gravatar_id":"","url":"https://api.github.com/users/sjudson","html_url":"https://github.com/sjudson","followers_url":"https://api.github.com/users/sjudson/followers","following_url":"https://api.github.com/users/sjudson/following{/other_user}","gists_url":"https://api.github.com/users/sjudson/gists{/gist_id}","starred_url":"https://api.github.com/users/sjudson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sjudson/subscriptions","organizations_url":"https://api.github.com/users/sjudson/orgs","repos_url":"https://api.github.com/users/sjudson/repos","events_url":"https://api.github.com/users/sjudson/events{/privacy}","received_events_url":"https://api.github.com/users/sjudson/received_events","type":"User","site_admin":false},"created_at":"2018-03-03T00:10:28Z","updated_at":"2018-03-03T00:10:28Z","author_association":"NONE","body":"I can't speak to the documentation, or the lack thereof. But the code is clear that names of strategies can be overwritten by a first parameter in the `passport.use()` call, and that the new name may be used to reference the strategy for `passport.authenticate()` calls."}}
{"actor":{"display_login":"sjudson"}}