From e49948b5121559fd78c447d7a6f5b2586e27b6bd Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 20 Jun 2017 09:21:53 -0400 Subject: [PATCH] two factor email setup --- src/app/services/apiService.js | 1 + src/app/settings/settingsTwoStepController.js | 14 +++ .../settingsTwoStepEmailController.js | 97 +++++++++++++++++++ .../settings/views/settingsTwoStepEmail.html | 77 +++++++++++++++ src/index.html | 1 + 5 files changed, 190 insertions(+) create mode 100644 src/app/settings/settingsTwoStepEmailController.js create mode 100644 src/app/settings/views/settingsTwoStepEmail.html diff --git a/src/app/services/apiService.js b/src/app/services/apiService.js index 82070316d8..e9dc9ffb30 100644 --- a/src/app/services/apiService.js +++ b/src/app/services/apiService.js @@ -115,6 +115,7 @@ list: { method: 'GET', params: {} }, getEmail: { url: _apiUri + '/two-factor/get-email', method: 'POST', params: {} }, getAuthenticator: { url: _apiUri + '/two-factor/get-authenticator', method: 'POST', params: {} }, + sendEmail: { url: _apiUri + '/two-factor/send-email', method: 'POST', params: {} }, putEmail: { url: _apiUri + '/two-factor/email', method: 'POST', params: {} }, putAuthenticator: { url: _apiUri + '/two-factor/authenticator', method: 'POST', params: {} }, disable: { url: _apiUri + '/two-factor/disable', method: 'POST', params: {} }, diff --git a/src/app/settings/settingsTwoStepController.js b/src/app/settings/settingsTwoStepController.js index 2fb6ea8ae0..2cbe6a0c63 100644 --- a/src/app/settings/settingsTwoStepController.js +++ b/src/app/settings/settingsTwoStepController.js @@ -67,6 +67,20 @@ authenticatorModal.result.then(function () { + }); + } + else if(provider.type === constants.twoFactorProvider.email) { + var emailModal = $uibModal.open({ + animation: true, + templateUrl: 'app/settings/views/settingsTwoStepEmail.html', + controller: 'settingsTwoStepEmailController', + resolve: { + enabled: function () { return provider.enabled; } + } + }); + + emailModal.result.then(function () { + }); } }; diff --git a/src/app/settings/settingsTwoStepEmailController.js b/src/app/settings/settingsTwoStepEmailController.js new file mode 100644 index 0000000000..04d30409be --- /dev/null +++ b/src/app/settings/settingsTwoStepEmailController.js @@ -0,0 +1,97 @@ +angular + .module('bit.settings') + + .controller('settingsTwoStepEmailController', function ($scope, apiService, $uibModalInstance, cryptoService, + authService, toastr, $analytics, constants) { + $analytics.eventTrack('settingsTwoStepEmailController', { category: 'Modal' }); + var _profile = null, + _masterPasswordHash; + + $scope.updateModel = { + token: null, + email: null + }; + + $scope.auth = function (model) { + _masterPasswordHash = cryptoService.hashPassword(model.masterPassword); + + var response = null; + $scope.authPromise = apiService.twoFactor.getEmail({}, { + masterPasswordHash: _masterPasswordHash + }).$promise.then(function (apiResponse) { + response = apiResponse; + return authService.getUserProfile(); + }).then(function (profile) { + _profile = profile; + $scope.enabled = response.Enabled; + $scope.updateModel.email = $scope.enabled ? response.Email : _profile.email; + $scope.authed = true; + }); + }; + + $scope.sendEmail = function (model) { + $scope.emailError = false; + $scope.emailSuccess = false; + + if (!model || !model.email || model.email.indexOf('@') < 0) { + $scope.emailError = true; + $scope.emailSuccess = false; + return; + } + + $scope.emailLoading = true; + apiService.twoFactor.sendEmail({}, { + masterPasswordHash: _masterPasswordHash, + email: model.email + }, function (response) { + $scope.emailError = false; + $scope.emailSuccess = true; + $scope.emailLoading = false; + }, function (response) { + $scope.emailError = true; + $scope.emailSuccess = false; + $scope.emailLoading = false; + }); + }; + + $scope.submit = function (model) { + if (!model || !model.token) { + disable(); + return; + } + + update(model); + }; + + function disable() { + if (!confirm('Are you sure you want to disable the email provider?')) { + return; + } + + $scope.submitPromise = apiService.twoFactor.disable({}, { + masterPasswordHash: _masterPasswordHash, + type: constants.twoFactorProvider.email + }, function (response) { + $analytics.eventTrack('Disabled Two-step Email'); + toastr.success('Email has been disabled.'); + $scope.close(); + }).$promise; + } + + function update(model) { + $scope.submitPromise = apiService.twoFactor.putEmail({}, { + email: model.email.toLowerCase().trim(), + token: model.token.replace(' ', ''), + masterPasswordHash: _masterPasswordHash + }, function (response) { + $analytics.eventTrack('Enabled Two-step Email'); + $scope.enabled = response.Enabled; + model.email = response.Email; + model.token = null; + }).$promise; + } + + $scope.close = function () { + $uibModalInstance.close(); + }; + }); diff --git a/src/app/settings/views/settingsTwoStepEmail.html b/src/app/settings/views/settingsTwoStepEmail.html new file mode 100644 index 0000000000..bebfd50ec6 --- /dev/null +++ b/src/app/settings/views/settingsTwoStepEmail.html @@ -0,0 +1,77 @@ + +
+ + +
+
+ + +
diff --git a/src/index.html b/src/index.html index d6ac8f447f..4d61b9335d 100644 --- a/src/index.html +++ b/src/index.html @@ -189,6 +189,7 @@ +