Secondary Google account (part3)
This commit is contained in:
parent
31fea5be52
commit
8eea89b3aa
@ -924,7 +924,7 @@ define([
|
|||||||
});
|
});
|
||||||
$(".action-import-docs-settings-confirm").click(function() {
|
$(".action-import-docs-settings-confirm").click(function() {
|
||||||
storage.clear();
|
storage.clear();
|
||||||
var allowedKeys = /^file\.|^focusMode$|^folder\.|^publish\.|^settings$|^sync\.|^themeV3$|^mode$|^version$|^welcomeTour$/;
|
var allowedKeys = /^file\.|^focusMode$|^folder\.|^publish\.|^settings$|^sync\.|^google\.\d+\.|^themeV3$|^mode$|^version$|^welcomeTour$/;
|
||||||
_.each(newstorage, function(value, key) {
|
_.each(newstorage, function(value, key) {
|
||||||
if(allowedKeys.test(key)) {
|
if(allowedKeys.test(key)) {
|
||||||
storage[key] = value;
|
storage[key] = value;
|
||||||
|
@ -15,7 +15,9 @@ define([
|
|||||||
var connected = false;
|
var connected = false;
|
||||||
var authorizationMgrMap = {};
|
var authorizationMgrMap = {};
|
||||||
function AuthorizationMgr(accountId) {
|
function AuthorizationMgr(accountId) {
|
||||||
var permissionList = {};
|
var permissionList = {
|
||||||
|
profile: true
|
||||||
|
};
|
||||||
var refreshFlag = true;
|
var refreshFlag = true;
|
||||||
_.each((storage[accountId + '.permissions'] || '').split(';'), function(permission) {
|
_.each((storage[accountId + '.permissions'] || '').split(';'), function(permission) {
|
||||||
permission && (permissionList[permission] = true);
|
permission && (permissionList[permission] = true);
|
||||||
@ -38,15 +40,13 @@ define([
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
var token = {
|
var userId = storage[accountId + '.userId'];
|
||||||
access_token: storage[accountId + '.token']
|
this.setUserId = function(value) {
|
||||||
|
userId = value;
|
||||||
|
storage[accountId + '.userId'] = userId;
|
||||||
};
|
};
|
||||||
this.saveToken = function() {
|
this.getUserId = function() {
|
||||||
token = gapi.auth.getToken();
|
return userId;
|
||||||
storage[accountId + '.token'] = token.access_token;
|
|
||||||
};
|
|
||||||
this.getToken = function() {
|
|
||||||
return token;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,6 +92,9 @@ define([
|
|||||||
|
|
||||||
// Try to authenticate with Oauth
|
// Try to authenticate with Oauth
|
||||||
var scopeMap = {
|
var scopeMap = {
|
||||||
|
profile: [
|
||||||
|
'https://www.googleapis.com/auth/userinfo.profile'
|
||||||
|
],
|
||||||
gdrive: [
|
gdrive: [
|
||||||
'https://www.googleapis.com/auth/drive.install',
|
'https://www.googleapis.com/auth/drive.install',
|
||||||
settings.gdriveFullAccess === true ? 'https://www.googleapis.com/auth/drive' : 'https://www.googleapis.com/auth/drive.file'
|
settings.gdriveFullAccess === true ? 'https://www.googleapis.com/auth/drive' : 'https://www.googleapis.com/auth/drive.file'
|
||||||
@ -111,15 +114,73 @@ define([
|
|||||||
authorizationMgrMap[accountId] = authorizationMgr;
|
authorizationMgrMap[accountId] = authorizationMgr;
|
||||||
}
|
}
|
||||||
task.onRun(function() {
|
task.onRun(function() {
|
||||||
var immediate = false;
|
function loadGdriveClient() {
|
||||||
var token = authorizationMgr.getToken();
|
if(gapi.client.drive) {
|
||||||
if(token.access_token) {
|
|
||||||
immediate = true;
|
|
||||||
gapi.auth.setToken(token);
|
|
||||||
if(authorizationMgr.isAuthorized(permission)) {
|
|
||||||
task.chain();
|
task.chain();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
gapi.client.load('drive', 'v2', function() {
|
||||||
|
task.chain();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
function getTokenInfo() {
|
||||||
|
$.ajax({
|
||||||
|
url: 'https://www.googleapis.com/oauth2/v1/tokeninfo',
|
||||||
|
data: {
|
||||||
|
access_token: gapi.auth.getToken().access_token
|
||||||
|
},
|
||||||
|
timeout: constants.AJAX_TIMEOUT,
|
||||||
|
type: "GET"
|
||||||
|
}).done(function(data) {
|
||||||
|
var currentUserId = authorizationMgr.getUserId();
|
||||||
|
if(currentUserId && currentUserId != data.user_id) {
|
||||||
|
doAuthenticate();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
authorizationMgr.setUserId(data.user_id);
|
||||||
|
authorizationMgr.token = gapi.auth.getToken();
|
||||||
|
task.chain(loadGdriveClient);
|
||||||
|
}
|
||||||
|
}).fail(function(jqXHR) {
|
||||||
|
var error = {
|
||||||
|
code: jqXHR.status,
|
||||||
|
message: jqXHR.statusText
|
||||||
|
};
|
||||||
|
handleError(error, task);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
var authuser = 0;
|
||||||
|
var immediate;
|
||||||
|
function localAuthenticate() {
|
||||||
|
if(immediate === false) {
|
||||||
|
task.timeout = constants.ASYNC_TASK_LONG_TIMEOUT;
|
||||||
|
}
|
||||||
|
var scopeList = _.chain(scopeMap).pick(authorizationMgr.getListWithNew(permission)).flatten().value();
|
||||||
|
gapi.auth.authorize({
|
||||||
|
client_id: constants.GOOGLE_CLIENT_ID,
|
||||||
|
scope: scopeList,
|
||||||
|
immediate: immediate,
|
||||||
|
authuser: immediate === false ? '' : authuser
|
||||||
|
}, function(authResult) {
|
||||||
|
if(!authResult || authResult.error) {
|
||||||
|
if(connected === true && immediate === true) {
|
||||||
|
// If immediate did not work retry without immediate
|
||||||
|
// flag
|
||||||
|
immediate = false;
|
||||||
|
task.chain(oauthRedirect);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Error
|
||||||
|
task.error(new Error("Access to Google account is not authorized."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Success
|
||||||
|
authuser++;
|
||||||
|
authorizationMgr.add(permission);
|
||||||
|
task.chain(getTokenInfo);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
function oauthRedirect() {
|
function oauthRedirect() {
|
||||||
if(immediate === true) {
|
if(immediate === true) {
|
||||||
@ -132,38 +193,19 @@ define([
|
|||||||
task.error(new Error('Operation canceled.'));
|
task.error(new Error('Operation canceled.'));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function localAuthenticate() {
|
function doAuthenticate() {
|
||||||
if(immediate === false) {
|
immediate = true;
|
||||||
task.timeout = constants.ASYNC_TASK_LONG_TIMEOUT;
|
if(authorizationMgr.token && authorizationMgr.isAuthorized(permission)) {
|
||||||
|
gapi.auth.setToken(authorizationMgr.token);
|
||||||
|
task.chain();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
var scopeList = _.chain(scopeMap).pick(authorizationMgr.getListWithNew(permission)).flatten().value();
|
if(!authorizationMgr.getUserId()) {
|
||||||
gapi.auth.authorize({
|
immediate = false;
|
||||||
client_id: constants.GOOGLE_CLIENT_ID,
|
}
|
||||||
scope: scopeList,
|
task.chain(oauthRedirect);
|
||||||
immediate: immediate,
|
|
||||||
authuser: immediate ? undefined : ''
|
|
||||||
}, function(authResult) {
|
|
||||||
gapi.client.load('drive', 'v2', function() {
|
|
||||||
if(!authResult || authResult.error) {
|
|
||||||
// If immediate did not work retry without immediate
|
|
||||||
// flag
|
|
||||||
if(connected === true && immediate === true) {
|
|
||||||
immediate = false;
|
|
||||||
task.chain(oauthRedirect);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Error
|
|
||||||
task.error(new Error("Access to Google account is not authorized."));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Success
|
|
||||||
authorizationMgr.add(permission);
|
|
||||||
immediate === false && authorizationMgr.saveToken();
|
|
||||||
task.chain();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
task.chain(oauthRedirect);
|
doAuthenticate();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
googleHelper.refreshGdriveToken = function(accountId) {
|
googleHelper.refreshGdriveToken = function(accountId) {
|
||||||
|
Loading…
Reference in New Issue
Block a user