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

136 lines
4.6 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._pgp;
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 || {};
// extract public key from private key block if missing in key file
if (!$scope.key.publicKeyArmored || $scope.key.publicKeyArmored.indexOf('-----BEGIN PGP PUBLIC KEY BLOCK-----') < 0) {
try {
$scope.key.publicKeyArmored = pgp.extractPublicKey($scope.key.privateKeyArmored);
} catch (e) {
$scope.onError(new Error('Error parsing public key from private key!'));
return;
}
}
// parse keypair params
var privKeyParams, pubKeyParams;
try {
privKeyParams = pgp.getKeyParams($scope.key.privateKeyArmored);
pubKeyParams = pgp.getKeyParams($scope.key.publicKeyArmored);
} catch (e) {
$scope.onError(new Error('Error reading key params!'));
return;
}
// set parsed private key
2013-11-04 08:20:14 -05:00
keypair.privateKey = {
_id: privKeyParams._id,
2013-11-04 08:20:14 -05:00
userId: userId,
userIds: privKeyParams.userIds,
2013-11-04 08:20:14 -05:00
encryptedKey: $scope.key.privateKeyArmored
};
if (!keypair.publicKey) {
// there is no public key on the key server yet... use parsed
keypair.publicKey = {
_id: pubKeyParams._id,
userId: userId,
userIds: pubKeyParams.userIds,
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
appController._auth.storeCredentials(function(err) {
if (err) {
return $scope.onError(err);
}
$location.path('/desktop');
$scope.$apply();
});
2013-10-22 08:48:38 -04:00
}
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-----'),
keyParts;
2013-10-22 11:32:30 -04:00
if (index === -1) {
scope.onError(new Error('Error parsing private PGP key block!'));
2013-10-22 11:32:30 -04:00
return;
}
keyParts = {
publicKeyArmored: rawKeys.substring(0, index).trim(),
privateKeyArmored: rawKeys.substring(index, rawKeys.length).trim()
2013-10-22 08:48:38 -04:00
};
scope.$apply(function() {
scope.key = keyParts;
});
2013-10-22 11:32:30 -04:00
};
2013-10-22 08:48:38 -04:00
reader.readAsText(files[0]);
});
};
});
2013-10-21 07:10:42 -04:00
return LoginExistingCtrl;
});