Browse Source

Update googleapis library version and change the behavior

Sotaro KARASAWA 9 years ago
parent
commit
6d7f3802fe
3 changed files with 29 additions and 18 deletions
  1. 6 0
      lib/routes/me.js
  2. 22 17
      lib/util/googleAuth.js
  3. 1 1
      package.json

+ 6 - 0
lib/routes/me.js

@@ -236,6 +236,12 @@ module.exports = function(crowi, app) {
         return res.redirect('/me');
       }
       userData.updateGoogleId(googleId, function(err, userData) {
+        if (err) {
+          debug('Failed to updateGoogleId', err);
+          req.flash('warningMessage.auth.google', 'Failed to connect Google Account');
+          return res.redirect('/me');
+        }
+
         // TODO if err
         req.flash('successMessage', 'Googleコネクトを設定しました。');
         return res.redirect('/me');

+ 22 - 17
lib/util/googleAuth.js

@@ -5,13 +5,13 @@
 module.exports = function(config) {
   'use strict';
 
-  var googleapis = require('googleapis')
+  var google = require('googleapis')
     , debug = require('debug')('crowi:lib:googleAuth')
     , lib = {}
     ;
 
   function createOauth2Client(url) {
-    return new googleapis.auth.OAuth2Client(
+    return new google.auth.OAuth2(
       config.crowi['google:clientId'],
       config.crowi['google:clientSecret'],
       url
@@ -20,11 +20,12 @@ module.exports = function(config) {
 
   lib.createAuthUrl = function(req, callback) {
     var callbackUrl = config.crowi['app:url'] + '/google/callback';
-    var google = createOauth2Client(callbackUrl);
+    var oauth2Client = createOauth2Client(callbackUrl);
+    google.options({auth: oauth2Client});
 
-    var redirectUrl = google.generateAuthUrl({
+    var redirectUrl = oauth2Client.generateAuthUrl({
       access_type: 'offline',
-      scope: 'https://www.googleapis.com/auth/userinfo.email',
+      scope: ['profile', 'email'],
     });
 
     callback(null, redirectUrl);
@@ -32,31 +33,35 @@ module.exports = function(config) {
 
   lib.handleCallback = function(req, callback) {
     var callbackUrl = config.crowi['app:url'] + '/google/callback';
-    var google = createOauth2Client(callbackUrl);
+    var oauth2Client = createOauth2Client(callbackUrl);
+    google.options({auth: oauth2Client});
+
     var code = req.session.googleAuthCode || null;
 
     if (!code) {
       return callback(new Error('No code exists.'), null);
     }
 
-    google.getToken(code, function(err, tokens) {
+    debug('Request googleToken by auth code', code);
+    oauth2Client.getToken(code, function(err, tokens) {
+      debug('Result of google.getToken()', err, tokens);
       if (err) {
         return callback(new Error('[googleAuth.handleCallback] Error to get token.'), null);
       }
 
-      googleapis.discover('oauth2', 'v1').withOpts({cache: { path: __dirname + '/../../tmp/googlecache'}}).execute(function(err, client) {
+      oauth2Client.setCredentials({
+        access_token: tokens.access_token,
+      });
+
+      var oauth2 = google.oauth2('v2');
+      oauth2.userinfo.get({}, function(err, response) {
+        debug('Response of oauth2.userinfo.get', err, response);
         if (err) {
-          return callback(new Error('[googleAuth.handleCallback] Failed to discover oauth2 API endpoint.'), null);
+          return callback(new Error('[googleAuth.handleCallback] Error while proceccing userinfo.get.'), null);
         }
 
-        var tokeninfo = client.oauth2.tokeninfo({id_token: tokens.id_token});
-        tokeninfo.execute(function(err, response) {
-          if (err) {
-            return callback(new Error('[googleAuth.handleCallback] Error while proceccing tokeninfo.'), null);
-          }
-
-          return callback(null, response);
-        });
+        response.user_id = response.id; // This is for B.C. (tokeninfo をつかっている前提のコードに対してのもの)
+        return callback(null, response);
       });
     });
   };

+ 1 - 1
package.json

@@ -58,7 +58,7 @@
     "express-form": "~0.12.0",
     "express-session": "~1.12.0",
     "font-awesome": "~4.5.0",
-    "googleapis": "=0.4.7",
+    "googleapis": "=12.3.0",
     "gulp": "~3.9.0",
     "gulp-concat": "~2.6.0",
     "gulp-cssmin": "~0.1.7",