From dadcad95462316869d000193a13c71df7546166e Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Wed, 12 Oct 2016 14:21:23 -0500 Subject: [PATCH 1/2] Add authentication from Auth0 --- app/auth-auth0.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 app/auth-auth0.js diff --git a/app/auth-auth0.js b/app/auth-auth0.js new file mode 100644 index 0000000..088c4b1 --- /dev/null +++ b/app/auth-auth0.js @@ -0,0 +1,54 @@ +/** + * Copyright (c) Jupyter Development Team. + * Distributed under the terms of the Modified BSD License. + */ +/** + * Example: Passport JS strategy for Auth0. + * Requires additional configuration and npm packages. See + * https://github.com/jupyter-incubator/dashboards_server/wiki/Authentication + * for details. + */ +var config = require('./config'); +var passport = require('passport'); +var Auth0Strategy = require('passport-auth0'); + +module.exports = function(app) { + + app.get('/callback', + passport.authenticate('auth0', { failureRedirect: '/login' }), + function(req, res) { + if (!req.user) { + throw new Error('user null'); + } + res.redirect("/"); + } + ); + + app.get('/login', + passport.authenticate('auth0', {}), function (req, res) { + res.redirect("/"); + }); + + app.post('/logout', function(req, res){ + req.logout(); + res.redirect('https://' + config.get('AUTH0_DOMAIN') + '/v2/logout?returnTo=' + config.get('PUBLIC_LINK_PATTERN') + '&client_id=' + config.get('AUTH0_CLIENT_ID')); + }); + + var strategy = new Auth0Strategy({ + domain: config.get('AUTH0_DOMAIN'), + clientID: config.get('AUTH0_CLIENT_ID'), + clientSecret: config.get('AUTH0_CLIENT_SECRET'), + callbackURL: config.get('AUTH0_CALLBACK_URL') + }, + function(accessToken, refreshToken, extraParams, profile, done) { + // accessToken is the token to call Auth0 API (not needed in the most cases) + // extraParams.id_token has the JSON Web Token + // profile has all the information from the user + return done(null, profile); + } + ); + + // passport.use(strategy); + + return strategy; +}; From 9f06c7a226af755a86188e1edc89f8091e8464df Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Mon, 17 Oct 2016 10:39:49 -0500 Subject: [PATCH 2/2] Use url.format --- app/auth-auth0.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/auth-auth0.js b/app/auth-auth0.js index 088c4b1..38506ff 100644 --- a/app/auth-auth0.js +++ b/app/auth-auth0.js @@ -8,6 +8,7 @@ * https://github.com/jupyter-incubator/dashboards_server/wiki/Authentication * for details. */ +var url = require('url'); var config = require('./config'); var passport = require('passport'); var Auth0Strategy = require('passport-auth0'); @@ -18,7 +19,7 @@ module.exports = function(app) { passport.authenticate('auth0', { failureRedirect: '/login' }), function(req, res) { if (!req.user) { - throw new Error('user null'); + throw new Error('User name must be set'); } res.redirect("/"); } @@ -31,7 +32,16 @@ module.exports = function(app) { app.post('/logout', function(req, res){ req.logout(); - res.redirect('https://' + config.get('AUTH0_DOMAIN') + '/v2/logout?returnTo=' + config.get('PUBLIC_LINK_PATTERN') + '&client_id=' + config.get('AUTH0_CLIENT_ID')); + var logout_obj_url = { + host: config.get('AUTH0_DOMAIN'), + pathname: '/v2/logout', + query: { + 'returnTo': config.get('PUBLIC_LINK_PATTERN'), + 'client_id': config.get('AUTH0_CLIENT_ID'), + } + }; + var logout_url = url.format(logout_obj_url); + res.redirect(logout_url); }); var strategy = new Auth0Strategy({