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'),
|
|
|
|
appController = require('js/app-controller');
|
|
|
|
|
|
|
|
var LoginExistingCtrl = function($scope, $location) {
|
2013-10-21 07:10:42 -04:00
|
|
|
$scope.confirmPassphrase = function() {
|
2013-10-22 08:48:38 -04:00
|
|
|
var passphrase = $scope.passphrase,
|
|
|
|
emailDao = appController._emailDao;
|
|
|
|
|
|
|
|
if (!passphrase) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlockCrypto(imapLogin);
|
|
|
|
|
|
|
|
function unlockCrypto(callback) {
|
|
|
|
var userId = emailDao._account.emailAddress;
|
|
|
|
emailDao._keychain.getUserKeyPair(userId, function(err, keypair) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
keypair.privateKey = {
|
|
|
|
_id: keypair.publicKey._id,
|
|
|
|
userId: userId,
|
2013-10-22 10:12:18 -04:00
|
|
|
encryptedKey: $scope.key.privateKeyArmored
|
2013-10-22 08:48:38 -04:00
|
|
|
};
|
|
|
|
emailDao.unlock(keypair, passphrase, function(err) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emailDao._keychain.putUserKeyPair(keypair, callback);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function imapLogin(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// login to imap backend
|
|
|
|
appController._emailDao.imapLogin(function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onLogin();
|
|
|
|
});
|
|
|
|
}
|
2013-10-21 07:10:42 -04:00
|
|
|
};
|
|
|
|
|
2013-10-22 08:48:38 -04:00
|
|
|
function onLogin() {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.onload = (function(scope) {
|
|
|
|
return function(e) {
|
2013-10-22 10:12:18 -04:00
|
|
|
var rawKeys = e.target.result,
|
|
|
|
index = rawKeys.indexOf('-----BEGIN PGP PRIVATE KEY BLOCK-----');
|
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
console.error('Erroneous key file format!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.key = {
|
|
|
|
publicKeyArmored: rawKeys.substring(0,index),
|
|
|
|
privateKeyArmored: rawKeys.substring(index,rawKeys.length)
|
|
|
|
};
|
2013-10-22 08:48:38 -04:00
|
|
|
};
|
|
|
|
})(scope);
|
|
|
|
reader.readAsText(files[0]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2013-10-21 07:10:42 -04:00
|
|
|
return LoginExistingCtrl;
|
|
|
|
});
|