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'),
|
2013-11-14 14:44:57 -05:00
|
|
|
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 = {};
|
2013-11-14 14:44:57 -05:00
|
|
|
// 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-03-05 14:14:23 -05:00
|
|
|
$scope.importKey = function() {
|
|
|
|
$location.path('/login-new-device');
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
*/
|
2014-01-08 13:54:02 -05:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2013-11-11 11:56:51 -05:00
|
|
|
$scope.setState(states.PROCESSING);
|
2013-10-23 11:17:36 -04:00
|
|
|
setTimeout(function() {
|
2013-12-05 10:32:12 -05:00
|
|
|
emailDao.unlock({
|
|
|
|
passphrase: passphrase
|
|
|
|
}, function(err) {
|
2013-10-23 11:17:36 -04:00
|
|
|
if (err) {
|
2013-11-14 17:57:06 -05:00
|
|
|
$scope.setState(states.IDLE);
|
2013-11-14 14:44:57 -05:00
|
|
|
$scope.onError(err);
|
2013-10-23 11:17:36 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-10-21 07:10:42 -04:00
|
|
|
|
2013-11-14 17:57:06 -05: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) {
|
2013-11-14 14:44:57 -05:00
|
|
|
$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,
|
2014-01-07 13:23:03 -05:00
|
|
|
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) {
|
2013-11-14 14:44:57 -05:00
|
|
|
$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
|
|
|
|
2013-11-14 17:57:06 -05:00
|
|
|
$scope.setState = function(state) {
|
2013-11-11 07:28:05 -05:00
|
|
|
$scope.state.ui = state;
|
2013-11-11 11:56:51 -05:00
|
|
|
};
|
2013-10-21 07:10:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return LoginInitialCtrl;
|
2013-10-23 11:17:36 -04:00
|
|
|
});
|