Skip to content

Instantly share code, notes, and snippets.

@geovanisouza92
Last active February 6, 2022 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geovanisouza92/75a0cdd2d1e7e1595e5f2ad78b10f88f to your computer and use it in GitHub Desktop.
Save geovanisouza92/75a0cdd2d1e7e1595e5f2ad78b10f88f to your computer and use it in GitHub Desktop.
Express with Passport (local, facebook, google, jwt)
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const { Strategy: JwtStrategy, ExtractJwt } = require('passport-jwt');
const FacebookStrategy = require('passport-facebook');
const GoogleStrategy = require('passport-google-oidc');
const { MultiSamlStrategy } = require('passport-saml');
const app = express();
app.use(cookieParser());
app.use(bodyParser.json());
passport.use(new LocalStrategy(function verify(user, password, cb) {
// TODO check user/password against DB
}));
passport.use(new FacebookStrategy({
clientID: '',
clientSecret: '',
callbackURL: environment.apiUrl + '/auth/callback/facebook',
}, function verify(accessToken, refreshToken, profile, cb) {
// TODO check profile.id against DB
}));
passport.use(new GoogleStrategy({
clientID: '',
clientSecret: '',
callbackURL: environment.apiUrl + '/auth/callback/google',
}, function verify(accessToken, refreshToken, profile, cb) {
// TODO check profile.id against DB
}));
passport.use(new MultiSamlStrategy({
path: environment.apiUrl + '/auth/callback/saml',
providerName: '<app name>',
passReqToCallback: true,
getSamlOptions(req, done) {
// TODO get saml options for req from DB
done(null, {
issuer: 'cc:123',
entryPoint: 'https://idp.example.com/idp/profile/SAML2/Redirect/SSO',
});
},
}, function verify(req, profile, done) {
// TODO check profile.id against DB
}));
app.get('/auth/session', (req, res) => {
// TODO Return session data
});
app.post('/auth/signout', (req, res) => {
req.logout();
// TODO: Clear token cookie
res.redirect('/');
});
app.post('/auth/signin/local', /* csurf, */ passport.authenticate('local'));
app.get('/auth/signin/facebook', passport.authenticate('facebook'));
app.get('/auth/callback/facebook', passport.authenticate('facebook'));
app.get('/auth/signin/google', passport.authenticate('google'));
app.get('/auth/callback/google', passport.authenticate('google'));
app.get('/auth/signin/saml', passport.authenticate('saml'));
app.post('/auth/callback/saml', bodyParser.urlencoded({ extended: true }), passport.authenticate('saml'));
passport.use(new JwtStrategy({
secretOrKey: secret,
jwtFromRequest: ExtractJwt.fromExtractors([
function fromSecureCookies(req) {
return req?.secureCookies?.token;
},
ExtractJwt.fromAuthHeaderAsBearerToken(),
ExtractJwt.fromUrlQueryParameter('token'),
]),
}, function verify(payload, done) {
// payload.iss = 'cc:123';
// payload.sub = 'cc:123:users/456';
done(null, payload);
}));
app.use(passport.authenticate('jwt'));
// other routes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment