diff --git a/src/models/api/responseModels.js b/src/models/api/responseModels.js index 48132313..fa3bae38 100644 --- a/src/models/api/responseModels.js +++ b/src/models/api/responseModels.js @@ -166,3 +166,27 @@ var DomainsResponse = function (response) { this.globalEquivalentDomains.push(new GlobalDomainResponse(globalEquivalentDomains[i])); } }; + +var SyncResponse = function (response) { + if (response.Profile) { + this.profile = new ProfileResponse(response.Profile); + } + + this.folders = []; + if (response.Folders) { + for (var i = 0; i < response.Folders.length; i++) { + this.folders.push(new FolderResponse(response.Folders[i])); + } + } + + this.ciphers = []; + if (response.Ciphers) { + for (var i = 0; i < response.Ciphers.length; i++) { + this.ciphers.push(new CipherResponse(response.Ciphers[i])); + } + } + + if (response.Domains) { + this.domains = new DomainsResponse(response.Domains); + } +}; diff --git a/src/services/apiService.js b/src/services/apiService.js index fa4ff26d..7f2473ab 100644 --- a/src/services/apiService.js +++ b/src/services/apiService.js @@ -30,8 +30,8 @@ function initApiService() { } // Desktop - //self.baseUrl = 'http://localhost:4000'; - //self.identityBaseUrl = 'http://localhost:33656'; + self.baseUrl = 'http://localhost:4000'; + self.identityBaseUrl = 'http://localhost:33656'; // Desktop HTTPS //self.baseUrl = 'https://localhost:44377'; @@ -46,8 +46,8 @@ function initApiService() { //self.identityBaseUrl = 'https://preview-identity.bitwarden.com'; // Production - self.baseUrl = 'https://api.bitwarden.com'; - self.identityBaseUrl = 'https://identity.bitwarden.com'; + //self.baseUrl = 'https://api.bitwarden.com'; + //self.identityBaseUrl = 'https://identity.bitwarden.com'; }; // Auth APIs @@ -138,26 +138,6 @@ function initApiService() { }); }; - ApiService.prototype.getProfile = function (success, error) { - var self = this; - handleTokenState(self).then(function (tokenHeader) { - $.ajax({ - type: 'GET', - url: self.baseUrl + '/accounts/profile', - dataType: 'json', - headers: tokenHeader, - success: function (response) { - success(new ProfileResponse(response)); - }, - error: function (jqXHR, textStatus, errorThrown) { - handleError(error, jqXHR, false, self); - } - }); - }, function (jqXHR) { - handleError(error, jqXHR, true, self); - }); - }; - ApiService.prototype.getKeys = function (success, error) { var self = this; handleTokenState(self).then(function (tokenHeader) { @@ -212,28 +192,6 @@ function initApiService() { }); }; - // Settings APIs - - ApiService.prototype.getIncludedDomains = function (success, error) { - var self = this; - handleTokenState(self).then(function (tokenHeader) { - $.ajax({ - type: 'GET', - url: self.baseUrl + '/settings/domains?excluded=false', - dataType: 'json', - headers: tokenHeader, - success: function (response) { - success(new DomainsResponse(response)); - }, - error: function (jqXHR, textStatus, errorThrown) { - handleError(error, jqXHR, false, self); - } - }); - }, function (jqXHR) { - handleError(error, jqXHR, true, self); - }); - }; - // Login APIs ApiService.prototype.getLogin = function (id, success, error) { @@ -322,31 +280,6 @@ function initApiService() { }); }; - ApiService.prototype.getFolders = function (success, error) { - var self = this; - handleTokenState(self).then(function (tokenHeader) { - $.ajax({ - type: 'GET', - url: self.baseUrl + '/folders', - dataType: 'json', - headers: tokenHeader, - success: function (response) { - var data = []; - for (var i = 0; i < response.Data.length; i++) { - data.push(new FolderResponse(response.Data[i])); - } - - success(new ListResponse(data)); - }, - error: function (jqXHR, textStatus, errorThrown) { - handleError(error, jqXHR, false, self); - } - }); - }, function (jqXHR) { - handleError(error, jqXHR, true, self); - }); - }; - ApiService.prototype.postFolder = function (folderRequest, success, error) { var self = this; handleTokenState(self).then(function (tokenHeader) { @@ -433,31 +366,6 @@ function initApiService() { }); }; - ApiService.prototype.getCiphers = function (success, error) { - var self = this; - handleTokenState(self).then(function (tokenHeader) { - $.ajax({ - type: 'GET', - url: self.baseUrl + '/ciphers', - dataType: 'json', - headers: tokenHeader, - success: function (response) { - var data = []; - for (var i = 0; i < response.Data.length; i++) { - data.push(new CipherResponse(response.Data[i])); - } - - success(new ListResponse(data)); - }, - error: function (jqXHR, textStatus, errorThrown) { - handleError(error, jqXHR, false, self); - } - }); - }, function (jqXHR) { - handleError(error, jqXHR, true, self); - }); - }; - ApiService.prototype.deleteCipher = function (id, success, error) { var self = this; handleTokenState(self).then(function (tokenHeader) { @@ -521,6 +429,28 @@ function initApiService() { }); }; + // Sync APIs + + ApiService.prototype.getSync = function (success, error) { + var self = this; + handleTokenState(self).then(function (tokenHeader) { + $.ajax({ + type: 'GET', + url: self.baseUrl + '/sync', + dataType: 'json', + headers: tokenHeader, + success: function (response) { + success(new SyncResponse(response)); + }, + error: function (jqXHR, textStatus, errorThrown) { + handleError(error, jqXHR, false, self); + } + }); + }, function (jqXHR) { + handleError(error, jqXHR, true, self); + }); + }; + // Helpers function handleError(errorCallback, jqXHR, tokenError, self) { diff --git a/src/services/syncService.js b/src/services/syncService.js index fd854743..24977f72 100644 --- a/src/services/syncService.js +++ b/src/services/syncService.js @@ -53,26 +53,28 @@ function initSyncService() { log('starting sync'); self.userService.getUserId(function (userId) { - log('sync profile'); - syncProfile(self).then(function () { - log('sync folders'); - return syncFolders(self, userId); - }).then(function () { - log('sync ciphers'); - return syncCiphers(self, userId); - }).then(function () { - log('sync settings'); - return syncSettings(self, userId); - }).then(function () { - log('all done with the syncs - ' + now); - self.setLastSync(now, function () { - self.syncCompleted(true); - callback(true); + self.apiService.getSync(function (response) { + log('sync profile'); + syncProfile(self, response.profile).then(function () { + log('sync folders'); + return syncFolders(self, userId, response.folders); + }).then(function () { + log('sync ciphers'); + return syncCiphers(self, userId, response.ciphers); + }).then(function () { + log('sync settings'); + return syncSettings(self, userId, response.domains); + }).then(function () { + log('all done with the syncs - ' + now); + self.setLastSync(now, function () { + self.syncCompleted(true); + callback(true); + }); + }, function () { + log('and error happened during the syncs'); + self.syncCompleted(false); + callback(false); }); - }, function () { - log('and error happened during the syncs'); - self.syncCompleted(false); - callback(false); }); }); }); @@ -110,38 +112,34 @@ function initSyncService() { }); } - function syncProfile(self) { + function syncProfile(self, response) { var deferred = Q.defer(); - self.apiService.getProfile(function (response) { - self.userService.getSecurityStamp().then(function (stamp) { - if (stamp && stamp !== response.securityStamp) { - if (self.logoutCallback) { - self.logoutCallback(true, function () { }); - } - - deferred.reject(); - return; + self.userService.getSecurityStamp().then(function (stamp) { + if (stamp && stamp !== response.securityStamp) { + if (self.logoutCallback) { + self.logoutCallback(true, function () { }); } - return self.cryptoService.setEncKey(response.key); - }).then(function () { - return self.cryptoService.setEncPrivateKey(response.privateKey); - }, function () { deferred.reject(); - }).then(function () { - return self.cryptoService.setOrgKeys(response.organizations); - }, function () { - deferred.reject(); - }).then(function () { - return self.userService.setSecurityStamp(response.securityStamp); - }, function () { - deferred.reject(); - }).then(function () { - deferred.resolve(); - }, function () { - deferred.reject(); - }); + return; + } + + return self.cryptoService.setEncKey(response.key); + }).then(function () { + return self.cryptoService.setEncPrivateKey(response.privateKey); + }, function () { + deferred.reject(); + }).then(function () { + return self.cryptoService.setOrgKeys(response.organizations); + }, function () { + deferred.reject(); + }).then(function () { + return self.userService.setSecurityStamp(response.securityStamp); + }, function () { + deferred.reject(); + }).then(function () { + deferred.resolve(); }, function () { deferred.reject(); }); @@ -149,70 +147,58 @@ function initSyncService() { return deferred.promise; } - function syncFolders(self, userId) { + function syncFolders(self, userId, response) { var deferred = Q.defer(); - self.apiService.getFolders(function (response) { - var folders = {}; + var folders = {}; - for (var i = 0; i < response.data.length; i++) { - folders[response.data[i].id] = new FolderData(response.data[i], userId); - } + for (var i = 0; i < response.length; i++) { + folders[response[i].id] = new FolderData(response[i], userId); + } - self.folderService.replace(folders, function () { - deferred.resolve(); - }); - }, function () { - deferred.reject(); + self.folderService.replace(folders, function () { + deferred.resolve(); }); return deferred.promise; } - function syncCiphers(self, userId) { + function syncCiphers(self, userId, response) { var deferred = Q.defer(); - self.apiService.getCiphers(function (response) { - var logins = {}; + var logins = {}; - for (var i = 0; i < response.data.length; i++) { - var data = response.data[i]; - if (data.type === 1) { - logins[data.id] = new LoginData(data, userId); - } + for (var i = 0; i < response.length; i++) { + var data = response[i]; + if (data.type === 1) { + logins[data.id] = new LoginData(data, userId); } + } - self.loginService.replace(logins, function () { - deferred.resolve(); - }); - }, function () { - deferred.reject(); + self.loginService.replace(logins, function () { + deferred.resolve(); }); return deferred.promise; } - function syncSettings(self, userId) { + function syncSettings(self, userId, response) { var deferred = Q.defer(); - var ciphers = self.apiService.getIncludedDomains(function (response) { - var eqDomains = []; - if (response && response.equivalentDomains) { - eqDomains = eqDomains.concat(response.equivalentDomains); - } - if (response && response.globalEquivalentDomains) { - for (var i = 0; i < response.globalEquivalentDomains.length; i++) { - if (response.globalEquivalentDomains[i].domains.length) { - eqDomains.push(response.globalEquivalentDomains[i].domains); - } + var eqDomains = []; + if (response && response.equivalentDomains) { + eqDomains = eqDomains.concat(response.equivalentDomains); + } + if (response && response.globalEquivalentDomains) { + for (var i = 0; i < response.globalEquivalentDomains.length; i++) { + if (response.globalEquivalentDomains[i].domains.length) { + eqDomains.push(response.globalEquivalentDomains[i].domains); } } + } - self.settingsService.setEquivalentDomains(eqDomains, function () { - deferred.resolve(); - }); - }, function () { - deferred.reject(); + self.settingsService.setEquivalentDomains(eqDomains, function () { + deferred.resolve(); }); return deferred.promise;