2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2013-10-21 07:10:42 -04:00
|
|
|
|
2014-12-16 10:14:16 -05:00
|
|
|
var LoginExistingCtrl = function($scope, $location, $routeParams, $q, email, auth, pgp, keychain) {
|
2014-11-19 14:54:59 -05:00
|
|
|
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
|
2013-10-22 11:32:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.incorrect = false;
|
2013-10-22 08:48:38 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.confirmPassphrase = function() {
|
2014-11-12 10:12:26 -05:00
|
|
|
if ($scope.form.$invalid || !$scope.key) {
|
|
|
|
$scope.errMsg = 'Please fill out all required fields!';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-16 10:14:16 -05:00
|
|
|
var userId = auth.emailAddress,
|
|
|
|
keypair;
|
2014-11-12 10:12:26 -05:00
|
|
|
|
2014-12-16 10:14:16 -05:00
|
|
|
return $q(function(resolve) {
|
|
|
|
$scope.busy = true;
|
|
|
|
$scope.errMsg = undefined; // reset error msg
|
|
|
|
$scope.incorrect = false;
|
|
|
|
resolve();
|
2013-10-22 08:48:38 -04:00
|
|
|
|
2014-12-16 10:14:16 -05:00
|
|
|
}).then(function() {
|
|
|
|
// check if user already has a public key on the key server
|
|
|
|
return keychain.getUserKeyPair(userId);
|
2014-03-05 14:14:23 -05:00
|
|
|
|
2014-12-16 10:14:16 -05:00
|
|
|
}).then(function(keys) {
|
|
|
|
keypair = keys || {};
|
2014-07-01 14:58:34 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// 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) {
|
2014-06-18 04:02:33 -04:00
|
|
|
try {
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.key.publicKeyArmored = pgp.extractPublicKey($scope.key.privateKeyArmored);
|
2014-06-18 04:02:33 -04:00
|
|
|
} catch (e) {
|
2014-12-16 10:14:16 -05:00
|
|
|
throw new Error('Error reading PGP key!');
|
2014-06-18 04:02:33 -04:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
2014-06-18 04:02:33 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// parse keypair params
|
|
|
|
var privKeyParams, pubKeyParams;
|
|
|
|
try {
|
|
|
|
privKeyParams = pgp.getKeyParams($scope.key.privateKeyArmored);
|
|
|
|
pubKeyParams = pgp.getKeyParams($scope.key.publicKeyArmored);
|
|
|
|
} catch (e) {
|
2014-12-16 10:14:16 -05:00
|
|
|
throw new Error('Error reading key paramaters!');
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set parsed private key
|
|
|
|
keypair.privateKey = {
|
|
|
|
_id: privKeyParams._id,
|
|
|
|
userId: userId,
|
|
|
|
userIds: privKeyParams.userIds,
|
|
|
|
encryptedKey: $scope.key.privateKeyArmored
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!keypair.publicKey) {
|
|
|
|
// there is no public key on the key server yet... use parsed
|
|
|
|
keypair.publicKey = {
|
|
|
|
_id: pubKeyParams._id,
|
2013-11-04 08:20:14 -05:00
|
|
|
userId: userId,
|
2014-10-02 16:05:44 -04:00
|
|
|
userIds: pubKeyParams.userIds,
|
|
|
|
publicKey: $scope.key.publicKeyArmored
|
2013-11-04 08:20:14 -05:00
|
|
|
};
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
2013-11-04 08:20:14 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// import and validate keypair
|
2014-12-16 10:14:16 -05:00
|
|
|
return email.unlock({
|
2014-10-02 16:05:44 -04:00
|
|
|
keypair: keypair,
|
|
|
|
passphrase: $scope.passphrase
|
2014-12-16 10:14:16 -05:00
|
|
|
}).catch(function(err) {
|
|
|
|
$scope.incorrect = true;
|
|
|
|
throw err;
|
2013-11-04 08:20:14 -05:00
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-16 10:14:16 -05:00
|
|
|
}).then(function() {
|
|
|
|
// perist keys locally
|
|
|
|
return keychain.putUserKeyPair(keypair);
|
2013-11-04 08:20:14 -05:00
|
|
|
|
2014-12-16 10:14:16 -05:00
|
|
|
}).then(function() {
|
|
|
|
// persist credentials locally
|
|
|
|
return auth.storeCredentials();
|
2013-10-21 07:10:42 -04:00
|
|
|
|
2014-12-16 10:14:16 -05:00
|
|
|
}).then(function() {
|
|
|
|
// go to main account screen
|
2014-12-02 10:12:42 -05:00
|
|
|
$location.path('/account');
|
2014-11-12 10:12:26 -05:00
|
|
|
|
2014-12-16 10:14:16 -05:00
|
|
|
}).catch(displayError);
|
|
|
|
};
|
|
|
|
|
|
|
|
function displayError(err) {
|
2014-11-12 10:12:26 -05:00
|
|
|
$scope.busy = false;
|
|
|
|
$scope.errMsg = err.errMsg || err.message;
|
2014-12-16 10:14:16 -05:00
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2014-07-01 13:49:19 -04:00
|
|
|
|
2014-10-08 06:34:34 -04:00
|
|
|
module.exports = LoginExistingCtrl;
|