oauth for desktop

This commit is contained in:
antelle 2016-03-27 17:47:29 +03:00
parent 0a74168ace
commit 7d4152a02c
7 changed files with 93 additions and 23 deletions

View File

@ -8,15 +8,20 @@ var AuthReceiver = {
if (location.href.indexOf('state=') >= 0) {
DropboxLink.receive();
} else {
var message = {};
window.location.href.split(/[\?#&]/g).forEach(function(part) {
var parts = part.split('=');
if (parts.length === 2) {
message[parts[0]] = parts[1];
}
});
var message = this.urlArgsToMessage(window.location.href);
opener.postMessage(message, window.location.origin);
}
},
urlArgsToMessage: function(url) {
var message = {};
url.split(/[\?#&]/g).forEach(function(part) {
var parts = part.split('=');
if (parts.length === 2) {
message[parts[0]] = parts[1];
}
});
return message;
}
};

View File

@ -109,6 +109,9 @@ if (window.process && window.process.versions && window.process.versions.electro
proxy = match && match[1] ? { host: match[1], port: +match[2] } : null;
callback(proxy);
});
},
openWindow: function(opts) {
return this.remReq('app').openWindow(opts);
}
};
Backbone.on('launcher-exit-request', function() {

View File

@ -2,12 +2,17 @@
var Backbone = require('backbone'),
Alerts = require('./alerts'),
Launcher = require('./launcher'),
AuthReceiver = require('./auth-receiver'),
Links = require('../const/links'),
Timeouts = require('../const/timeouts'),
Locale = require('../util/locale');
var PopupNotifier = {
init: function() {
if (window.open) {
if (Launcher) {
window.open = this._openLauncherWindow;
} else {
var windowOpen = window.open;
window.open = function() {
var win = windowOpen.apply(window, arguments);
@ -27,6 +32,60 @@ var PopupNotifier = {
}
},
_openLauncherWindow: function(url, title, settings) {
var opts = {
show: false,
'web-preferences': {
'node-integration': false,
'web-security': false,
'allow-displaying-insecure-content': true,
'allow-running-insecure-content': true
}
};
if (settings) {
var settingsObj = {};
settings.split(',').forEach(function(part) {
var parts = part.split('=');
settingsObj[parts[0].trim()] = parts[1].trim();
});
if (settings.width) { opts.width = settings.width; }
if (settings.height) { opts.height = settings.height; }
if (settings.top) { opts.y = settings.top; }
if (settings.left) { opts.x = settings.left; }
}
var win = Launcher.openWindow(opts);
win.webContents.on('did-get-redirect-request', function(e, fromUrl, toUrl) {
if (toUrl.lastIndexOf(Links.WebApp, 0) === 0) {
win.webContents.stop();
win.close();
PopupNotifier.processReturnToApp(toUrl);
}
});
win.webContents.on('will-navigate', function(e, toUrl) {
if (toUrl.lastIndexOf(Links.WebApp, 0) === 0) {
e.preventDefault();
win.close();
PopupNotifier.processReturnToApp(toUrl);
}
});
win.loadUrl(url);
win.show();
win.on('closed', function() {
setTimeout(PopupNotifier.triggerClosed.bind(PopupNotifier, win), Timeouts.CheckWindowClosed);
});
Backbone.trigger('popup-opened', win);
return win;
},
processReturnToApp: function(url) {
var returnMessage = AuthReceiver.urlArgsToMessage(url);
if (Object.keys(returnMessage).length > 0) {
var evt = new Event('message');
evt.data = returnMessage;
window.dispatchEvent(evt);
}
},
deferCheckClosed: function(win) {
setTimeout(PopupNotifier.checkClosed.bind(PopupNotifier, win), Timeouts.CheckWindowClosed);
},

View File

@ -3,7 +3,7 @@
var Links = {
Repo: 'https://github.com/antelle/keeweb',
Desktop: 'https://github.com/antelle/keeweb/releases/latest',
WebApp: 'https://app.keeweb.info/',
WebApp: 'https://app.keeweb.info',
License: 'https://github.com/antelle/keeweb/blob/master/MIT-LICENSE.txt',
UpdateDesktop: 'https://github.com/antelle/keeweb/releases/download/v{ver}/UpdateDesktop.zip',
ReleaseNotes: 'https://github.com/antelle/keeweb/blob/master/release-notes.md#release-notes',

View File

@ -3,7 +3,8 @@
var Backbone = require('backbone'),
Logger = require('../util/logger'),
AppSettingsModel = require('../models/app-settings-model'),
RuntimeDataModel = require('../models/runtime-data-model');
RuntimeDataModel = require('../models/runtime-data-model'),
Links = require('../const/links');
var MaxRequestRetries = 3;
@ -123,8 +124,16 @@ _.extend(StorageBase.prototype, {
callback();
return;
}
var redirectUrl = window.location.href;
if (redirectUrl.lastIndexOf('file:', 0) === 0) {
redirectUrl = Links.WebApp;
}
var url = opts.url + '?client_id={cid}&scope={scope}&response_type=token&redirect_uri={url}'
.replace('{cid}', opts.clientId)
.replace('{scope}', encodeURIComponent(opts.scope))
.replace('{url}', encodeURIComponent(redirectUrl));
that.logger.debug('OAuth popup opened');
if (!that._openPopup(opts.url, 'OAuth', opts.width, opts.height)) {
if (!that._openPopup(url, 'OAuth', opts.width, opts.height)) {
callback('cannot open popup');
}
var popupClosed = function() {

View File

@ -136,13 +136,10 @@ var StorageGDrive = StorageBase.extend({
_getOAuthConfig: function() {
var clientId = this.appSettings.get('gdriveClientId') || GDriveClientId;
var url = 'https://accounts.google.com/o/oauth2/v2/auth' +
'?client_id={cid}&scope={scope}&response_type=token&redirect_uri={url}'
.replace('{cid}', clientId)
.replace('{scope}', encodeURIComponent('https://www.googleapis.com/auth/drive'))
.replace('{url}', encodeURIComponent(window.location));
return {
url: url,
scope: 'https://www.googleapis.com/auth/drive',
url: 'https://accounts.google.com/o/oauth2/v2/auth',
clientId: clientId,
width: 600,
height: 400
};

View File

@ -177,13 +177,10 @@ var StorageOneDrive = StorageBase.extend({
_getOAuthConfig: function() {
var clientId = this._getClientId();
var url = 'https://login.live.com/oauth20_authorize.srf' +
'?client_id={cid}&scope={scope}&response_type=token&redirect_uri={url}'
.replace('{cid}', clientId)
.replace('{scope}', 'onedrive.readwrite')
.replace('{url}', encodeURIComponent(window.location));
return {
url: url,
url: 'https://login.live.com/oauth20_authorize.srf',
scope: 'onedrive.readwrite',
clientId: clientId,
width: 600,
height: 500
};