mail/src/js/controller/app/privatekey-upload.js

155 lines
4.2 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
var util = require('crypto-lib').util;
2014-10-02 16:05:44 -04:00
2014-12-17 12:18:26 -05:00
var PrivateKeyUploadCtrl = function($scope, $q, keychain, pgp, dialog, auth) {
2014-10-02 16:05:44 -04: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
2014-12-17 12:18:26 -05:00
return $scope.checkServerForKey().then(function(privateKeySynced) {
2014-10-02 16:05:44 -04:00
if (privateKeySynced) {
// close lightbox
$scope.state.lightbox = undefined;
// show message
2014-12-17 12:18:26 -05:00
return dialog.info({
2014-10-02 16:05:44 -04:00
title: 'Info',
message: 'Your PGP key has already been synced.'
});
}
2014-10-02 16:05:44 -04:00
// show sync ui if key is not synced
$scope.displayUploadUi();
});
}
};
//
// scope functions
//
2014-12-17 12:18:26 -05:00
$scope.checkServerForKey = function() {
2014-10-02 16:05:44 -04:00
var keyParams = pgp.getKeyParams();
2014-12-17 12:18:26 -05:00
return $q(function(resolve) {
resolve();
2014-12-17 12:18:26 -05:00
}).then(function() {
return keychain.hasPrivateKey({
userId: keyParams.userId,
keyId: keyParams._id
});
2014-12-18 09:19:06 -05:00
}).then(function(privateKeySynced) {
return privateKeySynced ? privateKeySynced : undefined;
2014-12-17 12:18:26 -05:00
}).catch(dialog.error);
2014-10-02 16:05:44 -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-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.');
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;
};
2014-12-17 12:18:26 -05:00
$scope.setDeviceName = function() {
return $q(function(resolve) {
resolve();
}).then(function() {
return keychain.setDeviceName($scope.deviceName);
});
2014-10-02 16:05:44 -04:00
};
2014-12-17 12:18:26 -05:00
$scope.encryptAndUploadKey = function() {
var userId = auth.emailAddress;
2014-10-02 16:05:44 -04:00
var code = $scope.code;
2014-10-02 16:05:44 -04:00
// register device to keychain service
2014-12-17 12:18:26 -05:00
return $q(function(resolve) {
resolve();
2014-12-17 12:18:26 -05:00
}).then(function() {
// register the device
return keychain.registerDevice({
userId: userId
});
}).then(function() {
2014-10-02 16:05:44 -04:00
// encrypt private PGP key using code and upload
2014-12-17 12:18:26 -05:00
return keychain.uploadPrivateKey({
2014-10-02 16:05:44 -04:00
userId: userId,
code: code
2014-12-17 12:18:26 -05:00
});
}).catch(dialog.error);
2014-10-02 16:05:44 -04:00
};
$scope.goBack = function() {
if ($scope.step > 1) {
$scope.step--;
}
};
2014-10-02 16:05:44 -04:00
$scope.goForward = function() {
if ($scope.step < 2) {
$scope.step++;
return;
}
2014-10-02 16:05:44 -04:00
if ($scope.step === 2 && $scope.verifyCode()) {
$scope.step++;
return;
}
2014-10-02 16:05:44 -04:00
if ($scope.step === 3) {
// set device name to local storage
2014-12-17 12:18:26 -05:00
return $scope.setDeviceName().then(function() {
2014-10-02 16:05:44 -04:00
// show spinner
$scope.step++;
2014-10-02 16:05:44 -04:00
// init key sync
2014-12-17 12:18:26 -05:00
return $scope.encryptAndUploadKey();
}).then(function() {
// close sync dialog
$scope.state.privateKeyUpload.toggle(false);
// show success message
dialog.info({
title: 'Success',
message: 'Whiteout Keychain setup successful!'
});
2014-12-17 12:18:26 -05:00
}).catch(dialog.error);
2014-10-02 16:05:44 -04:00
}
};
2014-10-02 16:05:44 -04:00
};
2014-10-08 06:34:34 -04:00
module.exports = PrivateKeyUploadCtrl;