Azure SSO using saml in node js keeps redirecting

Viewed 194

I am trying to implement Azure SSO saml login on node.js. I have configured the app in Azure and got login URL, cert and issuer.

In node js code, I am using passport-saml npm. I have initialized the passport using below code

app.use(passport.initialize());
app.use(passport.session());

passport.use(new SamlStrategy(config.saml, function(user, done) {  
    done(null, user);
}));

passport.serializeUser(function(user, done) {  
    done(null, user);
});

passport.deserializeUser(function(user, done) {  
    done(null, user);
});

And My saml config is as below

saml: {
    callback: "https://fake-url.com/callback",
    entryPoint: "https://login.microsoftonline.com/<AppId>/saml2",
    issuer: "my-app-issue",
    cert: "My Cert",
    disableRequestedAuthnContext: true,
    identifierFormat: null,
    acceptedClockSkewMs: 30000,
    protocol: "https://"
  },

I have configured my login route as below

router.get('/login',
    passport.authenticate('saml', {failureRedirect: '/error', failureFlash: true}),
    function(req, res) {
        res.sendFile('https://fake-url/static/default.html');
});

And I have configured my callback URL as below

router.post(
    '/callback',
    passport.authenticate('saml', {failureRedirect: '/error', failureFlash: true}),
    function(req, res) { 
        res.redirect('/');  
    }
);

Now whenever I am logging I am getting the response in callback url and I can see logged in user details as well. But when it redirects to url '/', In my authentication middle ware I am getting not authenticated. My authentication middle ware is as below.

router.use(function(req, res, next){     
    if (req.isAuthenticated()) {      // this line always returns false  
        next();
    } else {
        res.redirect("/login");
    }
});

In my above code snippet second line always return false even after redirected from success calllback and it keeps redirecting to login.

Any help is appreciated.

0 Answers
Related