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

140 lines
5.2 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-23 05:16:02 +02:00
$state, constants, $analytics, $uibModal, $timeout, $window) {
2017-03-31 04:06:01 +02:00
$scope.state = $state;
2017-06-23 18:39:56 +02:00
$scope.twoFactorProviderConstants = constants.twoFactorProvider;
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-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) {
$scope.loginPromise = authService.logIn(model.email, model.masterPassword);
$scope.loginPromise.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;
$scope.twoFactorProvider = parseInt(Object.keys(twoFactorProviders)[0]);
$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-20 23:06:14 +02:00
$scope.twoFactor = function (token) {
2017-06-23 16:41:57 +02:00
$scope.twoFactorPromise = authService.logIn(_email, _masterPassword, token, $scope.twoFactorProvider, true);
2015-12-09 04:35:05 +01:00
$scope.twoFactorPromise.then(function () {
$analytics.eventTrack('Logged In From Two-step');
loggedInGo();
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
});
});
};
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];
Duo.init({
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);
if (challenges.length < 1) {
return;
}
$window.u2f.sign(challenges[0].appId, challenges[0].challenge, [{
version: challenges[0].version,
keyHandle: challenges[0].keyHandle
}], function (data) {
console.log('call back data:');
console.log(data);
$scope.twoFactor(JSON.stringify(data));
});
}
2017-06-21 21:17:44 +02:00
}
2015-12-09 04:35:05 +01:00
});