0
votes

Has anyone found a way to use passport-saml with KoaJS? I've managed to identify the issue -- passport-saml uses res.send, which isn't available in koa, but I'm not having any luck finding a way around it so far..

1

1 Answers

3
votes

I was wondering the same thing today and stumbled upon this post. There is no koa based passport-saml implementation, however, there is a neat middleware module called koa-passport that is backwards compatible with older passport strategies like passport-saml. It uses a mock express request object to bridge some of the gaps you encounter with these express based middlewares.

Here's a snippet of the setup (full sample here on github):

// passport.js
const Saml    = require('passport-saml').Strategy,
      passport= require('koa-passport');

passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));

passport.use(new Saml(config, (profile, done) => done(null, profile) });
module.exports = passport;

Login and Callback routes

// app.js
const app = Koa();

app.keys = ['abc'];

app.use(session({key: 'test.cookie'}));
app.use(passport.initialize());
app.use(passport.session());

router.get('/login', passport.authenticate('saml',
    {
        successRedirect: '/',
        failureRedirect: '/login'
    })
);

router.post('/login/callback', passport.authenticate('saml',
    {
        failureRedirect: '/',
        failureFlash: true
    }),
    function *consume() {
        this.redirect('/');
    }
);