1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-10 06:08:34 +02:00
bitwarden-browser/src/app/accounts/accountsLoginController.js

222 lines
8.3 KiB
JavaScript
Raw Normal View History

2015-12-09 04:35:05 +01:00
angular
.module('bit.accounts')
.controller('accountsLoginController', function ($scope, $rootScope, $cookies, apiService, cryptoService, authService,
2017-06-24 15:19:04 +02:00
$state, constants, $analytics, $uibModal, $timeout, $window, $filter, toastr) {
2017-03-31 04:06:01 +02:00
$scope.state = $state;
2017-06-23 18:39:56 +02:00
$scope.twoFactorProviderConstants = constants.twoFactorProvider;
2017-06-23 22:31:55 +02:00
$scope.rememberTwoFactor = { checked: false };
2017-06-24 22:59:01 +02:00
if ($state.current.name.indexOf('twoFactor') > -1 && (!$scope.twoFactorProviders || !$scope.twoFactorProviders.length)) {
$state.go('frontend.login.info', { returnState: returnState });
}
var returnState;
if (!$state.params.returnState && $state.params.org) {
returnState = {
name: 'backend.user.settingsCreateOrg',
params: { plan: $state.params.org }
};
}
else {
returnState = $state.params.returnState;
}
var rememberedEmail = $cookies.get(constants.rememberedEmailCookieName);
2017-03-29 03:16:44 +02:00
if (rememberedEmail || $state.params.email) {
2015-12-09 04:35:05 +01:00
$scope.model = {
2017-03-29 03:16:44 +02:00
email: $state.params.email ? $state.params.email : rememberedEmail,
rememberEmail: rememberedEmail !== null
2015-12-09 04:35:05 +01:00
};
2017-06-24 22:59:01 +02:00
$timeout(function () {
$("#masterPassword").focus();
});
}
else {
$timeout(function () {
$("#email").focus();
});
2015-12-09 04:35:05 +01:00
}
2017-06-20 23:06:14 +02:00
var _email,
_masterPassword;
$scope.twoFactorProviders = null;
$scope.twoFactorProvider = null;
2015-12-09 04:35:05 +01:00
$scope.login = function (model) {
2017-06-24 15:19:04 +02:00
$scope.loginPromise = authService.logIn(model.email, model.masterPassword).then(function (twoFactorProviders) {
2015-12-09 04:35:05 +01:00
if (model.rememberEmail) {
var cookieExpiration = new Date();
cookieExpiration.setFullYear(cookieExpiration.getFullYear() + 10);
$cookies.put(
constants.rememberedEmailCookieName,
2015-12-09 04:35:05 +01:00
model.email,
{ expires: cookieExpiration });
}
else {
$cookies.remove(constants.rememberedEmailCookieName);
2015-12-09 04:35:05 +01:00
}
2017-06-20 23:06:14 +02:00
if (twoFactorProviders && Object.keys(twoFactorProviders).length > 0) {
_email = model.email;
_masterPassword = model.masterPassword;
$scope.twoFactorProviders = twoFactorProviders;
2017-06-23 22:31:55 +02:00
$scope.twoFactorProvider = getDefaultProvider(twoFactorProviders);
$analytics.eventTrack('Logged In To Two-step');
2017-06-20 23:06:14 +02:00
$state.go('frontend.login.twoFactor', { returnState: returnState }).then(function () {
$timeout(function () {
$("#code").focus();
2017-06-21 21:17:44 +02:00
init();
2017-06-20 23:06:14 +02:00
});
});
2015-12-09 04:35:05 +01:00
}
else {
$analytics.eventTrack('Logged In');
loggedInGo();
2015-12-09 04:35:05 +01:00
}
2017-06-23 22:31:55 +02:00
model.masterPassword = '';
2015-12-09 04:35:05 +01:00
});
};
2017-06-23 22:31:55 +02:00
function getDefaultProvider(twoFactorProviders) {
var keys = Object.keys(twoFactorProviders);
var providerType = null;
var providerPriority = -1;
for (var i = 0; i < keys.length; i++) {
var provider = $filter('filter')(constants.twoFactorProviderInfo, { type: keys[i], active: true });
if (provider.length && provider[0].priority > providerPriority) {
providerType = provider[0].type;
2017-06-26 21:32:34 +02:00
providerPriority = provider[0].priority;
2017-06-23 22:31:55 +02:00
}
}
return parseInt(providerType);
}
2017-06-20 23:06:14 +02:00
$scope.twoFactor = function (token) {
if ($scope.twoFactorProvider === constants.twoFactorProvider.email
|| $scope.twoFactorProvider === constants.twoFactorProvider.authenticator) {
token = token.replace(' ', '');
}
2017-06-23 22:31:55 +02:00
$scope.twoFactorPromise = authService.logIn(_email, _masterPassword, token, $scope.twoFactorProvider,
$scope.rememberTwoFactor.checked || false);
2015-12-09 04:35:05 +01:00
$scope.twoFactorPromise.then(function () {
$analytics.eventTrack('Logged In From Two-step');
loggedInGo();
2017-06-24 22:59:01 +02:00
}, function () {
if ($scope.twoFactorProvider === constants.twoFactorProvider.u2f) {
init();
}
2015-12-09 04:35:05 +01:00
});
};
2017-06-20 23:06:14 +02:00
$scope.anotherMethod = function () {
var modal = $uibModal.open({
animation: true,
templateUrl: 'app/accounts/views/accountsTwoFactorMethods.html',
controller: 'accountsTwoFactorMethodsController',
resolve: {
providers: function () { return $scope.twoFactorProviders; }
}
});
modal.result.then(function (provider) {
$scope.twoFactorProvider = provider;
$timeout(function () {
$("#code").focus();
2017-06-21 21:17:44 +02:00
init();
2017-06-20 23:06:14 +02:00
});
});
};
2017-06-24 15:19:04 +02:00
$scope.sendEmail = function (doToast) {
if ($scope.twoFactorProvider !== constants.twoFactorProvider.email) {
return;
}
var key = cryptoService.makeKey(_masterPassword, _email);
var hash = cryptoService.hashPassword(_masterPassword, key);
apiService.twoFactor.sendEmailLogin({
email: _email,
masterPasswordHash: hash
}, function () {
if (doToast) {
2017-06-24 17:55:39 +02:00
toastr.success('Verification email sent to ' + $scope.twoFactorEmail + '.');
2017-06-24 15:19:04 +02:00
}
}, function () {
toastr.error('Could not send verification email.');
});
};
function loggedInGo() {
if (returnState) {
$state.go(returnState.name, returnState.params);
}
else {
$state.go('backend.user.vault');
}
}
2017-06-21 21:17:44 +02:00
function init() {
if ($scope.twoFactorProvider === constants.twoFactorProvider.duo) {
var params = $scope.twoFactorProviders[constants.twoFactorProvider.duo];
2017-06-23 22:31:55 +02:00
$window.Duo.init({
2017-06-21 21:17:44 +02:00
host: params.Host,
sig_request: params.Signature,
submit_callback: function (theForm) {
var response = $(theForm).find('input[name="sig_response"]').val();
$scope.twoFactor(response);
}
});
}
2017-06-23 05:16:02 +02:00
else if ($scope.twoFactorProvider === constants.twoFactorProvider.u2f) {
var params = $scope.twoFactorProviders[constants.twoFactorProvider.u2f];
var challenges = JSON.parse(params.Challenges);
2017-06-23 22:31:55 +02:00
initU2f(challenges);
}
2017-06-24 15:19:04 +02:00
else if ($scope.twoFactorProvider === constants.twoFactorProvider.email) {
2017-06-24 17:55:39 +02:00
var params = $scope.twoFactorProviders[constants.twoFactorProvider.email];
$scope.twoFactorEmail = params.Email;
2017-06-24 15:19:04 +02:00
if (Object.keys($scope.twoFactorProviders).length > 1) {
$scope.sendEmail(false);
}
}
2017-06-23 22:31:55 +02:00
}
function initU2f(challenges) {
if (challenges.length < 1 || $scope.twoFactorProvider !== constants.twoFactorProvider.u2f) {
return;
}
console.log('listening for u2f key...');
$window.u2f.sign(challenges[0].appId, challenges[0].challenge, [{
version: challenges[0].version,
keyHandle: challenges[0].keyHandle
}], function (data) {
if ($scope.twoFactorProvider !== constants.twoFactorProvider.u2f) {
2017-06-23 05:16:02 +02:00
return;
}
2017-06-23 22:31:55 +02:00
if (data.errorCode) {
console.log(data.errorCode);
if (data.errorCode === 5) {
initU2f(challenges);
}
return;
}
$scope.twoFactor(JSON.stringify(data));
}, 10);
2017-06-21 21:17:44 +02:00
}
2015-12-09 04:35:05 +01:00
});