|
|
@@ -1,5 +1,7 @@
|
|
|
|
|
|
import { ErrorV3 } from '@growi/core';
|
|
|
+import createError from 'http-errors';
|
|
|
+import next from 'next';
|
|
|
|
|
|
import { SupportedAction } from '~/interfaces/activity';
|
|
|
import { LoginErrorCode } from '~/interfaces/errors/login-error';
|
|
|
@@ -166,6 +168,13 @@ module.exports = function(crowi, app) {
|
|
|
return res.apiv3Err(error);
|
|
|
};
|
|
|
|
|
|
+ const loginFailureForExternalAccount = (error, req, res, next) => {
|
|
|
+ const { nextApp } = crowi;
|
|
|
+ req.crowi = crowi;
|
|
|
+ nextApp.render(req, res, '/login', { loginError: error });
|
|
|
+ return;
|
|
|
+ };
|
|
|
+
|
|
|
/**
|
|
|
* return true(valid) or false(invalid)
|
|
|
*
|
|
|
@@ -359,8 +368,8 @@ module.exports = function(crowi, app) {
|
|
|
const loginWithGoogle = function(req, res, next) {
|
|
|
if (!passportService.isGoogleStrategySetup) {
|
|
|
debug('GoogleStrategy has not been set up');
|
|
|
- req.flash('warningMessage', req.t('message.strategy_has_not_been_set_up', { strategy: 'GoogleStrategy' }));
|
|
|
- return next();
|
|
|
+ const error = new ErrorV3('message.strategy_has_not_been_set_up', undefined, undefined, { strategy: 'GoogleStrategy' });
|
|
|
+ return next(error);
|
|
|
}
|
|
|
|
|
|
passport.authenticate('google', {
|
|
|
@@ -379,7 +388,7 @@ module.exports = function(crowi, app) {
|
|
|
response = await promisifiedPassportAuthentication(strategyName, req, res);
|
|
|
}
|
|
|
catch (err) {
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3(err.message));
|
|
|
}
|
|
|
|
|
|
let name;
|
|
|
@@ -413,14 +422,14 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
const externalAccount = await getOrCreateUser(req, res, userInfo, providerId);
|
|
|
if (!externalAccount) {
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3('message.sign_in_failure'));
|
|
|
}
|
|
|
|
|
|
const user = await externalAccount.getPopulatedUser();
|
|
|
|
|
|
// login
|
|
|
req.logIn(user, async(err) => {
|
|
|
- if (err) { debug(err.message); return next() }
|
|
|
+ if (err) { debug(err.message); return next(ErrorV3(err.message)) }
|
|
|
|
|
|
return loginSuccessHandler(req, res, user, SupportedAction.ACTION_USER_LOGIN_WITH_GOOGLE, true);
|
|
|
});
|
|
|
@@ -429,8 +438,8 @@ module.exports = function(crowi, app) {
|
|
|
const loginWithGitHub = function(req, res, next) {
|
|
|
if (!passportService.isGitHubStrategySetup) {
|
|
|
debug('GitHubStrategy has not been set up');
|
|
|
- req.flash('warningMessage', req.t('message.strategy_has_not_been_set_up', { strategy: 'GitHubStrategy' }));
|
|
|
- return next();
|
|
|
+ const error = new ErrorV3('message.strategy_has_not_been_set_up', undefined, undefined, { strategy: 'GitHubStrategy' });
|
|
|
+ return next(error);
|
|
|
}
|
|
|
|
|
|
passport.authenticate('github')(req, res);
|
|
|
@@ -445,7 +454,7 @@ module.exports = function(crowi, app) {
|
|
|
response = await promisifiedPassportAuthentication(strategyName, req, res);
|
|
|
}
|
|
|
catch (err) {
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3(err.message));
|
|
|
}
|
|
|
|
|
|
const userInfo = {
|
|
|
@@ -456,14 +465,14 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
const externalAccount = await getOrCreateUser(req, res, userInfo, providerId);
|
|
|
if (!externalAccount) {
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3('message.sign_in_failure'));
|
|
|
}
|
|
|
|
|
|
const user = await externalAccount.getPopulatedUser();
|
|
|
|
|
|
// login
|
|
|
req.logIn(user, async(err) => {
|
|
|
- if (err) { debug(err.message); return next() }
|
|
|
+ if (err) { debug(err.message); return next(ErrorV3(err.message)) }
|
|
|
|
|
|
return loginSuccessHandler(req, res, user, SupportedAction.ACTION_USER_LOGIN_WITH_GITHUB, true);
|
|
|
});
|
|
|
@@ -472,8 +481,8 @@ module.exports = function(crowi, app) {
|
|
|
const loginWithTwitter = function(req, res, next) {
|
|
|
if (!passportService.isTwitterStrategySetup) {
|
|
|
debug('TwitterStrategy has not been set up');
|
|
|
- req.flash('warningMessage', req.t('message.strategy_has_not_been_set_up', { strategy: 'TwitterStrategy' }));
|
|
|
- return next();
|
|
|
+ const error = new ErrorV3('message.strategy_has_not_been_set_up', undefined, undefined, { strategy: 'TwitterStrategy' });
|
|
|
+ return next(error);
|
|
|
}
|
|
|
|
|
|
passport.authenticate('twitter')(req, res);
|
|
|
@@ -488,7 +497,7 @@ module.exports = function(crowi, app) {
|
|
|
response = await promisifiedPassportAuthentication(strategyName, req, res);
|
|
|
}
|
|
|
catch (err) {
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3(err.message));
|
|
|
}
|
|
|
|
|
|
const userInfo = {
|
|
|
@@ -499,14 +508,14 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
const externalAccount = await getOrCreateUser(req, res, userInfo, providerId);
|
|
|
if (!externalAccount) {
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3('message.sign_in_failure'));
|
|
|
}
|
|
|
|
|
|
const user = await externalAccount.getPopulatedUser();
|
|
|
|
|
|
// login
|
|
|
req.logIn(user, async(err) => {
|
|
|
- if (err) { debug(err.message); return next() }
|
|
|
+ if (err) { debug(err.message); return next(ErrorV3(err.message)) }
|
|
|
|
|
|
return loginSuccessHandler(req, res, user, SupportedAction.ACTION_USER_LOGIN_WITH_TWITTER, true);
|
|
|
});
|
|
|
@@ -515,8 +524,8 @@ module.exports = function(crowi, app) {
|
|
|
const loginWithOidc = function(req, res, next) {
|
|
|
if (!passportService.isOidcStrategySetup) {
|
|
|
debug('OidcStrategy has not been set up');
|
|
|
- req.flash('warningMessage', req.t('message.strategy_has_not_been_set_up', { strategy: 'OidcStrategy' }));
|
|
|
- return next();
|
|
|
+ const error = new ErrorV3('message.strategy_has_not_been_set_up', undefined, undefined, { strategy: 'OidcStrategy' });
|
|
|
+ return next(error);
|
|
|
}
|
|
|
|
|
|
passport.authenticate('oidc')(req, res);
|
|
|
@@ -536,7 +545,7 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
|
catch (err) {
|
|
|
debug(err);
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3(err.message));
|
|
|
}
|
|
|
|
|
|
const userInfo = {
|
|
|
@@ -549,13 +558,13 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
const externalAccount = await getOrCreateUser(req, res, userInfo, providerId);
|
|
|
if (!externalAccount) {
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3('message.sign_in_failure'));
|
|
|
}
|
|
|
|
|
|
// login
|
|
|
const user = await externalAccount.getPopulatedUser();
|
|
|
req.logIn(user, async(err) => {
|
|
|
- if (err) { debug(err.message); return next() }
|
|
|
+ if (err) { debug(err.message); return next(ErrorV3(err.message)) }
|
|
|
|
|
|
return loginSuccessHandler(req, res, user, SupportedAction.ACTION_USER_LOGIN_WITH_OIDC, true);
|
|
|
});
|
|
|
@@ -564,8 +573,8 @@ module.exports = function(crowi, app) {
|
|
|
const loginWithSaml = function(req, res, next) {
|
|
|
if (!passportService.isSamlStrategySetup) {
|
|
|
debug('SamlStrategy has not been set up');
|
|
|
- req.flash('warningMessage', req.t('message.strategy_has_not_been_set_up', { strategy: 'SamlStrategy' }));
|
|
|
- return next();
|
|
|
+ const error = new ErrorV3('message.strategy_has_not_been_set_up', undefined, undefined, { strategy: 'SamlStrategy' });
|
|
|
+ return next(error);
|
|
|
}
|
|
|
|
|
|
passport.authenticate('saml')(req, res);
|
|
|
@@ -585,7 +594,7 @@ module.exports = function(crowi, app) {
|
|
|
response = await promisifiedPassportAuthentication(strategyName, req, res);
|
|
|
}
|
|
|
catch (err) {
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3(err.message));
|
|
|
}
|
|
|
|
|
|
const userInfo = {
|
|
|
@@ -603,12 +612,12 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
// Attribute-based Login Control
|
|
|
if (!crowi.passportService.verifySAMLResponseByABLCRule(response)) {
|
|
|
- return loginFailureHandler(req, res, 'Sign in failure due to insufficient privileges.');
|
|
|
+ return next(ErrorV3('Sign in failure due to insufficient privileges.'));
|
|
|
}
|
|
|
|
|
|
const externalAccount = await getOrCreateUser(req, res, userInfo, providerId);
|
|
|
if (!externalAccount) {
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3('message.sign_in_failure'));
|
|
|
}
|
|
|
|
|
|
const user = await externalAccount.getPopulatedUser();
|
|
|
@@ -617,7 +626,7 @@ module.exports = function(crowi, app) {
|
|
|
req.logIn(user, (err) => {
|
|
|
if (err != null) {
|
|
|
logger.error(err);
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3(err.message));
|
|
|
}
|
|
|
|
|
|
return loginSuccessHandler(req, res, user, SupportedAction.ACTION_USER_LOGIN_WITH_SAML, true);
|
|
|
@@ -633,8 +642,8 @@ module.exports = function(crowi, app) {
|
|
|
const loginWithBasic = async(req, res, next) => {
|
|
|
if (!passportService.isBasicStrategySetup) {
|
|
|
debug('BasicStrategy has not been set up');
|
|
|
- req.flash('warningMessage', req.t('message.strategy_has_not_been_set_up', { strategy: 'Basic' }));
|
|
|
- return next();
|
|
|
+ const error = new ErrorV3('message.strategy_has_not_been_set_up', undefined, undefined, { strategy: 'Basic' });
|
|
|
+ return next(error);
|
|
|
}
|
|
|
|
|
|
const providerId = 'basic';
|
|
|
@@ -645,7 +654,7 @@ module.exports = function(crowi, app) {
|
|
|
userId = await promisifiedPassportAuthentication(strategyName, req, res);
|
|
|
}
|
|
|
catch (err) {
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3('message.sign_in_failure'));
|
|
|
}
|
|
|
|
|
|
const userInfo = {
|
|
|
@@ -656,12 +665,12 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
const externalAccount = await getOrCreateUser(req, res, userInfo, providerId);
|
|
|
if (!externalAccount) {
|
|
|
- return loginFailureHandler(req, res);
|
|
|
+ return next(ErrorV3('message.sign_in_failure'));
|
|
|
}
|
|
|
|
|
|
const user = await externalAccount.getPopulatedUser();
|
|
|
await req.logIn(user, (err) => {
|
|
|
- if (err) { debug(err.message); return next() }
|
|
|
+ if (err) { debug(err.message); return next(ErrorV3(err.message)) }
|
|
|
|
|
|
return loginSuccessHandler(req, res, user, SupportedAction.ACTION_USER_LOGIN_WITH_BASIC, true);
|
|
|
});
|
|
|
@@ -670,6 +679,7 @@ module.exports = function(crowi, app) {
|
|
|
return {
|
|
|
cannotLoginErrorHadnler,
|
|
|
loginFailure,
|
|
|
+ loginFailureForExternalAccount,
|
|
|
loginWithLdap,
|
|
|
testLdapCredentials,
|
|
|
loginWithLocal,
|