keeweb/app/scripts/storage/storage-base.js

211 lines
7.5 KiB
JavaScript
Raw Normal View History

2016-03-27 09:06:23 +02:00
'use strict';
var Backbone = require('backbone'),
Logger = require('../util/logger'),
2016-03-27 14:57:22 +02:00
AppSettingsModel = require('../models/app-settings-model'),
2016-03-27 16:47:29 +02:00
RuntimeDataModel = require('../models/runtime-data-model'),
Links = require('../const/links');
2016-03-27 09:06:23 +02:00
2016-03-27 15:18:05 +02:00
var MaxRequestRetries = 3;
2016-03-27 09:06:23 +02:00
var StorageBase = function() {
};
_.extend(StorageBase.prototype, {
name: null,
icon: null,
iconSvg: null,
enabled: false,
system: false,
uipos: null,
logger: null,
appSettings: AppSettingsModel.instance,
2016-03-27 14:57:22 +02:00
runtimeData: RuntimeDataModel.instance,
2016-03-27 09:06:23 +02:00
init: function() {
if (!this.name) {
throw 'Failed to init provider: no name';
}
if (!this.system) {
var enabled = this.appSettings.get(this.name);
if (typeof enabled === 'boolean') {
this.enabled = enabled;
}
}
this.logger = new Logger('storage-' + this.name);
2016-03-27 09:46:43 +02:00
return this;
2016-03-27 09:42:48 +02:00
},
setEnabled: function(enabled) {
this.enabled = enabled;
},
2016-03-27 09:42:48 +02:00
_xhr: function(config) {
var xhr = new XMLHttpRequest();
if (config.responseType) {
xhr.responseType = config.responseType;
}
var statuses = config.statuses || [200];
2016-03-27 15:18:05 +02:00
var that = this;
2016-03-27 09:42:48 +02:00
xhr.addEventListener('load', function() {
2016-03-27 15:18:05 +02:00
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);
}
2016-03-27 20:33:07 +02:00
that.logger.info('Repeat request, try #' + config.tryNum, config.url);
2016-03-27 15:18:05 +02:00
that._xhr(config);
}
});
} else {
2016-03-27 09:42:48 +02:00
return config.error && config.error('http status ' + xhr.status, xhr);
}
});
xhr.addEventListener('error', function() {
2016-03-27 15:18:05 +02:00
return config.error && config.error('network error', xhr);
2016-03-27 09:42:48 +02:00
});
xhr.addEventListener('timeout', function() {
2016-03-27 15:18:05 +02:00
return config.error && config.error('timeout', xhr);
2016-03-27 09:42:48 +02:00
});
xhr.open(config.method || 'GET', config.url);
2016-03-27 14:57:22 +02:00
if (this._oauthToken) {
xhr.setRequestHeader('Authorization',
this._oauthToken.tokenType + ' ' + this._oauthToken.accessToken);
}
2016-03-27 09:42:48 +02:00
_.forEach(config.headers, function(value, key) {
xhr.setRequestHeader(key, value);
});
xhr.send(config.data);
2016-03-27 13:54:35 +02:00
},
_openPopup: function(url, title, width, height) {
var dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : screen.left;
var dualScreenTop = window.screenTop !== undefined ? window.screenTop : screen.top;
var winWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
var winHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((winWidth / 2) - (width / 2)) + dualScreenLeft;
var top = ((winHeight / 2) - (height / 2)) + dualScreenTop;
var settings = {
width: width,
height: height,
left: left,
top: top,
dialog: 'yes',
dependent: 'yes',
scrollbars: 'yes',
location: 'yes'
};
settings = Object.keys(settings).map(function(key) { return key + '=' + settings[key]; }).join(',');
var win = window.open(url, title, settings);
2016-03-27 15:36:07 +02:00
if (win && win.focus) {
2016-03-27 13:54:35 +02:00
win.focus();
}
return win;
2016-03-27 14:57:22 +02:00
},
2016-04-03 16:33:34 +02:00
_getOauthRedirectUrl: function() {
var redirectUrl = window.location.href;
if (redirectUrl.lastIndexOf('file:', 0) === 0) {
redirectUrl = Links.WebApp;
}
return redirectUrl;
},
2016-03-27 15:18:05 +02:00
_oauthAuthorize: function(callback) {
2016-03-27 14:57:22 +02:00
var that = this;
2016-03-27 15:18:05 +02:00
if (that._oauthToken && !that._oauthToken.expired) {
return callback();
}
var opts = this._getOAuthConfig();
2016-03-27 14:57:22 +02:00
var oldToken = that.runtimeData.get(that.name + 'OAuthToken');
2016-03-27 15:18:05 +02:00
if (oldToken && !oldToken.expired) {
2016-03-27 14:57:22 +02:00
that._oauthToken = oldToken;
2016-03-27 15:18:05 +02:00
callback();
2016-03-27 14:57:22 +02:00
return;
}
2016-03-27 16:47:29 +02:00
var url = opts.url + '?client_id={cid}&scope={scope}&response_type=token&redirect_uri={url}'
2016-04-03 16:33:34 +02:00
.replace('{cid}', encodeURIComponent(opts.clientId))
2016-03-27 16:47:29 +02:00
.replace('{scope}', encodeURIComponent(opts.scope))
2016-04-03 16:33:34 +02:00
.replace('{url}', encodeURIComponent(this._getOauthRedirectUrl()));
2016-03-27 14:57:22 +02:00
that.logger.debug('OAuth popup opened');
2016-03-27 16:47:29 +02:00
if (!that._openPopup(url, 'OAuth', opts.width, opts.height)) {
2016-03-27 15:36:07 +02:00
callback('cannot open popup');
}
2016-03-27 14:57:22 +02:00
var popupClosed = function() {
Backbone.off('popup-closed', popupClosed);
window.removeEventListener('message', windowMessage);
that.logger.error('OAuth error', 'popup closed');
2016-03-27 15:18:05 +02:00
callback('popup closed');
2016-03-27 14:57:22 +02:00
};
var windowMessage = function(e) {
if (!e.data) {
return;
}
Backbone.off('popup-closed', popupClosed);
window.removeEventListener('message', windowMessage);
var token = that._oauthMsgToToken(e.data);
if (token.error) {
that.logger.error('OAuth error', token.error, token.errorDescription);
2016-03-27 15:18:05 +02:00
callback(token.error);
2016-03-27 14:57:22 +02:00
} else {
that._oauthToken = token;
that.runtimeData.set(that.name + 'OAuthToken', token);
that.logger.debug('OAuth success');
2016-03-27 15:18:05 +02:00
callback();
2016-03-27 14:57:22 +02:00
}
};
Backbone.on('popup-closed', popupClosed);
window.addEventListener('message', windowMessage);
},
_oauthMsgToToken: function(data) {
// jshint camelcase:false
if (data.error || !data.token_type) {
return { error: data.error || 'no token', errorDescription: data.error_description };
}
return {
tokenType: data.token_type,
accessToken: data.access_token,
authenticationToken: data.authentication_token,
expiresIn: data.expires_in,
scope: data.scope,
userId: data.user_id
};
2016-03-27 15:18:05 +02:00
},
_oauthRefreshToken: function(callback) {
this._oauthToken.expired = true;
this.runtimeData.set(this.name + 'OAuthToken', this._oauthToken);
this._oauthAuthorize(callback);
},
_oauthRevokeToken: function(url) {
var token = this.runtimeData.get(this.name + 'OAuthToken');
if (token) {
this._xhr({
url: url.replace('{token}', token.accessToken),
statuses: [200, 401]
});
this.runtimeData.unset(this.name + 'OAuthToken');
this._oauthToken = null;
}
2016-03-27 09:06:23 +02:00
}
});
StorageBase.extend = Backbone.Model.extend;
module.exports = StorageBase;