2015-12-09 04:35:05 +01:00
|
|
|
angular
|
|
|
|
.module('bit.accounts')
|
|
|
|
|
2017-01-28 07:19:43 +01:00
|
|
|
.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-04-25 16:46:54 +02:00
|
|
|
|
|
|
|
var returnState;
|
|
|
|
if (!$state.params.returnState && $state.params.org) {
|
|
|
|
returnState = {
|
|
|
|
name: 'backend.user.settingsCreateOrg',
|
|
|
|
params: { plan: $state.params.org }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
returnState = $state.params.returnState;
|
|
|
|
}
|
|
|
|
|
2017-04-11 00:55:18 +02:00
|
|
|
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;
|
2017-01-28 07:19:43 +01:00
|
|
|
|
2015-12-09 04:35:05 +01:00
|
|
|
$scope.login = function (model) {
|
|
|
|
$scope.loginPromise = authService.logIn(model.email, model.masterPassword);
|
|
|
|
|
2017-01-28 07:19:43 +01:00
|
|
|
$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(
|
2017-04-11 00:55:18 +02:00
|
|
|
constants.rememberedEmailCookieName,
|
2015-12-09 04:35:05 +01:00
|
|
|
model.email,
|
|
|
|
{ expires: cookieExpiration });
|
|
|
|
}
|
|
|
|
else {
|
2017-04-11 00:55:18 +02:00
|
|
|
$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]);
|
2017-01-28 07:19:43 +01:00
|
|
|
|
2016-08-12 02:43:05 +02:00
|
|
|
$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 {
|
2016-08-12 02:43:05 +02:00
|
|
|
$analytics.eventTrack('Logged In');
|
2017-03-23 21:57:47 +01:00
|
|
|
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 () {
|
2016-08-12 02:43:05 +02:00
|
|
|
$analytics.eventTrack('Logged In From Two-step');
|
2017-03-23 21:57:47 +01:00
|
|
|
loggedInGo();
|
2015-12-09 04:35:05 +01:00
|
|
|
});
|
|
|
|
};
|
2017-03-23 21:57:47 +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-03-23 21:57:47 +01: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
|
|
|
});
|