From 154427a0f31b5dd82bd7951226c17f5cfb1b39fc Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 25 Apr 2017 16:23:37 -0400 Subject: [PATCH] get private key if not available during sync --- src/models/api/responseModels.js | 5 +++++ src/services/apiService.js | 23 +++++++++++++++++++++-- src/services/syncService.js | 32 +++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/models/api/responseModels.js b/src/models/api/responseModels.js index e55a40b3b5..695edd4e8f 100644 --- a/src/models/api/responseModels.js +++ b/src/models/api/responseModels.js @@ -47,6 +47,11 @@ var ProfileResponse = function (response) { } }; +var KeysResponse = function (response) { + this.privateKey = response.PrivateKey; + this.publicKey = response.PublicKey; +}; + var ProfileOrganizationResponse = function (response) { this.id = response.Id; this.name = response.Name; diff --git a/src/services/apiService.js b/src/services/apiService.js index 65776401a7..b2d9554ca5 100644 --- a/src/services/apiService.js +++ b/src/services/apiService.js @@ -1,8 +1,8 @@ function ApiService(tokenService, appIdService, utilsService, logoutCallback) { - this.baseUrl = 'http://localhost:4000'; // Desktop + //this.baseUrl = 'http://localhost:4000'; // Desktop //this.baseUrl = 'http://192.168.1.8:4000'; // Desktop external //this.baseUrl = 'https://preview-api.bitwarden.com'; // Preview - //this.baseUrl = 'https://api.bitwarden.com'; // Production + this.baseUrl = 'https://api.bitwarden.com'; // Production this.tokenService = tokenService; this.logoutCallback = logoutCallback; this.appIdService = appIdService; @@ -78,6 +78,25 @@ function initApiService() { }); }; + ApiService.prototype.getKeys = function (success, error) { + var self = this; + handleTokenState(self).then(function (token) { + $.ajax({ + type: 'GET', + url: self.baseUrl + '/accounts/keys?access_token2=' + token, + dataType: 'json', + success: function (response) { + success(new KeysResponse(response)); + }, + error: function (jqXHR, textStatus, errorThrown) { + handleError(error, jqXHR, false, self); + } + }); + }, function (jqXHR) { + handleError(error, jqXHR, true, self); + }); + }; + ApiService.prototype.postPasswordHint = function (request, success, error) { var self = this; $.ajax({ diff --git a/src/services/syncService.js b/src/services/syncService.js index 030c15576d..4a98c8aeae 100644 --- a/src/services/syncService.js +++ b/src/services/syncService.js @@ -85,8 +85,34 @@ function initSyncService() { var self = this; self.apiService.getProfile(function (response) { - self.cryptoService.setOrgKeys(response.organizations).then(function () { - deferred.resolve(); + self.cryptoService.getPrivateKey().then(function (privateKey) { + if (response.organizations && !privateKey) { + self.apiService.getKeys(function (keysResponse) { + if (keysResponse.privateKey) { + self.cryptoService.setEncPrivateKey(keysResponse.privateKey).then(function () { + return self.cryptoService.setOrgKeys(response.organizations); + }, function () { + deferred.reject(); + }).then(function () { + deferred.resolve(); + }, function () { + deferred.reject(); + }); + } + else { + deferred.resolve(); + } + }, function () { + deferred.reject(); + }); + } + else { + self.cryptoService.setOrgKeys(response.organizations).then(function () { + deferred.resolve(); + }, function () { + deferred.reject(); + }); + } }); }, function () { deferred.reject(); @@ -103,7 +129,7 @@ function initSyncService() { var folders = {}; for (var i = 0; i < response.data.length; i++) { - folders[response.data.id] = new FolderData(response.data[i], userId); + folders[response.data[i].id] = new FolderData(response.data[i], userId); } self.folderService.replace(folders, function () {