sou 7 лет назад
Родитель
Сommit
f4c2f8c709
1 измененных файлов с 16 добавлено и 15 удалено
  1. 16 15
      lib/routes/login-passport.js

+ 16 - 15
lib/routes/login-passport.js

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