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

110 lines
3.4 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
2013-10-21 07:10:42 -04:00
var LoginExistingCtrl = function($scope, $location, $routeParams, email, auth, pgp, keychain) {
!$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() {
if ($scope.form.$invalid || !$scope.key) {
$scope.errMsg = 'Please fill out all required fields!';
return;
}
$scope.busy = true;
$scope.errMsg = undefined; // reset error msg
2014-10-02 16:05:44 -04:00
$scope.incorrect = false;
2014-10-02 16:05:44 -04:00
unlockCrypto();
};
2013-10-22 08:48:38 -04:00
2014-10-02 16:05:44 -04:00
function unlockCrypto() {
var userId = auth.emailAddress;
2014-10-02 16:05:44 -04:00
// check if user already has a public key on the key server
keychain.getUserKeyPair(userId, function(err, keypair) {
2014-10-02 16:05:44 -04:00
if (err) {
$scope.displayError(err);
2014-10-02 16:05:44 -04:00
return;
}
2014-10-02 16:05:44 -04:00
keypair = keypair || {};
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) {
try {
2014-10-02 16:05:44 -04:00
$scope.key.publicKeyArmored = pgp.extractPublicKey($scope.key.privateKeyArmored);
} catch (e) {
$scope.displayError(new Error('Error reading PGP key!'));
return;
}
2014-10-02 16:05:44 -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) {
$scope.displayError(new Error('Error reading key params!'));
2014-10-02 16:05:44 -04:00
return;
}
// 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
email.unlock({
2014-10-02 16:05:44 -04:00
keypair: keypair,
passphrase: $scope.passphrase
}, function(err) {
if (err) {
$scope.incorrect = true;
$scope.displayError(err);
2014-10-02 16:05:44 -04:00
return;
}
keychain.putUserKeyPair(keypair, onUnlock);
2013-11-04 08:20:14 -05:00
});
2014-10-02 16:05:44 -04:00
});
}
function onUnlock(err) {
if (err) {
$scope.displayError(err);
2014-10-02 16:05:44 -04:00
return;
2013-11-04 08:20:14 -05:00
}
auth.storeCredentials(function(err) {
2013-11-04 08:20:14 -05:00
if (err) {
$scope.displayError(err);
return;
2013-10-22 08:48:38 -04:00
}
2013-10-21 07:10:42 -04:00
2014-12-02 10:12:42 -05:00
$location.path('/account');
2014-10-02 16:05:44 -04:00
$scope.$apply();
});
}
$scope.displayError = function(err) {
$scope.busy = false;
$scope.errMsg = err.errMsg || err.message;
$scope.$apply();
};
2014-10-02 16:05:44 -04:00
};
2014-10-08 06:34:34 -04:00
module.exports = LoginExistingCtrl;