From d6c26bd3446763b556ef84ae1de178e091fe5722 Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Thu, 28 Jun 2018 19:26:28 -0500 Subject: [PATCH] Auth async --- .../javascripts/app/controllers/home.js | 2 +- .../app/directives/views/accountMenu.js | 5 +- .../app/directives/views/passwordWizard.js | 2 +- .../javascripts/app/services/authManager.js | 73 +++++++++---------- 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/app/assets/javascripts/app/controllers/home.js b/app/assets/javascripts/app/controllers/home.js index ec0f7c1c6..488012ccc 100644 --- a/app/assets/javascripts/app/controllers/home.js +++ b/app/assets/javascripts/app/controllers/home.js @@ -303,7 +303,7 @@ angular.module('app') }) } } else { - authManager.login(server, email, pw, false, false, {}, function(response){ + authManager.login(server, email, pw, false, false, {}).then((response) => { window.location.reload(); }) } diff --git a/app/assets/javascripts/app/directives/views/accountMenu.js b/app/assets/javascripts/app/directives/views/accountMenu.js index ccbdc3682..64cc0cf51 100644 --- a/app/assets/javascripts/app/directives/views/accountMenu.js +++ b/app/assets/javascripts/app/directives/views/accountMenu.js @@ -59,8 +59,7 @@ class AccountMenu { $scope.formData.status = "Generating Login Keys..."; $timeout(function(){ authManager.login($scope.formData.url, $scope.formData.email, $scope.formData.user_password, - $scope.formData.ephemeral, $scope.formData.strictSignin, extraParams, - (response) => { + $scope.formData.ephemeral, $scope.formData.strictSignin, extraParams).then((response) => { if(!response || response.error) { syncManager.unlockSyncing(); @@ -108,7 +107,7 @@ class AccountMenu { $scope.formData.status = "Generating Account Keys..."; $timeout(function(){ - authManager.register($scope.formData.url, $scope.formData.email, $scope.formData.user_password, $scope.formData.ephemeral ,function(response){ + authManager.register($scope.formData.url, $scope.formData.email, $scope.formData.user_password, $scope.formData.ephemeral).then((response) => { if(!response || response.error) { $scope.formData.status = null; var error = response ? response.error : {message: "An unknown error occured."} diff --git a/app/assets/javascripts/app/directives/views/passwordWizard.js b/app/assets/javascripts/app/directives/views/passwordWizard.js index 430d6ff4c..27d6c65a0 100644 --- a/app/assets/javascripts/app/directives/views/passwordWizard.js +++ b/app/assets/javascripts/app/directives/views/passwordWizard.js @@ -209,7 +209,7 @@ class PasswordWizard { // perform a sync beforehand to pull in any last minutes changes before we change the encryption key (and thus cant decrypt new changes) syncManager.sync((response) => { - authManager.changePassword(authManager.user.email, currentServerPw, newKeys, newAuthParams, (response) => { + authManager.changePassword(authManager.user.email, currentServerPw, newKeys, newAuthParams).then((response) => { if(response.error) { alert(response.error.message ? response.error.message : "There was an error changing your password. Please try again."); $timeout(() => callback(false)); diff --git a/app/assets/javascripts/app/services/authManager.js b/app/assets/javascripts/app/services/authManager.js index 4c6afb0e7..44d0a786f 100644 --- a/app/assets/javascripts/app/services/authManager.js +++ b/app/assets/javascripts/app/services/authManager.js @@ -56,17 +56,6 @@ class AuthManager extends SFAuthManager { return this._authParams; } - keys() { - if(!this._keys) { - var mk = this.storageManager.getItemSync("mk"); - if(!mk) { - return null; - } - this._keys = {mk: mk, ak: this.storageManager.getItemSync("ak")}; - } - return this._keys; - } - async protocolVersion() { var authParams = this.getAuthParams(); if(authParams && authParams.version) { @@ -82,26 +71,51 @@ class AuthManager extends SFAuthManager { } } - getAuthParamsForEmail(url, email, extraParams, callback) { - super.getAuthParamsForEmail(url, email, extraParams, (response) => { - callback(response); - }) + async resolveResponseInTimeout(response) { + return new Promise((resolve, reject) => { + this.$timeout(() => { + resolve(response); + }); + }); } - login(url, email, password, ephemeral, strictSignin, extraParams, callback) { - super.login(url, email, password, ephemeral, strictSignin, extraParams, (response) => { + async getAuthParamsForEmail(url, email, extraParams) { + return super.getAuthParamsForEmail(url, email, extraParams); + } + + async login(url, email, password, ephemeral, strictSignin, extraParams) { + return super.login(url, email, password, strictSignin, extraParams).then((response) => { if(!response.error) { this.setEphemeral(ephemeral); this.handleAuthResponse(response, email, url, authParams, keys); this.checkForSecurityUpdate(); } - this.$timeout(() => callback(response)); + + return this.resolveResponseInTimeout(response); }) } - handleAuthResponse(response, email, url, authParams, keys) { + async register(url, email, password, ephemeral) { + super.register(url, email, password).then((response) => { + if(!response.error) { + this.setEphemeral(ephemeral); + } + return this.resolveResponseInTimeout(response); + }) + } + + async changePassword(email, current_server_pw, newKeys, newAuthParams) { + super.changePassword(email, current_server_pw, newKeys, newAuthParams).then((response) => { + if(!response.error) { + this.checkForSecurityUpdate(); + } + return this.resolveResponseInTimeout(response); + }) + } + + async handleAuthResponse(response, email, url, authParams, keys) { try { - super.handleAuthResponse(response, email, url, authParams, keys); + await super.handleAuthResponse(response, email, url, authParams, keys); this._authParams = authParams; this.user = response.user; this.storageManager.setItem("user", JSON.stringify(response.user)); @@ -110,25 +124,6 @@ class AuthManager extends SFAuthManager { } } - register(url, email, password, ephemeral, callback) { - super.register(url, email, password, ephemeral, (response) => { - if(!response.error) { - this.setEphemeral(ephemeral); - } - callback(response); - }) - } - - changePassword(email, current_server_pw, newKeys, newAuthParams, callback) { - super.changePassword(email, current_server_pw, newKeys, newAuthParams, (response) => { - if(!response.error) { - // Allows security update status to be changed if neccessary - this.checkForSecurityUpdate(); - } - callback(response); - }) - } - checkForSecurityUpdate() { if(this.offline()) { return false;