Skip to content

Commit

Permalink
fix #1359: fixed Google Drive login issues in desktop apps
Browse files Browse the repository at this point in the history
  • Loading branch information
antelle committed Mar 17, 2020
1 parent 61d8331 commit 7ccf38f
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 77 deletions.
13 changes: 4 additions & 9 deletions app/scripts/comp/app/dropbox-chooser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AppSettingsModel } from 'models/app-settings-model';
import { UrlFormat } from 'util/formatting/url-format';

const ChooserAppKey = 'qp7ctun6qt5n9d6';

Expand Down Expand Up @@ -26,8 +27,8 @@ DropboxChooser.prototype.choose = function() {
};

DropboxChooser.prototype.buildUrl = function() {
const urlParams = {
origin: encodeURIComponent(window.location.protocol + '//' + window.location.host),
return UrlFormat.makeUrl('https://www.dropbox.com/chooser', {
origin: window.location.protocol + '//' + window.location.host,
'app_key': AppSettingsModel.dropboxAppKey || ChooserAppKey,
'link_type': 'direct',
trigger: 'js',
Expand All @@ -36,13 +37,7 @@ DropboxChooser.prototype.buildUrl = function() {
folderselect: 'false',
iframe: 'false',
version: 2
};
return (
'https://www.dropbox.com/chooser?' +
Object.keys(urlParams)
.map(key => key + '=' + urlParams[key])
.join('&')
);
});
};

DropboxChooser.prototype.onMessage = function(e) {
Expand Down
3 changes: 2 additions & 1 deletion app/scripts/const/timeouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const Timeouts = {
RedrawInactiveWindow: 50,
PopupWaitTime: 1000,
AutoUpdatePluginsAfterStart: 500,
LinkDownloadRevoke: 10 * 1000 * 60
LinkDownloadRevoke: 10 * 1000 * 60,
DefaultHttpRequest: 60000
};

export { Timeouts };
9 changes: 3 additions & 6 deletions app/scripts/storage/impl/storage-dropbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,25 +211,22 @@ class StorageDropbox extends StorageBase {
const host = args.host || 'api';
let headers;
let data = args.data;
let dataType;
if (args.apiArg) {
headers = {
'Dropbox-API-Arg': this._encodeJsonHttpHeader(JSON.stringify(args.apiArg))
};
if (args.data) {
headers['Content-Type'] = 'application/octet-stream';
}
} else if (args.data) {
data = JSON.stringify(data);
headers = {
'Content-Type': 'application/json'
};
dataType = 'application/json';
}
this._xhr({
url: `https://${host}.dropboxapi.com/2/${args.method}`,
method: 'POST',
responseType: args.responseType || 'json',
headers,
data,
dataType,
statuses: args.statuses || undefined,
success: args.success,
error: (e, xhr) => {
Expand Down
84 changes: 54 additions & 30 deletions app/scripts/storage/impl/storage-gdrive.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { StorageBase } from 'storage/storage-base';
import { Locale } from 'util/locale';
import { Features } from 'util/features';

const GDriveClientId = {
Local: '783608538594-36tkdh8iscrq8t8dq87gghubnhivhjp5.apps.googleusercontent.com',
Production: '847548101761-koqkji474gp3i2gn3k5omipbfju7pbt1.apps.googleusercontent.com'
Production: '847548101761-koqkji474gp3i2gn3k5omipbfju7pbt1.apps.googleusercontent.com',
Desktop: '847548101761-h2pcl2p6m1tssnlqm0vrm33crlveccbr.apps.googleusercontent.com'
};
const GDriveClientSecret = {
// They are not really secrets and are supposed to be embedded in the app code according to
// the official guide: https://developers.google.com/identity/protocols/oauth2#installed
// The process results in a client ID and, in some cases, a client secret,
// which you embed in the source code of your application.
// (In this context, the client secret is obviously not treated as a secret.)
Desktop: 'nTSCiqXtUNmURIIdASaC1TJK'
};
const NewFileIdPrefix = 'NewFile:';

Expand Down Expand Up @@ -95,46 +105,48 @@ class StorageGDrive extends StorageBase {
const ts = this.logger.ts();
const isNew = path.lastIndexOf(NewFileIdPrefix, 0) === 0;
let url;
let dataType;
let dataIsMultipart = false;
if (isNew) {
url =
this._baseUrlUpload +
'/files?uploadType=multipart&fields=id,headRevisionId';
const fileName = path.replace(NewFileIdPrefix, '') + '.kdbx';
const boundry = 'b' + Date.now() + 'x' + Math.round(Math.random() * 1000000);
data = new Blob(
[
'--',
boundry,
'\r\n',
'Content-Type: application/json; charset=UTF-8',
'\r\n\r\n',
JSON.stringify({ name: fileName }),
'\r\n',
'--',
boundry,
'\r\n',
'Content-Type: application/octet-stream',
'\r\n\r\n',
data,
'\r\n',
'--',
boundry,
'--',
'\r\n'
],
{ type: 'multipart/related; boundary="' + boundry + '"' }
);
const boundary = 'b' + Date.now() + 'x' + Math.round(Math.random() * 1000000);
data = [
'--',
boundary,
'\r\n',
'Content-Type: application/json; charset=UTF-8',
'\r\n\r\n',
JSON.stringify({ name: fileName }),
'\r\n',
'--',
boundary,
'\r\n',
'Content-Type: application/octet-stream',
'\r\n\r\n',
data,
'\r\n',
'--',
boundary,
'--',
'\r\n'
];
dataType = 'multipart/related; boundary="' + boundary + '"';
dataIsMultipart = true;
} else {
url =
this._baseUrlUpload +
'/files/{id}?uploadType=media&fields=headRevisionId'.replace('{id}', path);
data = new Blob([data], { type: 'application/octet-stream' });
}
this._xhr({
url,
method: isNew ? 'POST' : 'PATCH',
responseType: 'json',
data,
dataType,
dataIsMultipart,
success: response => {
this.logger.debug('Saved', path, this.logger.ts(ts));
const newRev = response.headRevisionId;
Expand Down Expand Up @@ -239,20 +251,32 @@ class StorageGDrive extends StorageBase {

_getOAuthConfig() {
let clientId = this.appSettings.gdriveClientId;
let clientSecret;
if (!clientId) {
clientId =
location.origin.indexOf('localhost') >= 0
? GDriveClientId.Local
: GDriveClientId.Production;
if (Features.isDesktop) {
clientId = GDriveClientId.Desktop;
clientSecret = GDriveClientSecret.Desktop;
} else {
clientId =
location.origin.indexOf('localhost') >= 0
? GDriveClientId.Local
: GDriveClientId.Production;
}
}
return {
scope: 'https://www.googleapis.com/auth/drive',
url: 'https://accounts.google.com/o/oauth2/v2/auth',
tokenUrl: 'https://oauth2.googleapis.com/token',
clientId,
clientSecret,
width: 600,
height: 400
};
}

_useLocalOAuthRedirectListener() {
return Features.isDesktop;
}
}

export { StorageGDrive };
5 changes: 3 additions & 2 deletions app/scripts/storage/impl/storage-onedrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class StorageOneDrive extends StorageBase {
method: 'PUT',
responseType: 'json',
headers: rev ? { 'If-Match': rev } : null,
data: new Blob([data], { type: 'application/octet-stream' }),
data,
statuses: [200, 201, 412],
success: (response, xhr) => {
rev = response.eTag;
Expand Down Expand Up @@ -213,7 +213,8 @@ class StorageOneDrive extends StorageBase {
method: 'POST',
responseType: 'json',
statuses: [200, 204],
data: new Blob([data], { type: 'application/json' }),
data,
dataType: 'application/json',
success: () => {
this.logger.debug('Made dir', path, this.logger.ts(ts));
return callback && callback();
Expand Down

1 comment on commit 7ccf38f

@thedaviddelta
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing! Great work man thank you

Please sign in to comment.