2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-20 09:14:39 -05:00
|
|
|
var util = require('crypto-lib').util;
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-11-20 09:14:39 -05:00
|
|
|
var PrivateKeyUploadCtrl = function($scope, keychain, pgp, dialog, auth) {
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-04 10:05:26 -05:00
|
|
|
//
|
|
|
|
// scope state
|
|
|
|
//
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.state.privateKeyUpload = {
|
|
|
|
toggle: function(to) {
|
|
|
|
// open lightbox
|
|
|
|
$scope.state.lightbox = (to) ? 'privatekey-upload' : undefined;
|
|
|
|
if (!to) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// show syncing status
|
|
|
|
$scope.step = 4;
|
|
|
|
// check if key is already synced
|
|
|
|
$scope.checkServerForKey(function(privateKeySynced) {
|
|
|
|
if (privateKeySynced) {
|
|
|
|
// close lightbox
|
|
|
|
$scope.state.lightbox = undefined;
|
|
|
|
// show message
|
2014-11-20 09:14:39 -05:00
|
|
|
dialog.info({
|
2014-10-02 16:05:44 -04:00
|
|
|
title: 'Info',
|
|
|
|
message: 'Your PGP key has already been synced.'
|
|
|
|
});
|
2014-06-13 06:33:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// show sync ui if key is not synced
|
|
|
|
$scope.displayUploadUi();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-12-04 10:05:26 -05:00
|
|
|
//
|
|
|
|
// scope functions
|
|
|
|
//
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.checkServerForKey = function(callback) {
|
|
|
|
var keyParams = pgp.getKeyParams();
|
|
|
|
keychain.hasPrivateKey({
|
|
|
|
userId: keyParams.userId,
|
|
|
|
keyId: keyParams._id
|
|
|
|
}, function(err, privateKeySynced) {
|
|
|
|
if (err) {
|
2014-11-20 09:14:39 -05:00
|
|
|
dialog.error(err);
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
2014-07-22 15:05:34 -04:00
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if (privateKeySynced) {
|
|
|
|
callback(privateKeySynced);
|
2014-07-22 15:05:34 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.displayUploadUi = function() {
|
|
|
|
// go to step 1
|
|
|
|
$scope.step = 1;
|
|
|
|
// generate new code for the user
|
|
|
|
$scope.code = util.randomString(24);
|
|
|
|
$scope.displayedCode = $scope.code.slice(0, 4) + '-' + $scope.code.slice(4, 8) + '-' + $scope.code.slice(8, 12) + '-' + $scope.code.slice(12, 16) + '-' + $scope.code.slice(16, 20) + '-' + $scope.code.slice(20, 24);
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-12-01 05:53:45 -05:00
|
|
|
// clear input field of any previous artifacts
|
|
|
|
$scope.inputCode = '';
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.verifyCode = function() {
|
2014-12-01 05:53:45 -05:00
|
|
|
if ($scope.inputCode.toUpperCase() !== $scope.code) {
|
2014-10-02 16:05:44 -04:00
|
|
|
var err = new Error('The code does not match. Please go back and check the generated code.');
|
2014-11-20 09:14:39 -05:00
|
|
|
dialog.error(err);
|
2014-10-02 16:05:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-22 15:32:11 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.setDeviceName = function(callback) {
|
|
|
|
keychain.setDeviceName($scope.deviceName, callback);
|
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.encryptAndUploadKey = function(callback) {
|
2014-11-20 09:14:39 -05:00
|
|
|
var userId = auth.emailAddress;
|
2014-10-02 16:05:44 -04:00
|
|
|
var code = $scope.code;
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// register device to keychain service
|
|
|
|
keychain.registerDevice({
|
|
|
|
userId: userId
|
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
2014-11-20 09:14:39 -05:00
|
|
|
dialog.error(err);
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
2014-06-13 06:33:30 -04:00
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// encrypt private PGP key using code and upload
|
|
|
|
keychain.uploadPrivateKey({
|
|
|
|
userId: userId,
|
|
|
|
code: code
|
|
|
|
}, callback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.goBack = function() {
|
|
|
|
if ($scope.step > 1) {
|
|
|
|
$scope.step--;
|
|
|
|
}
|
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.goForward = function() {
|
|
|
|
if ($scope.step < 2) {
|
|
|
|
$scope.step++;
|
|
|
|
return;
|
|
|
|
}
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if ($scope.step === 2 && $scope.verifyCode()) {
|
|
|
|
$scope.step++;
|
|
|
|
return;
|
|
|
|
}
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if ($scope.step === 3) {
|
|
|
|
// set device name to local storage
|
|
|
|
$scope.setDeviceName(function(err) {
|
2014-06-13 06:33:30 -04:00
|
|
|
if (err) {
|
2014-11-20 09:14:39 -05:00
|
|
|
dialog.error(err);
|
2014-06-13 06:33:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// show spinner
|
2014-06-13 06:33:30 -04:00
|
|
|
$scope.step++;
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.$apply();
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// init key sync
|
|
|
|
$scope.encryptAndUploadKey(function(err) {
|
2014-06-13 06:33:30 -04:00
|
|
|
if (err) {
|
2014-11-20 09:14:39 -05:00
|
|
|
dialog.error(err);
|
2014-06-13 06:33:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// close sync dialog
|
|
|
|
$scope.state.privateKeyUpload.toggle(false);
|
|
|
|
// show success message
|
2014-11-20 09:14:39 -05:00
|
|
|
dialog.info({
|
2014-10-02 16:05:44 -04:00
|
|
|
title: 'Success',
|
|
|
|
message: 'Whiteout Keychain setup successful!'
|
2014-06-13 06:33:30 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
}
|
2014-06-13 06:33:30 -04:00
|
|
|
};
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
|
|
|
|
2014-10-08 06:34:34 -04:00
|
|
|
module.exports = PrivateKeyUploadCtrl;
|