Skip to content

Commit

Permalink
Merge pull request #93 from subutai-io/gw-wallet
Browse files Browse the repository at this point in the history
Gw wallet
  • Loading branch information
absidish authored Jul 11, 2019
2 parents fa22563 + 6fcec41 commit 452876e
Show file tree
Hide file tree
Showing 17 changed files with 486 additions and 150 deletions.
2 changes: 2 additions & 0 deletions chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"common/img/mail_signed_96.png",
"common/img/mail_open_96.png",
"common/img/encrypt-22.png",
"common/img/decrypt-22.png",
"common/img/sign-22.png",
"common/img/undo-22.png",
"common/img/mail_new.png",
Expand All @@ -49,6 +50,7 @@
"common/ui/inline/dialogs/signDialog.html",
"common/ui/inline/dialogs/gwSignDialog.html",
"common/ui/inline/dialogs/pubkeyDialog.html",
"common/ui/inline/dialogs/decryptDialog.html",
"common/ui/editor/editor.html",
"common/ui/options.html",
"common/dep/bootstrap/css/bootstrap.css",
Expand Down
Binary file added common/img/decrypt-22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 103 additions & 8 deletions common/lib/controller/encrypt.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ define(function (require, exports, module) {
that.ports.eFrame.postMessage({event: 'recipient-proposal'});
});
break;
case 'decrypt-dialog-init':

// send content
this.porto.data.load('common/ui/inline/dialogs/templates/decrypt.html').then(function (content) {
that.ports.eDialog.postMessage({event: 'encrypt-dialog-content', data: content});
// get potential recipients from eFrame
// if editor is active get recipients from parent eFrame
that.ports.eFrame.postMessage({event: 'recipient-proposal'});
});
break;
case 'eframe-recipient-proposal':
var emails = this.porto.util.sortAndDeDup(msg.data);
var userKeys = localKeyring.getKeyUserIDs(emails);
Expand All @@ -120,10 +130,76 @@ define(function (require, exports, module) {
}
break;
case 'encrypt-dialog-ok':
// add recipients to buffer
this.keyidBuffer = msg.recipient;
// get email text from eFrame
this.ports.eFrame.postMessage({event: 'email-text', type: msg.type, action: 'encrypt'});
this.signBuffer = {};
var key;
try {
key = this.keyring.getById(this.porto.LOCAL_KEYRING_ID).getKeyForSigning(msg.signKeyId);
} catch ( err ) {
console.log(err);
break;
}
// add key in buffer
this.signBuffer.key = key.signKey;
this.signBuffer.keyid = msg.signKeyId;
this.signBuffer.userid = key.userId;
this.signBuffer.reason = 'PWD_DIALOG_REASON_SIGN';
this.signBuffer.keyringId = this.porto.LOCAL_KEYRING_ID;
this.pwdControl = sub.factory.get('pwdDialog');
this.pwdControl.unlockKey(this.signBuffer)
.then(function () {
that.ports.eFrame.postMessage({
event: 'email-text',
type: msg.type,
action: 'encrypt',
fingerprint: msg.fingerprint
});
})
.catch(function (err) {
if (err.code = 'PWD_DIALOG_CANCEL') {
that.ports.eFrame.postMessage({event: 'sign-dialog-cancel'});
return;
}
if (err) {
// TODO: propagate error to sign dialog
}
});

break;
case 'decrypt-dialog-ok':
this.signBuffer = {};
var key;
try {
key = this.keyring.getById(this.porto.LOCAL_KEYRING_ID).getKeyForSigning(msg.signKeyId);
} catch ( err) {
console.log(err);
break;
}
// add key in buffer
this.signBuffer.key = key.signKey;
this.signBuffer.keyid = msg.signKeyId;
this.signBuffer.userid = key.userId;
this.signBuffer.reason = 'PWD_DIALOG_REASON_SIGN';
this.signBuffer.keyringId = this.porto.LOCAL_KEYRING_ID;
this.pwdControl = sub.factory.get('pwdDialog');
this.pwdControl.unlockKey(this.signBuffer)
.then(function () {
that.ports.eFrame.postMessage({
event: 'email-text',
type: msg.type,
action: 'decrypt',
fingerprint: msg.fingerprint
});
})
.catch(function (err) {
if (err.code = 'PWD_DIALOG_CANCEL') {
that.ports.eFrame.postMessage({event: 'sign-dialog-cancel'});
return;
}
if (err) {
// TODO: propagate error to sign dialog
}
});

break;
case 'sign-dialog-ok':
this.signBuffer = {};
Expand Down Expand Up @@ -160,15 +236,34 @@ define(function (require, exports, module) {
break;
case 'eframe-email-text':
if (msg.action === 'encrypt') {
// TODO fix error while encrypting message, instead of passing three parameters to
// pgpMode.encryptMessage wrap it into one object with relevant keys
this.model.encryptMessage(msg.data, this.porto.LOCAL_KEYRING_ID, this.keyidBuffer)
this.model.encrypt(msg.data, this.signBuffer.key, this)
.then(function (msg) {
that.ports.eFrame.postMessage({event: 'encrypted-message', message: msg});
console.log('successfully encrypted: '+JSON.stringify(msg));
that.ports.eFrame.postMessage({event: 'encrypted-message', message: msg.data});
})
.catch(function (error) {
console.log('model.encryptMessage() error', error);
if (that.ports.eDialog) {
that.ports.eDialog.postMessage({event: 'encrypt-failed', data: error});
}
});
break;
} else if (msg.action === 'decrypt') {
this.model.decrypt(msg.data, this.signBuffer.key)
.then(function (msg) {
console.log('successfully decrypted: '+JSON.stringify(msg));
that.ports.eFrame.postMessage({event: 'decrypted-message', message: msg.data});
})
.catch(function (error) {
console.log('model.decryptMessage() error', error);
if (that.ports.eDialog) {
that.ports.eDialog.postMessage({event: 'decrypt-failed', data: error.message});
}
else {
that.ports.eFrame.postMessage({event: 'decrypt-failed', data: error.message});
}
});
break;
} else if (msg.action === 'sign') {
var fingerprint = msg.fingerprint;
this.model.signMessage(msg.data, this.signBuffer.key)
Expand Down
11 changes: 11 additions & 0 deletions common/lib/pgpModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ define(function(require, exports, module) {
}
}

function decrypt(armored, key) {
var msg = openpgp.message.readArmored(armored);
return openpgp.decrypt({message: msg, privateKey: key, format:'utf8'});
}

/**
* @param {Object} options
* @param {String} options.keyIdsHex
Expand Down Expand Up @@ -199,6 +204,10 @@ define(function(require, exports, module) {
});
}

function encrypt(textToEncrypt, key, that) {
return openpgp.encrypt({data: textToEncrypt, publicKeys: key, armor: true});
}

/**
* @param {Object} options
* @param {String} options.keyIdsHex
Expand Down Expand Up @@ -427,8 +436,10 @@ define(function(require, exports, module) {
exports.readMessage = readMessage;
exports.readCleartextMessage = readCleartextMessage;
exports.decryptMessage = decryptMessage;
exports.decrypt = decrypt;
exports.unlockKey = unlockKey;
exports.encryptMessage = encryptMessage;
exports.encrypt = encrypt;
exports.signAndEncryptMessage = signAndEncryptMessage;
exports.signMessage = signMessage;
exports.verifyMessage = verifyMessage;
Expand Down
16 changes: 16 additions & 0 deletions common/ui/inline/dialogs/decryptDialog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../../../dep/bootstrap/css/bootstrap.css">
<link rel="stylesheet" href="../../porto.css">
<link rel="stylesheet" href="signDialog.css">
<script src="../../../dep/jquery.min.js"></script>
<script src="../../../dep/jquery.ext.js"></script>
<script src="../../../dep/bootstrap/js/bootstrap.js"></script>
<script src="../../porto.js"></script>
<script src="decryptDialog.js"></script>
</head>
<body>
</body>
</html>
164 changes: 164 additions & 0 deletions common/ui/inline/dialogs/decryptDialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
'use strict';

var porto = porto || null;

(function() {
var id, name, port, l10n;

function init() {
if (document.body.dataset.porto) {
return;
}
document.body.dataset.porto = true;
// open port to background page
var qs = jQuery.parseQuerystring();
id = qs.id;
name = 'eDialog-' + id;
port = porto.extension.connect({name: name});
port.onMessage.addListener(messageListener);
porto.l10n.getMessages([
'encrypt_dialog_no_recipient',
'encrypt_dialog_add',
'decrypt_dialog_header',
'keygrid_delete',
'form_cancel',
'form_ok'
], function(result) {
port.postMessage({event: 'decrypt-dialog-init', sender: name});
l10n = result;
});
}

function load(content) {
$('body').html(content);
porto.l10n.localizeHTML(l10n);
$('#okBtn').click(onOk);
$('#cancelBtn').click(onCancel);
$('#addBtn').click(onAdd);
$('#deleteBtn').click(onDelete);
$('#keyDialog').fadeIn('fast');
// align width
$.setEqualWidth($('#okBtn'), $('#cancelBtn'));
$.setEqualWidth($('#addBtn'), $('#deleteBtn'));
}

function onOk() {
$('body').addClass('busy');
var $keySelect = $('#keySelect');
var selectedKey = $keySelect.find(':selected');
port.postMessage({
event: 'decrypt-dialog-ok',
sender: name,
signKeyId: $keySelect.val(),
fingerprint: selectedKey.data('fingerprint'),
type: 'text'
});
return false;
}

function onCancel() {
logUserInput('security_log_dialog_cancel');
port.postMessage({event: 'encrypt-dialog-cancel', sender: name});
return false;
}

function onAdd() {
// remove possible error
if ($('#keyList').hasClass('alert-error')) {
$('#keyList').removeClass('alert-error')
.empty();
}
var selected = $('#keySelect option:selected');
// add selection to key list
$('<option/>').val(selected.val()).text(selected.text()).appendTo($('#keyList'));
// find next proposal
var option = selected.next();
while (option.length !== 0) {
if (option.data('proposal')) {
option.prop('selected', true);
break;
}
option = option.next();
}
selected.prop('selected', false);
if (option.length === 0) {
// no further proposal found, get back to next of selected
option = selected.next();
if (option.length === 0) {
// jump back to first element
selected.siblings().first().prop('selected', true);
}
else {
// select next non-proposal element
option.prop('selected', true);
}
}
}

function onDelete() {
$('#keyList option:selected').remove();
}

/**
* send log entry for the extension
* @param {string} type
*/
function logUserInput(type) {
port.postMessage({
event: 'editor-user-input',
sender: name,
source: 'security_log_encrypt_dialog',
type: type
});
}

function messageListener(msg) {
switch (msg.event) {
case 'encrypt-dialog-content':
load(msg.data);
break;
case 'decrypt-failed':
$('body').removeClass('busy');
alert('Cannot decrypt with this key.');
console.log('Cannot decrypt with this key: '+JSON.stringify(msg.data));
break;
case 'public-key-userids':
var keySelect = $('#keySelect');
var firstProposal = true;
msg.keys.forEach(function(key) {
var option = $('<option/>').val(key.keyid).text(key.userid);
if (key.keyid === msg.primary) {
$('#keyList').append(option.clone());
key.proposal = false;
}
if (key.proposal) {
option.data('proposal', key.proposal);
if (firstProposal) {
// set the first proposal as selected
option.prop('selected', true);
firstProposal = false;
}
}
option.appendTo(keySelect);
});
break;
case 'encoding-defaults':
if (msg.defaults.type === 'text') {
$('#encodeText').prop('checked', true);
}
else {
$('#encodeHTML').prop('checked', true);
}
if (!msg.defaults.editable) {
$('input[name="encodeRadios"]').prop('disabled', true);
}
$('#encoding').show();
break;
default:
console.log('unknown event');
}
}

$(document).ready(init);

}());
3 changes: 2 additions & 1 deletion common/ui/inline/dialogs/encryptDialog.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
<meta charset="utf-8">
<link rel="stylesheet" href="../../../dep/bootstrap/css/bootstrap.css">
<link rel="stylesheet" href="../../porto.css">
<link rel="stylesheet" href="encryptDialog.css">
<link rel="stylesheet" href="signDialog.css">
<script src="../../../dep/jquery.min.js"></script>
<script src="../../../dep/jquery.ext.js"></script>
<script src="../../../dep/bootstrap/js/bootstrap.js"></script>
<script src="../../porto.js"></script>
<script src="encryptDialog.js"></script>
</head>
Expand Down
Loading

0 comments on commit 452876e

Please sign in to comment.