|
|
@@ -78,8 +78,9 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- const provider = 'ldap';
|
|
|
- const ldapAccountInfo = await promisifiedPassportAuthentication(req, res, next, provider);
|
|
|
+ const providerId = 'ldap';
|
|
|
+ const strategyName = 'ldapauth';
|
|
|
+ const ldapAccountInfo = await promisifiedPassportAuthentication(req, res, next, providerId, strategyName);
|
|
|
|
|
|
/*
|
|
|
* authentication success
|
|
|
@@ -97,7 +98,7 @@ module.exports = function(crowi, app) {
|
|
|
'name': nameToBeRegistered
|
|
|
}
|
|
|
|
|
|
- const externalAccount = await getOrCreateUser(req, res, next, userInfo, provider);
|
|
|
+ const externalAccount = await getOrCreateUser(req, res, next, userInfo, providerId);
|
|
|
const user = await externalAccount.getPopulatedUser();
|
|
|
|
|
|
// login
|
|
|
@@ -207,14 +208,15 @@ module.exports = function(crowi, app) {
|
|
|
};
|
|
|
|
|
|
const loginPassportGoogleCallback = async(req, res, next) => {
|
|
|
- const provider = 'google';
|
|
|
- const response = await promisifiedPassportAuthentication(req, res, next, provider);
|
|
|
+ const providerId = 'google';
|
|
|
+ const strategyName = 'google';
|
|
|
+ const response = await promisifiedPassportAuthentication(req, res, next, providerId, strategyName);
|
|
|
const userInfo = {
|
|
|
'id': response.id,
|
|
|
'username': response.displayName,
|
|
|
'name': `${response.name.givenName} ${response.name.familyName}`
|
|
|
}
|
|
|
- const externalAccount = await getOrCreateUser(req, res, next, userInfo, provider);
|
|
|
+ const externalAccount = await getOrCreateUser(req, res, next, userInfo, providerId);
|
|
|
const user = await externalAccount.getPopulatedUser();
|
|
|
|
|
|
// login
|
|
|
@@ -224,12 +226,11 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
- const promisifiedPassportAuthentication = (req, res, next, provider) => {
|
|
|
- if(provider === 'ldap'){ provider = 'ldapauth' };
|
|
|
+ const promisifiedPassportAuthentication = (req, res, next, providerId, strategyName) => {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- passport.authenticate(provider, (err, response, info) => {
|
|
|
+ passport.authenticate(strategyName, (err, response, info) => {
|
|
|
if (err) {
|
|
|
- if (provider === 'ldap'){
|
|
|
+ if (providerId === 'ldap'){
|
|
|
if (res.headersSent) { // dirty hack -- 2017.09.25
|
|
|
return; // cz: somehow passport.authenticate called twice when ECONNREFUSED error occurred
|
|
|
}
|
|
|
@@ -260,11 +261,11 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
- const getOrCreateUser = async(req, res, next, userInfo, provider) => {
|
|
|
+ const getOrCreateUser = async(req, res, next, userInfo, providerId) => {
|
|
|
try {
|
|
|
// find or register(create) user
|
|
|
const externalAccount = await ExternalAccount.findOrRegister(
|
|
|
- provider,
|
|
|
+ providerId,
|
|
|
userInfo.id,
|
|
|
userInfo.username,
|
|
|
userInfo.name
|
|
|
@@ -274,14 +275,14 @@ module.exports = function(crowi, app) {
|
|
|
catch (err) {
|
|
|
if (err.name === 'DuplicatedUsernameException') {
|
|
|
// get option
|
|
|
- const isSameUsernameTreatedAsIdenticalUser = Config.isSameUsernameTreatedAsIdenticalUser(config, provider);
|
|
|
+ const isSameUsernameTreatedAsIdenticalUser = Config.isSameUsernameTreatedAsIdenticalUser(config, providerId);
|
|
|
if (isSameUsernameTreatedAsIdenticalUser) {
|
|
|
// associate to existing user
|
|
|
debug(`ExternalAccount '${userInfo.username}' will be created and bound to the exisiting User account`);
|
|
|
- return ExternalAccount.associate(provider, userInfo.id, err.user);
|
|
|
+ return ExternalAccount.associate(providerId, userInfo.id, err.user);
|
|
|
}
|
|
|
else {
|
|
|
- req.flash('provider-DuplicatedUsernameException', provider);
|
|
|
+ req.flash('provider-DuplicatedUsernameException', providerId);
|
|
|
return loginFailure(req, res, next);
|
|
|
}
|
|
|
}
|