1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-22 11:13:46 +00:00

refactor custom env urls, cleanup bg script a bit

This commit is contained in:
Kyle Spearrin
2017-09-04 12:09:11 -04:00
parent a95bd44217
commit 4641240d47
8 changed files with 273 additions and 204 deletions

View File

@@ -5,53 +5,49 @@ function ApiService(tokenService, appIdService, utilsService, constantsService,
this.utilsService = utilsService;
this.constantsService = constantsService;
this.urlsSet = false;
this.baseUrl = null;
this.identityBaseUrl = null;
initApiService();
this.setUrls();
}
function initApiService() {
ApiService.prototype.setUrls = function () {
try {
var storedBaseUrl = window.localStorage.getItem(this.constantsService.baseUrlKey);
ApiService.prototype.setUrls = function (urls) {
var self = this;
self.urlsSet = true;
if (storedBaseUrl) {
this.baseUrl = storedBaseUrl + '/api';
this.identityBaseUrl = storedBaseUrl + '/identity';
return;
}
var storedApiUrl = window.localStorage.getItem(this.constantsService.apiUrlKey);
var storedIdentityUrl = window.localStorage.getItem(this.constantsService.identityUrlKey);
if (storedApiUrl && storedIdentityUrl) {
this.baseUrl = storedApiUrl;
this.identityBaseUrl = storedIdentityUrl;
return;
}
if (urls.base) {
self.baseUrl = urls.base + '/api';
self.identityBaseUrl = urls.base + '/identity';
return;
}
catch (e) {
console.log('Unable to set custom environment URLs:');
console.log(e);
if (urls.api && urls.identity) {
self.baseUrl = urls.api;
self.identityBaseUrl = urls.identity;
return;
}
// Desktop
//this.baseUrl = 'http://localhost:4000';
//this.identityBaseUrl = 'http://localhost:33656';
//self.baseUrl = 'http://localhost:4000';
//self.identityBaseUrl = 'http://localhost:33656';
// Desktop HTTPS
//this.baseUrl = 'https://localhost:44377';
//this.identityBaseUrl = 'https://localhost:44392';
//self.baseUrl = 'https://localhost:44377';
//self.identityBaseUrl = 'https://localhost:44392';
// Desktop external
//this.baseUrl = 'http://192.168.1.4:4000';
//this.identityBaseUrl = 'http://192.168.1.4:33656';
//self.baseUrl = 'http://192.168.1.4:4000';
//self.identityBaseUrl = 'http://192.168.1.4:33656';
// Preview
//this.baseUrl = 'https://preview-api.bitwarden.com';
//this.identityBaseUrl = 'https://preview-identity.bitwarden.com';
//self.baseUrl = 'https://preview-api.bitwarden.com';
//self.identityBaseUrl = 'https://preview-identity.bitwarden.com';
// Production
this.baseUrl = 'https://api.bitwarden.com';
this.identityBaseUrl = 'https://identity.bitwarden.com';
self.baseUrl = 'https://api.bitwarden.com';
self.identityBaseUrl = 'https://identity.bitwarden.com';
};
// Auth APIs

View File

@@ -1,9 +1,6 @@
function ConstantsService(i18nService) {
return {
baseUrlKey: 'baseUrl',
webVaultUrlKey: 'webVaultUrl',
apiUrlKey: 'apiUrl',
identityUrlKey: 'identityUrl',
environmentUrlsKey: 'environmentUrls',
disableGaKey: 'disableGa',
disableAddLoginNotificationKey: 'disableAddLoginNotification',
disableContextMenuItemKey: 'disableContextMenuItem',

View File

@@ -0,0 +1,96 @@
function EnvironmentService(constantsService, apiService) {
this.constantsService = constantsService;
this.apiService = apiService;
this.baseUrl = null;
this.webVaultUrl = null;
this.apiUrl = null;
this.identityUrl = null;
initEnvironmentService();
}
function initEnvironmentService() {
EnvironmentService.prototype.setUrlsFromStorage = function (callback) {
var self = this;
chrome.storage.local.get(self.constantsService.environmentUrlsKey, function (urlsObj) {
var urls = urlsObj[self.constantsService.environmentUrlsKey] || {
base: null,
api: null,
identity: null,
webVault: null
};
self.baseUrl = urls.base;
if (self.baseUrl) {
self.apiService.setUrls({
base: self.baseUrl
});
callback();
return;
}
self.webVaultUrl = urls.webVault;
self.apiUrl = urls.api;
self.identityUrl = urls.identity;
self.apiService.setUrls({
api: self.apiUrl,
identity: self.identityUrl
});
callback();
return;
});
};
EnvironmentService.prototype.setUrls = function (urls, callback) {
var self = this;
urls.base = formatUrl(urls.base);
urls.webVault = formatUrl(urls.webVault);
urls.api = formatUrl(urls.api);
urls.identity = formatUrl(urls.identity);
var urlsObj = {};
urlsObj[self.constantsService.environmentUrlsKey] = {
base: urls.base,
api: urls.api,
identity: urls.identity,
webVault: urls.webVault
};
chrome.storage.local.set(urlsObj, function () {
self.baseUrl = urls.base;
self.webVaultUrl = urls.webVault;
self.apiUrl = urls.api;
self.identityUrl = urls.identity;
if (self.baseUrl) {
self.apiService.setUrls({
base: self.baseUrl
});
}
else {
self.apiService.setUrls({
api: self.apiUrl,
identity: self.identityUrl
});
}
callback(urls);
});
};
function formatUrl(url) {
if (!url || url === '') {
return null;
}
url = url.replace(/\/+$/g, '');
if (!url.startsWith("http://") && !url.startsWith('https://')) {
url = 'https://' + url;
}
return url;
}
}