2013-10-22 08:48:38 -04:00
|
|
|
define(function(require) {
|
2013-10-21 07:10:42 -04:00
|
|
|
'use strict';
|
|
|
|
|
2013-10-22 08:48:38 -04:00
|
|
|
var angular = require('angular'),
|
2013-11-14 14:44:57 -05:00
|
|
|
errorUtil = require('js/util/error'),
|
2013-10-22 08:48:38 -04:00
|
|
|
appController = require('js/app-controller');
|
|
|
|
|
|
|
|
var LoginExistingCtrl = function($scope, $location) {
|
2013-11-14 14:44:57 -05:00
|
|
|
// global state... inherited to all child scopes
|
|
|
|
$scope.$root.state = {};
|
|
|
|
// attach global error handler
|
|
|
|
errorUtil.attachHandler($scope);
|
|
|
|
|
2013-11-04 08:20:14 -05:00
|
|
|
var emailDao = appController._emailDao;
|
|
|
|
|
2013-10-22 11:32:30 -04:00
|
|
|
$scope.incorrect = false;
|
|
|
|
|
2013-10-21 07:10:42 -04:00
|
|
|
$scope.confirmPassphrase = function() {
|
2013-11-04 08:20:14 -05:00
|
|
|
if (!$scope.passphrase) {
|
2013-10-22 11:32:30 -04:00
|
|
|
$scope.incorrect = true;
|
2013-10-22 08:48:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-22 11:32:30 -04:00
|
|
|
$scope.incorrect = false;
|
2013-11-04 08:20:14 -05:00
|
|
|
unlockCrypto();
|
|
|
|
};
|
2013-10-22 08:48:38 -04:00
|
|
|
|
2013-11-04 08:20:14 -05:00
|
|
|
function unlockCrypto() {
|
|
|
|
var userId = emailDao._account.emailAddress;
|
|
|
|
emailDao._keychain.getUserKeyPair(userId, function(err, keypair) {
|
2013-10-22 08:48:38 -04:00
|
|
|
if (err) {
|
2013-11-14 14:44:57 -05:00
|
|
|
$scope.onError(err);
|
2013-10-22 08:48:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-04 08:20:14 -05:00
|
|
|
keypair.privateKey = {
|
|
|
|
_id: keypair.publicKey._id,
|
|
|
|
userId: userId,
|
|
|
|
encryptedKey: $scope.key.privateKeyArmored
|
|
|
|
};
|
|
|
|
|
2013-12-03 14:25:39 -05:00
|
|
|
emailDao.unlock({
|
|
|
|
keypair: keypair,
|
|
|
|
passphrase: $scope.passphrase
|
|
|
|
}, function(err) {
|
2013-10-22 08:48:38 -04:00
|
|
|
if (err) {
|
2013-11-04 08:20:14 -05:00
|
|
|
$scope.incorrect = true;
|
2013-11-14 14:44:57 -05:00
|
|
|
$scope.onError(err);
|
2013-10-22 08:48:38 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-11-04 08:20:14 -05:00
|
|
|
|
|
|
|
emailDao._keychain.putUserKeyPair(keypair, onUnlock);
|
2013-10-22 08:48:38 -04:00
|
|
|
});
|
2013-11-04 08:20:14 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onUnlock(err) {
|
|
|
|
if (err) {
|
2013-11-14 14:44:57 -05:00
|
|
|
$scope.onError(err);
|
2013-11-04 08:20:14 -05:00
|
|
|
return;
|
2013-10-22 08:48:38 -04:00
|
|
|
}
|
2013-10-21 07:10:42 -04:00
|
|
|
|
2013-10-22 08:48:38 -04:00
|
|
|
$location.path('/desktop');
|
|
|
|
$scope.$apply();
|
|
|
|
}
|
2013-10-21 07:10:42 -04:00
|
|
|
};
|
|
|
|
|
2013-10-22 08:48:38 -04:00
|
|
|
var ngModule = angular.module('login-new-device', []);
|
|
|
|
ngModule.directive('fileReader', function() {
|
|
|
|
return function(scope, elm) {
|
|
|
|
elm.bind('change', function(e) {
|
|
|
|
var files = e.target.files,
|
|
|
|
reader = new FileReader();
|
|
|
|
|
|
|
|
if (files.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-22 11:32:30 -04:00
|
|
|
reader.onload = function(e) {
|
|
|
|
var rawKeys = e.target.result,
|
|
|
|
index = rawKeys.indexOf('-----BEGIN PGP PRIVATE KEY BLOCK-----');
|
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
console.error('Erroneous key file format!');
|
|
|
|
return;
|
|
|
|
}
|
2013-10-22 10:12:18 -04:00
|
|
|
|
2013-10-22 11:32:30 -04:00
|
|
|
scope.key = {
|
|
|
|
publicKeyArmored: rawKeys.substring(0, index),
|
|
|
|
privateKeyArmored: rawKeys.substring(index, rawKeys.length)
|
2013-10-22 08:48:38 -04:00
|
|
|
};
|
2013-10-22 11:32:30 -04:00
|
|
|
scope.$apply();
|
|
|
|
};
|
2013-10-22 08:48:38 -04:00
|
|
|
reader.readAsText(files[0]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2013-10-21 07:10:42 -04:00
|
|
|
return LoginExistingCtrl;
|
|
|
|
});
|