|
|
@@ -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);
|
|
|
});
|
|
|
});
|
|
|
};
|