mirror of https://github.com/keeweb/keeweb.git
validate and update tokens
This commit is contained in:
parent
ec8efbf4d3
commit
61b1ec5ecf
|
@ -5,6 +5,8 @@ var Backbone = require('backbone'),
|
|||
AppSettingsModel = require('../models/app-settings-model'),
|
||||
RuntimeDataModel = require('../models/runtime-data-model');
|
||||
|
||||
var MaxRequestRetries = 3;
|
||||
|
||||
var StorageBase = function() {
|
||||
};
|
||||
|
||||
|
@ -40,17 +42,34 @@ _.extend(StorageBase.prototype, {
|
|||
xhr.responseType = config.responseType;
|
||||
}
|
||||
var statuses = config.statuses || [200];
|
||||
var that = this;
|
||||
xhr.addEventListener('load', function() {
|
||||
if (statuses.indexOf(xhr.status) < 0) {
|
||||
if (statuses.indexOf(xhr.status) >= 0) {
|
||||
return config.success && config.success(xhr.response, xhr);
|
||||
}
|
||||
if (xhr.status === 401 && that._oauthToken) {
|
||||
that._oauthRefreshToken(function(err) {
|
||||
if (err) {
|
||||
return config.error && config.error('unauthorized', xhr);
|
||||
} else {
|
||||
config.tryNum = (config.tryNum || 0) + 1;
|
||||
if (config.tryNum >= MaxRequestRetries) {
|
||||
that.logger.info('Too many authorize attempts, fail request', config.url);
|
||||
return config.error && config.error('unauthorized', xhr);
|
||||
}
|
||||
that.logger.info('Repeat request, try #', config.url, config.tryNum);
|
||||
that._xhr(config);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return config.error && config.error('http status ' + xhr.status, xhr);
|
||||
}
|
||||
return config.success && config.success(xhr.response, xhr);
|
||||
});
|
||||
xhr.addEventListener('error', function() {
|
||||
return config.error && config.error('network error');
|
||||
return config.error && config.error('network error', xhr);
|
||||
});
|
||||
xhr.addEventListener('timeout', function() {
|
||||
return config.error && config.error('timeout');
|
||||
return config.error && config.error('timeout', xhr);
|
||||
});
|
||||
xhr.open(config.method || 'GET', config.url);
|
||||
if (this._oauthToken) {
|
||||
|
@ -92,12 +111,16 @@ _.extend(StorageBase.prototype, {
|
|||
return win;
|
||||
},
|
||||
|
||||
_oauthAuthorize: function(opts) {
|
||||
_oauthAuthorize: function(callback) {
|
||||
var that = this;
|
||||
if (that._oauthToken && !that._oauthToken.expired) {
|
||||
return callback();
|
||||
}
|
||||
var opts = this._getOAuthConfig();
|
||||
var oldToken = that.runtimeData.get(that.name + 'OAuthToken');
|
||||
if (oldToken) {
|
||||
if (oldToken && !oldToken.expired) {
|
||||
that._oauthToken = oldToken;
|
||||
opts.callback();
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
that.logger.debug('OAuth popup opened');
|
||||
|
@ -106,7 +129,7 @@ _.extend(StorageBase.prototype, {
|
|||
Backbone.off('popup-closed', popupClosed);
|
||||
window.removeEventListener('message', windowMessage);
|
||||
that.logger.error('OAuth error', 'popup closed');
|
||||
opts.callback('popup closed');
|
||||
callback('popup closed');
|
||||
};
|
||||
var windowMessage = function(e) {
|
||||
if (!e.data) {
|
||||
|
@ -117,12 +140,12 @@ _.extend(StorageBase.prototype, {
|
|||
var token = that._oauthMsgToToken(e.data);
|
||||
if (token.error) {
|
||||
that.logger.error('OAuth error', token.error, token.errorDescription);
|
||||
opts.callback(token.error);
|
||||
callback(token.error);
|
||||
} else {
|
||||
that._oauthToken = token;
|
||||
that.runtimeData.set(that.name + 'OAuthToken', token);
|
||||
that.logger.debug('OAuth success');
|
||||
opts.callback();
|
||||
callback();
|
||||
}
|
||||
};
|
||||
Backbone.on('popup-closed', popupClosed);
|
||||
|
@ -142,6 +165,12 @@ _.extend(StorageBase.prototype, {
|
|||
scope: data.scope,
|
||||
userId: data.user_id
|
||||
};
|
||||
},
|
||||
|
||||
_oauthRefreshToken: function(callback) {
|
||||
this._oauthToken.expired = true;
|
||||
this.runtimeData.set(this.name + 'OAuthToken', this._oauthToken);
|
||||
this._oauthAuthorize(callback);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ var StorageGDrive = StorageBase.extend({
|
|||
|
||||
stat: function(path, opts, callback) {
|
||||
var that = this;
|
||||
this._authorize(function(err) {
|
||||
this._oauthAuthorize(function(err) {
|
||||
if (err) {
|
||||
return callback && callback(err);
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ var StorageGDrive = StorageBase.extend({
|
|||
|
||||
list: function(callback) {
|
||||
var that = this;
|
||||
this._authorize(function(err) {
|
||||
this._oauthAuthorize(function(err) {
|
||||
if (err) { return callback && callback(err); }
|
||||
that.logger.debug('List');
|
||||
var url = that._baseUrl + '/files?fields={fields}&q={q}'
|
||||
|
@ -134,22 +134,18 @@ var StorageGDrive = StorageBase.extend({
|
|||
});
|
||||
},
|
||||
|
||||
_authorize: function(callback) {
|
||||
if (this._oauthToken) {
|
||||
return callback();
|
||||
}
|
||||
_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));
|
||||
this._oauthAuthorize({
|
||||
return {
|
||||
url: url,
|
||||
callback: callback,
|
||||
width: 600,
|
||||
height: 400
|
||||
});
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ var StorageOneDrive = StorageBase.extend({
|
|||
|
||||
load: function(path, opts, callback) {
|
||||
var that = this;
|
||||
this._authorize(function(err) {
|
||||
this._oauthAuthorize(function(err) {
|
||||
if (err) {
|
||||
return callback && callback(err);
|
||||
}
|
||||
|
@ -68,7 +68,7 @@ var StorageOneDrive = StorageBase.extend({
|
|||
|
||||
stat: function(path, opts, callback) {
|
||||
var that = this;
|
||||
this._authorize(function(err) {
|
||||
this._oauthAuthorize(function(err) {
|
||||
if (err) {
|
||||
return callback && callback(err);
|
||||
}
|
||||
|
@ -97,7 +97,7 @@ var StorageOneDrive = StorageBase.extend({
|
|||
|
||||
save: function(path, opts, data, callback, rev) {
|
||||
var that = this;
|
||||
this._authorize(function(err) {
|
||||
this._oauthAuthorize(function(err) {
|
||||
if (err) {
|
||||
return callback && callback(err);
|
||||
}
|
||||
|
@ -134,7 +134,7 @@ var StorageOneDrive = StorageBase.extend({
|
|||
|
||||
list: function(callback) {
|
||||
var that = this;
|
||||
this._authorize(function(err) {
|
||||
this._oauthAuthorize(function(err) {
|
||||
if (err) { return callback && callback(err); }
|
||||
that.logger.debug('List');
|
||||
var ts = that.logger.ts();
|
||||
|
@ -175,22 +175,18 @@ var StorageOneDrive = StorageBase.extend({
|
|||
return clientId;
|
||||
},
|
||||
|
||||
_authorize: function(callback) {
|
||||
if (this._oauthToken) {
|
||||
return callback();
|
||||
}
|
||||
_getOAuthConfig: function(callback) {
|
||||
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));
|
||||
this._oauthAuthorize({
|
||||
return {
|
||||
url: url,
|
||||
callback: callback,
|
||||
width: 600,
|
||||
height: 500
|
||||
});
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue