mail/src/js/controller/login-initial.js

141 lines
4.3 KiB
JavaScript
Raw Normal View History

2013-10-21 07:10:42 -04:00
define(function(require) {
'use strict';
2013-10-23 11:17:36 -04:00
var appController = require('js/app-controller'),
errorUtil = require('js/util/error'),
2013-10-23 11:17:36 -04:00
dl = require('js/util/download');
2013-10-21 07:10:42 -04:00
var LoginInitialCtrl = function($scope, $location) {
2013-10-23 11:17:36 -04:00
var emailDao = appController._emailDao,
states;
2013-11-11 07:28:05 -05:00
// global state... inherited to all child scopes
$scope.$root.state = {};
// attach global error handler
errorUtil.attachHandler($scope);
2013-11-11 07:28:05 -05:00
states = {
2013-10-23 11:17:36 -04:00
IDLE: 1,
PROCESSING: 2,
DONE: 4
};
2013-11-11 07:28:05 -05:00
$scope.state.ui = states.IDLE; // initial state
2013-10-23 11:17:36 -04:00
//
// scope functions
//
2013-10-21 07:10:42 -04:00
2014-01-09 04:50:43 -05:00
/*
* Taken from jQuery validate.password plug-in 1.0
* http://bassistance.de/jquery-plugins/jquery-plugin-validate.password/
*
* Copyright (c) 2009 Jörn Zaefferer
*
* Licensed under the MIT
* http://www.opensource.org/licenses/mit-license.php
*/
$scope.checkPassphraseQuality = function() {
var passphrase = $scope.state.passphrase;
$scope.passphraseRating = 0;
var LOWER = /[a-z]/,
UPPER = /[A-Z]/,
DIGIT = /[0-9]/,
DIGITS = /[0-9].*[0-9]/,
SPECIAL = /[^a-zA-Z0-9]/,
SAME = /^(.)\1+$/;
function uncapitalize(str) {
return str.substring(0, 1).toLowerCase() + str.substring(1);
}
if (!passphrase || passphrase.length < 10) {
$scope.passphraseMsg = 'Too short';
return;
}
if (SAME.test(passphrase)) {
$scope.passphraseMsg = 'Very weak';
return;
}
var lower = LOWER.test(passphrase),
upper = UPPER.test(uncapitalize(passphrase)),
digit = DIGIT.test(passphrase),
digits = DIGITS.test(passphrase),
special = SPECIAL.test(passphrase);
if (lower && upper && digit || lower && digits || upper && digits || special) {
$scope.passphraseMsg = 'Strong';
$scope.passphraseRating = 3;
} else if (lower && upper || lower && digit || upper && digit) {
$scope.passphraseMsg = 'Good';
$scope.passphraseRating = 2;
} else {
$scope.passphraseMsg = 'Weak';
$scope.passphraseRating = 1;
}
};
2013-10-21 07:10:42 -04:00
$scope.confirmPassphrase = function() {
2013-11-11 07:28:05 -05:00
var passphrase = $scope.state.passphrase,
confirmation = $scope.state.confirmation;
2013-10-21 07:10:42 -04:00
if (!passphrase || passphrase !== confirmation) {
return;
}
$scope.setState(states.PROCESSING);
2013-10-23 11:17:36 -04:00
setTimeout(function() {
emailDao.unlock({
passphrase: passphrase
}, function(err) {
2013-10-23 11:17:36 -04:00
if (err) {
$scope.setState(states.IDLE);
$scope.onError(err);
2013-10-23 11:17:36 -04:00
return;
}
2013-10-21 07:10:42 -04:00
$scope.setState(states.DONE);
$scope.$apply();
2013-10-23 11:17:36 -04:00
});
}, 500);
};
2013-10-21 07:10:42 -04:00
2013-10-23 11:17:36 -04:00
$scope.exportKeypair = function() {
// export keys from keychain
emailDao._crypto.exportKeys(function(err, keys) {
2013-10-21 07:10:42 -04:00
if (err) {
$scope.onError(err);
2013-10-21 07:10:42 -04:00
return;
}
2013-10-23 11:17:36 -04:00
var id = keys.keyId.substring(8, keys.keyId.length);
2013-11-04 08:20:14 -05:00
dl.createDownload({
content: keys.publicKeyArmored + keys.privateKeyArmored,
filename: 'whiteout_mail_' + emailDao._account.emailAddress + '_' + id + '.asc',
2013-11-04 08:20:14 -05:00
contentType: 'text/plain'
}, onSave);
2013-10-23 11:17:36 -04:00
});
2013-11-04 08:20:14 -05:00
function onSave(err) {
2013-10-23 11:17:36 -04:00
if (err) {
$scope.onError(err);
2013-10-23 11:17:36 -04:00
return;
}
2013-11-04 08:20:14 -05:00
$scope.proceed();
$scope.$apply();
}
2013-10-21 07:10:42 -04:00
};
2013-11-04 08:20:14 -05:00
$scope.proceed = function() {
2013-10-21 07:10:42 -04:00
$location.path('/desktop');
2013-11-04 08:20:14 -05:00
};
2013-10-23 11:17:36 -04:00
$scope.setState = function(state) {
2013-11-11 07:28:05 -05:00
$scope.state.ui = state;
};
2013-10-21 07:10:42 -04:00
};
return LoginInitialCtrl;
2013-10-23 11:17:36 -04:00
});