Преглед изворни кода

GoogleStrategy: setting up..

sou пре 7 година
родитељ
комит
fe91b1704a
4 измењених фајлова са 91 додато и 17 уклоњено
  1. 1 1
      lib/crowi/index.js
  2. 6 0
      lib/models/config.js
  3. 16 16
      lib/routes/admin.js
  4. 68 0
      lib/service/passport.js

+ 1 - 1
lib/crowi/index.js

@@ -263,7 +263,7 @@ Crowi.prototype.setupPassport = function() {
   // setup strategies
   // setup strategies
   this.passportService.setupLocalStrategy();
   this.passportService.setupLocalStrategy();
   this.passportService.setupLdapStrategy();
   this.passportService.setupLdapStrategy();
-
+  this.passportService.setupGoogleStrategy();
   return Promise.resolve();
   return Promise.resolve();
 };
 };
 
 

+ 6 - 0
lib/models/config.js

@@ -64,6 +64,7 @@ module.exports = function(crowi) {
       'security:passport-ldap:groupSearchFilter' : undefined,
       'security:passport-ldap:groupSearchFilter' : undefined,
       'security:passport-ldap:groupDnProperty' : undefined,
       'security:passport-ldap:groupDnProperty' : undefined,
       'security:passport-ldap:isSameUsernameTreatedAsIdenticalUser': false,
       'security:passport-ldap:isSameUsernameTreatedAsIdenticalUser': false,
+      'security:passport-google:isEnabled' : false,
 
 
       'aws:bucket'          : 'growi',
       'aws:bucket'          : 'growi',
       'aws:region'          : 'ap-northeast-1',
       'aws:region'          : 'ap-northeast-1',
@@ -267,6 +268,11 @@ module.exports = function(crowi) {
     return getValueForCrowiNS(config, key);
     return getValueForCrowiNS(config, key);
   };
   };
 
 
+  configSchema.statics.isEnabledPassportGoogle = function(config) {
+    const key = 'security:passport-google:isEnabled';
+    return getValueForCrowiNS(config, key);
+  };
+
   configSchema.statics.isSameUsernameTreatedAsIdenticalUser = function(config, providerType) {
   configSchema.statics.isSameUsernameTreatedAsIdenticalUser = function(config, providerType) {
     const key = `security:passport-${providerType}:isSameUsernameTreatedAsIdenticalUser`;
     const key = `security:passport-${providerType}:isSameUsernameTreatedAsIdenticalUser`;
     return getValueForCrowiNS(config, key);
     return getValueForCrowiNS(config, key);

+ 16 - 16
lib/routes/admin.js

@@ -909,23 +909,23 @@ module.exports = function(crowi, app) {
     if (!req.form.isValid) {
     if (!req.form.isValid) {
       return res.json({status: false, message: req.form.errors.join('\n')});
       return res.json({status: false, message: req.form.errors.join('\n')});
     }
     }
-    console.log(form);
+
     debug('form content', form);
     debug('form content', form);
-    // return saveSettingAsync(form)
-    //   .then(() => {
-    //     const config = crowi.getConfig();
-
-    //     // reset strategy
-    //     crowi.passportService.resetGoogleStrategy();
-    //     // setup strategy
-    //     if (Config.isEnabledPassportGoogle(config)) {
-    //       crowi.passportService.setupGoogleStrategy(true);
-    //     }
-    //     return;
-    //   })
-    //   .then(() => {
-    //     res.json({status: true});
-    //   });
+    return saveSettingAsync(form)
+      .then(() => {
+        const config = crowi.getConfig();
+
+        // reset strategy
+        crowi.passportService.resetGoogleStrategy();
+        // setup strategy
+        if (Config.isEnabledPassportGoogle(config)) {
+          crowi.passportService.setupGoogleStrategy(true);
+        }
+        return;
+      })
+      .then(() => {
+        res.json({status: true});
+      });
   };
   };
 
 
   actions.api.customizeSetting = function(req, res) {
   actions.api.customizeSetting = function(req, res) {

+ 68 - 0
lib/service/passport.js

@@ -2,6 +2,7 @@ const debug = require('debug')('growi:service:PassportService');
 const passport = require('passport');
 const passport = require('passport');
 const LocalStrategy = require('passport-local').Strategy;
 const LocalStrategy = require('passport-local').Strategy;
 const LdapStrategy = require('passport-ldapauth');
 const LdapStrategy = require('passport-ldapauth');
+const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
 
 
 /**
 /**
  * the service class of Passport
  * the service class of Passport
@@ -25,6 +26,11 @@ class PassportService {
      */
      */
     this.isLdapStrategySetup = false;
     this.isLdapStrategySetup = false;
 
 
+    /**
+     * the flag whether LdapStrategy is set up successfully
+     */
+    this.isGoogleStrategySetup = false;
+
     /**
     /**
      * the flag whether serializer/deserializer are set up successfully
      * the flag whether serializer/deserializer are set up successfully
      */
      */
@@ -235,6 +241,68 @@ class PassportService {
     };
     };
   }
   }
 
 
+  /**
+   * Asynchronous configuration retrieval
+   *
+   * @memberof PassportService
+   */
+  setupGoogleStrategy() {
+    // check whether the strategy has already been set up
+    if (this.isGoogleStrategySetup) {
+      throw new Error('GoogleStrategy has already been set up');
+    }
+
+    const config = this.crowi.config;
+    const Config = this.crowi.model('Config');
+    //this
+    const isGoogleEnabled = Config.isEnabledPassportGoogle(config);
+
+    // when disabled
+    if (!isGoogleEnabled) {
+      return;
+    }
+
+    debug('GoogleStrategy: setting up..');
+
+    // passport.use(new LdapStrategy(this.getLdapConfigurationFunc(config, {passReqToCallback: true}),
+    //   (req, ldapAccountInfo, done) => {
+    //     debug('LDAP authentication has succeeded', ldapAccountInfo);
+
+    //     // store ldapAccountInfo to req
+    //     req.ldapAccountInfo = ldapAccountInfo;
+
+    //     done(null, ldapAccountInfo);
+    //   }
+    // ));
+    console.log('rere')
+    passport.use(new GoogleStrategy({
+      clientID: config.crowi['google:clientId'],
+      clientSecret: config.crowi['google:clientSecret'],
+      callbackURL: '/auth/passport/google/callback',
+    },
+    function(accessToken, refreshToken, profile, done) {
+      console.log(profile)
+      // User.findOrCreate({ googleId: profile.id }, function(err, user) {
+      //   return done(err, user);
+      // });
+    }
+    ));
+
+    this.isGoogleStrategySetup = true;
+    debug('GoogleStrategy: setup is done');
+  }
+
+  /**
+   * reset GoogleStrategy
+   *
+   * @memberof PassportService
+   */
+  resetGoogleStrategy() {
+    debug('GoogleStrategy: reset');
+    passport.unuse('google');
+    this.isGoogleStrategySetup = false;
+  }
+
   /**
   /**
    * setup serializer and deserializer
    * setup serializer and deserializer
    *
    *