Explorar o código

Merge pull request #412 from weseek/imprv/406-show-ldap-logs

Imprv/406 show ldap logs
Yuki Takei %!s(int64=7) %!d(string=hai) anos
pai
achega
95ce78c7f6

+ 12 - 6
lib/routes/login-passport.js

@@ -2,6 +2,7 @@ module.exports = function(crowi, app) {
   'use strict';
   'use strict';
 
 
   var debug = require('debug')('growi:routes:login-passport')
   var debug = require('debug')('growi:routes:login-passport')
+    , logger = require('@alias/logger')('growi:routes:login-passport')
     , passport = require('passport')
     , passport = require('passport')
     , config = crowi.getConfig()
     , config = crowi.getConfig()
     , Config = crowi.model('Config')
     , Config = crowi.model('Config')
@@ -18,7 +19,7 @@ module.exports = function(crowi, app) {
     // update lastLoginAt
     // update lastLoginAt
     user.updateLastLoginAt(new Date(), (err, userData) => {
     user.updateLastLoginAt(new Date(), (err, userData) => {
       if (err) {
       if (err) {
-        console.log(`updateLastLoginAt dumps error: ${err}`);
+        logger.error(`updateLastLoginAt dumps error: ${err}`);
         debug(`updateLastLoginAt dumps error: ${err}`);
         debug(`updateLastLoginAt dumps error: ${err}`);
       }
       }
     });
     });
@@ -71,8 +72,6 @@ module.exports = function(crowi, app) {
       return next();
       return next();
     }
     }
 
 
-    const loginForm = req.body.loginForm;
-
     if (!req.form.isValid) {
     if (!req.form.isValid) {
       debug('invalid form');
       debug('invalid form');
       return res.render('login', {
       return res.render('login', {
@@ -89,7 +88,7 @@ module.exports = function(crowi, app) {
       debug('info', info);
       debug('info', info);
 
 
       if (err) {  // DB Error
       if (err) {  // DB Error
-        console.log('LDAP Server Error: ', err);
+        logger.error('LDAP Server Error: ', err);
         req.flash('warningMessage', 'LDAP Server Error occured.');
         req.flash('warningMessage', 'LDAP Server Error occured.');
         return next(); // pass and the flash message is displayed when all of authentications are failed.
         return next(); // pass and the flash message is displayed when all of authentications are failed.
       }
       }
@@ -171,16 +170,19 @@ module.exports = function(crowi, app) {
       }
       }
 
 
       if (err) {  // DB Error
       if (err) {  // DB Error
-        console.log('LDAP Server Error: ', err);
+        logger.error('LDAP Server Error: ', err);
         return res.json({
         return res.json({
           status: 'warning',
           status: 'warning',
           message: 'LDAP Server Error occured.',
           message: 'LDAP Server Error occured.',
+          err
         });
         });
       }
       }
       if (info && info.message) {
       if (info && info.message) {
         return res.json({
         return res.json({
           status: 'warning',
           status: 'warning',
           message: info.message,
           message: info.message,
+          ldapConfiguration: req.ldapConfiguration,
+          ldapAccountInfo: req.ldapAccountInfo,
         });
         });
       }
       }
       if (user) {
       if (user) {
@@ -189,11 +191,15 @@ module.exports = function(crowi, app) {
           return res.json({
           return res.json({
             status: 'warning',
             status: 'warning',
             message: 'The user is found, but that has no groups.',
             message: 'The user is found, but that has no groups.',
+            ldapConfiguration: req.ldapConfiguration,
+            ldapAccountInfo: req.ldapAccountInfo,
           });
           });
         }
         }
         return res.json({
         return res.json({
           status: 'success',
           status: 'success',
           message: 'Successfully authenticated.',
           message: 'Successfully authenticated.',
+          ldapConfiguration: req.ldapConfiguration,
+          ldapAccountInfo: req.ldapAccountInfo,
         });
         });
       }
       }
     })(req, res, () => {});
     })(req, res, () => {});
@@ -217,7 +223,7 @@ module.exports = function(crowi, app) {
       debug('info', info);
       debug('info', info);
 
 
       if (err) {  // DB Error
       if (err) {  // DB Error
-        console.log('Database Server Error: ', err);
+        logger.error('Database Server Error: ', err);
         req.flash('warningMessage', 'Database Server Error occured.');
         req.flash('warningMessage', 'Database Server Error occured.');
         return next(); // pass and the flash message is displayed when all of authentications are failed.
         return next(); // pass and the flash message is displayed when all of authentications are failed.
       }
       }

+ 8 - 0
lib/service/passport.js

@@ -115,6 +115,10 @@ class PassportService {
     passport.use(new LdapStrategy(this.getLdapConfigurationFunc(config, {passReqToCallback: true}),
     passport.use(new LdapStrategy(this.getLdapConfigurationFunc(config, {passReqToCallback: true}),
       (req, ldapAccountInfo, done) => {
       (req, ldapAccountInfo, done) => {
         debug('LDAP authentication has succeeded', ldapAccountInfo);
         debug('LDAP authentication has succeeded', ldapAccountInfo);
+
+        // store ldapAccountInfo to req
+        req.ldapAccountInfo = ldapAccountInfo;
+
         done(null, ldapAccountInfo);
         done(null, ldapAccountInfo);
       }
       }
     ));
     ));
@@ -212,6 +216,10 @@ class PassportService {
           server: serverOpt,
           server: serverOpt,
         }, opts);
         }, opts);
         debug('ldap configuration: ', mergedOpts);
         debug('ldap configuration: ', mergedOpts);
+
+        // store configuration to req
+        req.ldapConfiguration = mergedOpts;
+
         callback(null, mergedOpts);
         callback(null, mergedOpts);
       });
       });
     };
     };

+ 1 - 1
lib/views/admin/widget/passport/ldap.html

@@ -311,7 +311,7 @@
 
 
       <div class="modal-body">
       <div class="modal-body">
 
 
-        {% include '../../../widget/passport/ldap-association-tester.html' %}
+        {% include '../../../widget/passport/ldap-association-tester.html' with { showLog: true } %}
 
 
       </div><!-- /.modal-body -->
       </div><!-- /.modal-body -->
 
 

+ 34 - 3
lib/views/widget/passport/ldap-association-tester.html

@@ -14,12 +14,20 @@
       </div>
       </div>
     </div>
     </div>
 
 
-    <div class="form-group">
-      <button type="button" class="btn btn-default col-xs-offset-5 col-xs-2" onclick="testLdapCredentials()">{{ t('Test') }}</button>
-    </div>
 
 
   </fieldset>
   </fieldset>
 
 
+  {% if showLog %}
+  <fieldset>
+    <h5>Logs</h5>
+    <textarea id="taLogs" class="col-xs-12" rows="4" readonly></textarea>
+  </fieldset>
+  {% endif %}
+
+  <fieldset class="mt-4">
+    <button type="button" class="btn btn-default col-xs-offset-5 col-xs-2" onclick="testLdapCredentials()">{{ t('Test') }}</button>
+  </fieldset>
+
   <script>
   <script>
     /**
     /**
      * test association (ajax)
      * test association (ajax)
@@ -44,6 +52,14 @@
           }, 5000);
           }, 5000);
         }
         }
       }
       }
+      /**
+       * add logs
+       */
+      function addLogs(formId, log) {
+        const textarea = $(`#${formId} #taLogs`);
+        const newLog = `${new Date()} - ${log}\n\n`;
+        textarea.val(`${newLog}${textarea.val()}`);
+      }
 
 
       var $form = $('#formTestLdapCredentials');
       var $form = $('#formTestLdapCredentials');
       var $action = '/_api/login/testLdap';
       var $action = '/_api/login/testLdap';
@@ -59,6 +75,21 @@
           else {
           else {
             showMessage($id, data.message, data.status);
             showMessage($id, data.message, data.status);
           }
           }
+
+          // add logs
+          if ('true' === '{{showLog}}') {
+            if (data.err) {
+              addLogs($id, data.err);
+            }
+            if (data.ldapConfiguration) {
+              const prettified = JSON.stringify(data.ldapConfiguration.server, undefined, 4);
+              addLogs($id, `LDAP Configuration : ${prettified}`);
+            }
+            if (data.ldapAccountInfo) {
+              const prettified = JSON.stringify(data.ldapAccountInfo, undefined, 4);
+              addLogs($id, `Retrieved LDAP Account : ${prettified}`);
+            }
+          }
         })
         })
         .fail(function() {
         .fail(function() {
           showMessage($id, 'エラーが発生しました', 'danger');
           showMessage($id, 'エラーが発生しました', 'danger');