Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Passport SAML from multiple Identity Providers

I've implemented Passport-SAML into my site, and now I've been tasked with connecting our site with two other Identity Providers. In my code, it seems to use only the most recent definition of the SamlStrategy. How can I set up Passport to allow multiple different implementations of the same Strategy?

My implementation looks like this:

passport.use(new SamlStrategy(
    {
        path: '/saml',
        entryPoint: "https://idp.identityprovider.net/idp/profile/SAML2/Redirect/SSO",
        issuer: 'https://www.serviceprovider.com/saml',
        identifierFormat: 'urn:domain:safemls:nameid-format:loginid'
    },
    function(profile, done) {
        console.log("SamlStrategy done", profile)
        User.findOne({email:profile.Email}, function(err, user) {
            if (err) {
                return done(err);
            }
            if(!user) return done(null, false, {message: 'No account associated with this email.'})
            return done(null, user);
        });
    }
));
like image 497
Alex Avatar asked Aug 09 '13 21:08

Alex


2 Answers

You can give each strategy a name

passport.use('config1', new SamlStrategy(..), callback);
passport.use('config2', new SamlStrategy(..), callback);

and then

app.post('/login/callback',
  function(req, res) {
      var config = // extract config name somehow
      passport.authenticate(config, { failureRedirect: '/', failureFlash: true })();
  }
  function(req, res) {
    res.redirect('/');
  }
);
like image 181
woloski Avatar answered Oct 24 '22 01:10

woloski


little fixes (and a lot of time saving :) ) to @woloski answer:

Giving strategy name:

passport.use( new SamlStrategy(name:'config1', ...), callback);
passport.use( new SamlStrategy(name:'config2', ...), callback);

and handling the post response:

app.post('/login/callback',
function(req, res, next) {
      var config = // extract config name somehow
      passport.authenticate(config, { failureRedirect: '/', failureFlash: true })(req, res, next);
  }
  function(req, res) {
    res.redirect('/');
  }
);

Cheers

like image 31
ykorach Avatar answered Oct 24 '22 00:10

ykorach