feat: use mock authentication for local dev
This commit is contained in:
committed by
mrugesh mohapatra
parent
b734f5033d
commit
f0c8211e95
29
api-server/package-lock.json
generated
29
api-server/package-lock.json
generated
@ -1064,6 +1064,15 @@
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-7.0.70.tgz",
|
||||
"integrity": "sha512-bAcW/1aM8/s5iFKhRpu/YJiQf/b1ZwnMRqqsWRCmAqEDQF2zY8Ez3Iu9AcZKFKc3vCJc8KJVpJ6Pn54sJ1BvXQ=="
|
||||
},
|
||||
"@types/passport": {
|
||||
"version": "0.4.6",
|
||||
"resolved": "https://registry.npmjs.org/@types/passport/-/passport-0.4.6.tgz",
|
||||
"integrity": "sha512-P7TxrdpAze3nvHghYPeLlHkYcFDiIkRBbp7xYz2ehX9zmi1yr/qWQMTpXsMxN5w3ESJpMzn917inK4giASaDcQ==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@types/express": "*"
|
||||
}
|
||||
},
|
||||
"@types/range-parser": {
|
||||
"version": "1.2.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.2.tgz",
|
||||
@ -10377,6 +10386,26 @@
|
||||
"passport-strategy": "1.x.x"
|
||||
}
|
||||
},
|
||||
"passport-mock-strategy": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/passport-mock-strategy/-/passport-mock-strategy-1.1.1.tgz",
|
||||
"integrity": "sha512-QSJoC2JB2piLVB3CIj0EaV3V8kfThN+dS7d34KNzcJaL03pYyFvfEl9Im8O8wTb+OZcmqWz6FAqUpobd1+IVNg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@types/express": "^4.11.1",
|
||||
"@types/passport": "^0.4.5",
|
||||
"es6-promise": "^4.2.4",
|
||||
"passport": "^0.4.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"es6-promise": {
|
||||
"version": "4.2.5",
|
||||
"resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.5.tgz",
|
||||
"integrity": "sha512-n6wvpdE43VFtJq+lUDYDBFUwV8TZbuGXLV4D6wKafg13ldznKsyEvatubnmUe31zcvelSzOHF+XbaT+Bl9ObDg==",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"passport-oauth": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/passport-oauth/-/passport-oauth-1.0.0.tgz",
|
||||
|
@ -98,6 +98,7 @@
|
||||
"joi-objectid": "^2.0.0",
|
||||
"loopback-component-explorer": "^6.3.1",
|
||||
"nodemon": "^1.18.4",
|
||||
"passport-mock-strategy": "^1.1.1",
|
||||
"pm2": "^3.0.3",
|
||||
"prettier": "^1.14.2",
|
||||
"sinon": "^7.1.1",
|
||||
|
@ -7,7 +7,11 @@ import { check } from 'express-validator/check';
|
||||
|
||||
import { homeLocation } from '../../../config/env';
|
||||
import { createCookieConfig } from '../utils/cookieConfig';
|
||||
import { createPassportCallbackAuthenticator } from '../component-passport';
|
||||
import {
|
||||
createPassportCallbackAuthenticator,
|
||||
saveResponseAuthCookies,
|
||||
loginRedirect
|
||||
} from '../component-passport';
|
||||
import {
|
||||
ifUserRedirectTo,
|
||||
ifNoUserRedirectTo,
|
||||
@ -25,15 +29,34 @@ module.exports = function enableAuthentication(app) {
|
||||
// loopback.io/doc/en/lb2/Authentication-authorization-and-permissions.html
|
||||
app.enableAuth();
|
||||
const ifUserRedirect = ifUserRedirectTo();
|
||||
const saveAuthCookies = saveResponseAuthCookies();
|
||||
const loginSuccessRedirect = loginRedirect();
|
||||
const ifNoUserRedirectHome = ifNoUserRedirectTo(homeLocation);
|
||||
const api = app.loopback.Router();
|
||||
const { AuthToken, User } = app.models;
|
||||
|
||||
api.get('/signin', ifUserRedirect, passport.authenticate('auth0-login', {}));
|
||||
api.get(
|
||||
'/auth/auth0/callback',
|
||||
createPassportCallbackAuthenticator('auth0-login', { provider: 'auth0' })
|
||||
);
|
||||
// Use a local mock strategy for signing in if we are in dev mode.
|
||||
// Otherwise we use auth0 login. We use a string for 'true' because values
|
||||
// set in the env file will always be strings and never boolean.
|
||||
if (process.env.LOCAL_MOCK_AUTH === 'true') {
|
||||
api.get(
|
||||
'/signin',
|
||||
passport.authenticate('devlogin'),
|
||||
saveAuthCookies,
|
||||
loginSuccessRedirect
|
||||
);
|
||||
} else {
|
||||
api.get(
|
||||
'/signin',
|
||||
ifUserRedirect,
|
||||
passport.authenticate('auth0-login', {})
|
||||
);
|
||||
|
||||
api.get(
|
||||
'/auth/auth0/callback',
|
||||
createPassportCallbackAuthenticator('auth0-login', { provider: 'auth0' })
|
||||
);
|
||||
}
|
||||
|
||||
api.get('/signout', (req, res) => {
|
||||
req.logout();
|
||||
|
@ -131,6 +131,50 @@ export function setupPassport(app) {
|
||||
});
|
||||
}
|
||||
|
||||
export const saveResponseAuthCookies = () => {
|
||||
|
||||
return (req, res, next) => {
|
||||
|
||||
const user = req.user;
|
||||
|
||||
if (!user) {
|
||||
return res.redirect('/signin');
|
||||
}
|
||||
|
||||
const { accessToken } = user;
|
||||
|
||||
const cookieConfig = {
|
||||
...createCookieConfig(req),
|
||||
maxAge: 77760000000
|
||||
};
|
||||
const jwtAccess = jwt.sign({ accessToken }, jwtSecret);
|
||||
res.cookie('jwt_access_token', jwtAccess, cookieConfig);
|
||||
res.cookie('access_token', accessToken.id, cookieConfig);
|
||||
res.cookie('userId', accessToken.userId, cookieConfig);
|
||||
|
||||
return next();
|
||||
};
|
||||
};
|
||||
|
||||
export const loginRedirect = () => {
|
||||
|
||||
return (req, res) => {
|
||||
const successRedirect = req => {
|
||||
if (!!req && req.session && req.session.returnTo) {
|
||||
delete req.session.returnTo;
|
||||
return `${homeLocation}/welcome`;
|
||||
}
|
||||
return `${homeLocation}/welcome`;
|
||||
};
|
||||
|
||||
let redirect = url.parse(successRedirect(req), true);
|
||||
delete redirect.search;
|
||||
|
||||
redirect = url.format(redirect);
|
||||
return res.redirect(redirect);
|
||||
};
|
||||
};
|
||||
|
||||
export const createPassportCallbackAuthenticator = (strategy, config) => (
|
||||
req,
|
||||
res,
|
||||
|
@ -7,6 +7,11 @@ const successRedirect = `${homeLocation}/welcome`;
|
||||
const failureRedirect = '/signin';
|
||||
|
||||
export default {
|
||||
devlogin: {
|
||||
authScheme: 'mock',
|
||||
provider: 'dev',
|
||||
module: 'passport-mock-strategy'
|
||||
},
|
||||
local: {
|
||||
provider: 'local',
|
||||
module: 'passport-local',
|
||||
|
Reference in New Issue
Block a user