Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logout using passport-saml with passport-jwt for persistent authentication #244

Closed
Brynjulf opened this issue Oct 27, 2017 · 1 comment
Closed

Comments

@Brynjulf
Copy link

Hi,

I have a backend where i'm using passport-saml for initial authentication, and passport-jwt for a custom persistence. Logging in and persisting works well enough, the main issue comes when a user wants to log out, I can't access the passport-saml strategy or its logout function. The error I'm receiving is passportSaml._strategy is not a function.

Passport-saml strategy:

export function passportConfig(passport) {
  const strategyCallback = (profile: IResponse, done) => {
    getUserByIdOrCreateIfNotExists(profile)
      .then(result => {
        return done(null, result);
      })
      .catch(err => done(err));
  };

  const strategy = new SamlStrategy({
    path: process.env.CALLBACK_URL,
    entryPoint: process.env.ENTRY_POINT,
    issuer: process.env.ISSUER,
    logoutUrl: process.env.LOGOUT_URL,
    logoutCallbackUrl: process.env.LOGOUT_CALLBACK_URL,
  }, strategyCallback);

  passport.use(strategy);
}

The getUserByIdOrCreateIfNotExists function looks up the id in the database and creates a token using simple-jwt regardless of whether it found one or not and returns the token and user.

Passport-jwt strategy:

export function passportConfig(passport) {
  opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
  opts.secretOrKey = secret;
  passport.use(new JwtStrategy(opts, payloadCallback));
}

function payloadCallback(payload, done) {
  models.User.findOne({
    where: {
      id: payload.id
    }
  })
  .then(user => {
    if (user) {
      return user.toJSON();
    }
    return null;
  })
  .asCallback(done);
}

I have tested the logout routes separate of the application:

const passportSaml = middleware.passportSaml(passport);

router.get('/', (req, res) => {
    try {
        const user = getCurrentUser(req);
        req.user = currentUser;
        passportSaml._strategy('saml').logout(req, (err, reqUrl) => {
            req.logout();
            res.redirect(reqUrl);
        });
    } catch (err) {
        return res.send(err.toString());
    }
});

router.get('/callback', (req, res) => {
    res.redirect('/');
});

In the server.js I call both strategies and authenticate based on routes:

middleware.passportJwt(passport);
middleware.passportSaml(passport);
app.use(passport.initialize());

router.use('/api/auth', passport.authenticate('saml', { failureRedirect: '/failure', failureFlash: true, session: false }), routes.samlRouter);
router.use('/api', passport.authenticate('jwt', { session: false }), routes.protectedRouter);

I would appreciate any help on everything and anything I'm doing wrong.

@sibelius
Copy link
Contributor

sibelius commented Oct 8, 2018

I think you are importing strategy in the wrong way

it should be

import { Strategy } from 'passport-saml'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants