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');
|
|
|
|
|
2014-08-13 10:16:28 -04:00
|
|
|
var LoginExistingCtrl = function($scope, $location, $routeParams) {
|
|
|
|
if (!appController._emailDao && !$routeParams.dev) {
|
|
|
|
$location.path('/'); // init app
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-05 14:14:23 -05:00
|
|
|
var emailDao = appController._emailDao,
|
2014-06-13 06:33:30 -04:00
|
|
|
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;
|
2014-03-05 14:14:23 -05:00
|
|
|
// 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) {
|
2013-11-14 14:44:57 -05:00
|
|
|
$scope.onError(err);
|
2013-10-22 08:48:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-05 14:14:23 -05:00
|
|
|
keypair = keypair || {};
|
|
|
|
|
2014-07-01 14:58:34 -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 {
|
|
|
|
$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
|
2014-06-18 04:02:33 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-05 14:14:23 -05:00
|
|
|
// set parsed private key
|
2013-11-04 08:20:14 -05:00
|
|
|
keypair.privateKey = {
|
2014-06-18 04:02:33 -04:00
|
|
|
_id: privKeyParams._id,
|
2013-11-04 08:20:14 -05:00
|
|
|
userId: userId,
|
2014-06-18 04:02:33 -04:00
|
|
|
userIds: privKeyParams.userIds,
|
2013-11-04 08:20:14 -05:00
|
|
|
encryptedKey: $scope.key.privateKeyArmored
|
|
|
|
};
|
|
|
|
|
2014-03-05 14:14:23 -05:00
|
|
|
if (!keypair.publicKey) {
|
|
|
|
// there is no public key on the key server yet... use parsed
|
|
|
|
keypair.publicKey = {
|
2014-06-18 04:02:33 -04:00
|
|
|
_id: pubKeyParams._id,
|
2014-03-05 14:14:23 -05:00
|
|
|
userId: userId,
|
2014-06-18 04:02:33 -04:00
|
|
|
userIds: pubKeyParams.userIds,
|
2014-03-05 14:14:23 -05:00
|
|
|
publicKey: $scope.key.publicKeyArmored
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// import and validate keypair
|
2013-12-03 14:25:39 -05:00
|
|
|
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;
|
2013-11-14 14:44:57 -05:00
|
|
|
$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) {
|
2013-11-14 14:44:57 -05:00
|
|
|
$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
|
|
|
|
2014-07-01 13:49:19 -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,
|
2014-07-01 14:58:34 -04:00
|
|
|
index = rawKeys.indexOf('-----BEGIN PGP PRIVATE KEY BLOCK-----'),
|
2014-06-18 04:02:33 -04:00
|
|
|
keyParts;
|
2013-10-22 11:32:30 -04:00
|
|
|
|
|
|
|
if (index === -1) {
|
2014-07-01 14:58:34 -04:00
|
|
|
scope.onError(new Error('Error parsing private PGP key block!'));
|
2013-10-22 11:32:30 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-10-22 10:12:18 -04:00
|
|
|
|
2014-06-18 04:02:33 -04:00
|
|
|
keyParts = {
|
|
|
|
publicKeyArmored: rawKeys.substring(0, index).trim(),
|
|
|
|
privateKeyArmored: rawKeys.substring(index, rawKeys.length).trim()
|
2013-10-22 08:48:38 -04:00
|
|
|
};
|
2014-06-18 04:02:33 -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;
|
|
|
|
});
|