I have encountered a problem when using passport (with the passport-azure-ad strategy) to authenticate requests. The request to Azure Active Directory works fine. I can login with my credentials. After that, I expect the verify callback to be called, setting the user object based on the accessToken etc. Then I expect the redirect route function to be called where the user object will be available. This is my setup:
passport.serializeUser((accessToken, done) => {
done(null, accessToken);
});
passport.deserializeUser((accessToken, done) => {
done(null, accessToken);
});
passport.use(new OIDCStrategy({
// options for the azure AD strategy
identityMetadata: config.auth.identityMetadata,
clientID: config.auth.clientID,
clientSecret: config.auth.clientSecret,
redirectUrl: config.auth.redirectUrl,
responseType: 'code id_token',
responseMode: 'query',
allowHttpForRedirectUrl: config.auth.allowHttpForRedirectUrl,
isB2C: true,
passReqToCallback: false,
scope: config.auth.scope,
loggingLevel: 'error'
}, (iss, sub, profile, jwtClaims, accessToken, refreshToken, params, done) => {
console.log('1. VERIFY CALLBACK');
if (!accessToken) {
return done(new Error('No accessToken was given'), null);
}
return done(null, { accessToken, expires: params.expires_on, refreshToken });
}));
My routes:
router.get('/auth/login', (req, res, next) => {
passport.authenticate('azuread-openidconnect', { failureRedirect: '/' })(req, res, next);
});
router.get('/auth/openid/redirect', (req, res, next) => {
passport.authenticate('azuread-openidconnect', { failureRedirect: '/' }, (err, user) => {
console.log('2. ROUTE REDIRECT');
return res.send('...then redirect existing user to profile page');
})(req, res, next);
});
2 out of 10 times, my expectations are right and everything works fine. The rest of the times the verify callback is never called, or the redirect route function is called before it, and therefor the user object is never set (user is 'false').
Anyone have a clue how I can get through this?