passport-oauth2 icon indicating copy to clipboard operation
passport-oauth2 copied to clipboard

Dynamic clientID and clientSecret

Open rbjarnason opened this issue 9 years ago • 9 comments

My app allows many different domains to be used so I need to store clientID and clientSecret in the database with each domain in the database. Can you give me pointers on what I'd need to do to make this work in passport?

After looking at the code for passport-facebook, passport-oauth2 and node-oauth2 then it looks like I would have to change those to somehow reset the oauth2 object with the dynamically on each authorize call in a similar place as the dynamic callbackURL is supported.

rbjarnason avatar Feb 01 '16 14:02 rbjarnason

I'm working on a similar issue. You simply store a unique name for your provider in with the clientID and clientSecret then pass that name to passport.use().

For example, pull all of your providers from the database, iterate through all of them one by one and fire off a call to passport.use with the first parameter as the unique name.

In sails.js, I do something like this:

`AuthProviders.find({host: hostName}).exec(function findCB(err, providers) { if (err) { // Do something with error }

for (provider in providers) { passport.use(providers[provider].uniqueName, {}, function authCallback(param1, param2) { // Do something when this callback is triggered by the passport authentication } } });`

mattmccarty avatar Feb 20 '16 17:02 mattmccarty

Thanks for the pointer @mattmccarty :) I will get back to working on those features in a couple of weeks time. Will report here how it goes.

rbjarnason avatar Feb 20 '16 17:02 rbjarnason

No problem. I'm working on an npm module called passport-sso that handles everything you have mentioned plus some. Maybe it's better to collaborate than both us us writing something new? Let me know if you are interested

mattmccarty avatar Feb 20 '16 23:02 mattmccarty

@mattmccarty I finally have the time to start looking at this again. I will try out your https://github.com/mattmccarty/passport-sso module - looks like you've already done most (if not all) of the work to get this working! :)

rbjarnason avatar Mar 22 '16 10:03 rbjarnason

@mattmccarty I've integrated passport-sso into my project and it works well, thanks for your great code :) I will need to add Twitter and SAML support in the next few weeks, I will do PR's in the case I need to add something to your base to support other models than local and oauth2, I'm not sure I will do. https://github.com/rbjarnason/your-priorities-app

rbjarnason avatar Apr 28 '16 15:04 rbjarnason

For what it's worth, I had to work around this issue as well. I wrote my own OAuth strategy for a couple reasons, but solved the client issue with a dedicated Client class. the API was basically:

const client = new Client(clientId, clientSecret);
passport.use(new CustomOAuth2Strategy({
  client: client,
  ...
});

The client class includes an update method that I call in response to configuration changes, so I can update without restarting the server.

kara-ryli avatar May 04 '16 02:05 kara-ryli

@Ry7n How did you get around the Oauth2 constructor requiring things like clientId and secret up front?

crung avatar May 31 '18 10:05 crung

I'm running into this limitation as well, by way of the facebook-strategy, which inherits this one. Trying to get the existing passport-oauth2 strategy to use dynamic clients seems like a bit of a shoehorn.

It looks like by design, there's an expectation for there to only be one instance of a type of strategy to be present in the passport configuration, as the strategy names are set in the strategy's constructor. While I do find passport to have been suitable for my needs, I ran across authom, which has allowed me to register multiple instances of the same kind of strategy, and is low level enough that it has allowed me to have it even work along side passport strategies.

twelve17 avatar Jun 08 '18 00:06 twelve17

I implemented a workaround downstream by dynamically reinstantiating them as they are accessed per request, which isn't desirable but works. It would be great to have this done on this level of the OAuth strategy so derived strategies will have this feature.

basememara avatar Apr 24 '24 11:04 basememara