mail/src/js/controller/login-new-device.js

104 lines
3.3 KiB
JavaScript
Raw Normal View History

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) {
var emailDao = appController._emailDao,
pgp = appController._crypto;
2013-11-04 08:20:14 -05:00
2013-10-22 11:32:30 -04:00
$scope.incorrect = false;
2013-10-21 07:10:42 -04:00
$scope.confirmPassphrase = function() {
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;
// check if user already has a public key on the key server
2013-11-04 08:20:14 -05:00
emailDao._keychain.getUserKeyPair(userId, function(err, keypair) {
2013-10-22 08:48:38 -04:00
if (err) {
$scope.onError(err);
2013-10-22 08:48:38 -04:00
return;
}
keypair = keypair || {};
// set parsed private key
2013-11-04 08:20:14 -05:00
keypair.privateKey = {
_id: pgp.getKeyId($scope.key.privateKeyArmored),
2013-11-04 08:20:14 -05:00
userId: userId,
encryptedKey: $scope.key.privateKeyArmored
};
if (!keypair.publicKey) {
// there is no public key on the key server yet... use parsed
keypair.publicKey = {
_id: pgp.getKeyId($scope.key.publicKeyArmored),
userId: userId,
publicKey: $scope.key.publicKeyArmored
};
}
// import and validate keypair
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;
$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) {
$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 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;
});