js: code cleanup (eslint)

--
* trying to stick to eslint from now on for readability

* As some attribution was lost in previous git confusions, I added my
  name into some of the licence headers
This commit is contained in:
Maximilian Krambach 2018-06-06 13:05:53 +02:00
parent 0356a667c5
commit bfd3799d39
27 changed files with 597 additions and 500 deletions

View File

@ -24,6 +24,9 @@
"error",
"always"
],
"no-var": [
"warn"
],
"max-len": 1
}
}

View File

@ -16,26 +16,12 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* gpgme.js - Javascript integration for gpgme
* Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
*
* This file is part of GPGME.
*
* GPGME is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* GPGME is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*/
/* global chrome */
document.addEventListener('DOMContentLoaded', function() {
chrome.tabs.create({

View File

@ -16,6 +16,11 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* global mocha */
mocha.run();

View File

@ -16,6 +16,12 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* global Gpgmejs_test, mocha*/
Gpgmejs_test.unittests();
mocha.run();

View File

@ -16,7 +16,13 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* global mocha, chai */
mocha.setup('bdd');
var expect = chai.expect;
const expect = chai.expect; //eslint-disable-line no-unused-vars
chai.config.includeStack = true;

View File

@ -16,6 +16,9 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* global describe, it, expect, Gpgmejs, ImportablePublicKey */

View File

@ -1,4 +1,3 @@
/* gpgme.js - Javascript integration for gpgme
* Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
*
@ -17,23 +16,31 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* global describe, it, expect, Gpgmejs */
/* global inputvalues, encryptedData, bigString, bigBoringString */
describe('Encryption and Decryption', function () {
it('Successful encrypt and decrypt simple string', function (done) {
let prm = Gpgmejs.init();
prm.then(function (context) {
context.encrypt(
inputvalues.encrypt.good.data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
inputvalues.encrypt.good.fingerprint).then(
function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE');
context.decrypt(answer.data).then(function (result) {
expect(result).to.not.be.empty;
expect(result.data).to.be.a('string');
expect(result.data).to.equal(inputvalues.encrypt.good.data);
expect(result.data).to.equal(
inputvalues.encrypt.good.data);
done();
});
});
@ -64,7 +71,7 @@ describe('Encryption and Decryption', function () {
inputvalues.encrypt.good.fingerprint).then(
function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include(
'BEGIN PGP MESSAGE');
expect(answer.data).to.include(
@ -82,7 +89,9 @@ describe('Encryption and Decryption', function () {
}).timeout(5000);
for (let j = 0; j < inputvalues.encrypt.good.data_nonascii_32.length; j++){
it('Roundtrip with >1MB non-ascii input meeting default chunksize (' + (j + 1) + '/' + inputvalues.encrypt.good.data_nonascii_32.length + ')',
it('Roundtrip with >1MB non-ascii input meeting default chunksize (' +
(j + 1) + '/'
+ inputvalues.encrypt.good.data_nonascii_32.length + ')',
function (done) {
let input = inputvalues.encrypt.good.data_nonascii_32[j];
expect(input).to.have.length(32);
@ -96,7 +105,7 @@ describe('Encryption and Decryption', function () {
inputvalues.encrypt.good.fingerprint).then(
function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include(
'BEGIN PGP MESSAGE');
expect(answer.data).to.include(
@ -111,7 +120,7 @@ describe('Encryption and Decryption', function () {
});
});
}).timeout(3000);
};
}
it('Random data, as string', function (done) {
let data = bigString(1000);
@ -121,7 +130,7 @@ describe('Encryption and Decryption', function () {
inputvalues.encrypt.good.fingerprint).then(
function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include(
'BEGIN PGP MESSAGE');
expect(answer.data).to.include(
@ -143,10 +152,10 @@ describe('Encryption and Decryption', function () {
let prm = Gpgmejs.init();
prm.then(function (context) {
context.encrypt(b64data,
inputvalues.encrypt.good.fingerprint,).then(
inputvalues.encrypt.good.fingerprint).then(
function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include(
'BEGIN PGP MESSAGE');
expect(answer.data).to.include(
@ -171,7 +180,7 @@ describe('Encryption and Decryption', function () {
inputvalues.encrypt.good.fingerprint, true).then(
function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include(
'BEGIN PGP MESSAGE');
expect(answer.data).to.include(
@ -196,7 +205,7 @@ describe('Encryption and Decryption', function () {
inputvalues.encrypt.good.fingerprint).then(
function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include(
'BEGIN PGP MESSAGE');

View File

@ -16,7 +16,14 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* global describe, it, expect, Gpgmejs */
/* global inputvalues, fixedLengthString */
describe('Encryption', function () {
it('Successful encrypt', function (done) {
let prm = Gpgmejs.init();
@ -25,7 +32,7 @@ describe('Encryption', function () {
inputvalues.encrypt.good.data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE');
done();
@ -41,7 +48,7 @@ describe('Encryption', function () {
data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE');
done();
@ -57,7 +64,7 @@ describe('Encryption', function () {
data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE');
done();
@ -73,7 +80,7 @@ describe('Encryption', function () {
data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE');
done();
@ -124,7 +131,7 @@ describe('Encryption', function () {
done();
});
});
}).timeout(5000);;
}).timeout(5000);
it('Overly large message ( > 65MB) is rejected', function (done) {
let prm = Gpgmejs.init();

View File

@ -16,9 +16,12 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
var inputvalues = {// eslint-disable-line no-unused-vars
const inputvalues = {// eslint-disable-line no-unused-vars
encrypt: {
good:{
data : 'Hello World.',
@ -131,7 +134,7 @@ function slightlyLessBoringString(megabytes, set){
}
// Data encrypted with testKey
var encryptedData =// eslint-disable-line no-unused-vars
const encryptedData =// eslint-disable-line no-unused-vars
'-----BEGIN PGP MESSAGE-----\n' +
'\n' +
'hQEMA6B8jfIUScGEAQgAlANd3uyhmhYLzVcfz4LEqA8tgUC3n719YH0iuKEzG/dv\n' +
@ -146,7 +149,7 @@ var encryptedData =// eslint-disable-line no-unused-vars
'=zap6\n' +
'-----END PGP MESSAGE-----\n';
var ImportablePublicKey = {// eslint-disable-line no-unused-vars
const ImportablePublicKey = {// eslint-disable-line no-unused-vars
fingerprint: '78034948BA7F5D0E9BDB67E4F63790C11E60278A',
key:'-----BEGIN PGP PUBLIC KEY BLOCK-----\n' +
'\n' +

View File

@ -1,6 +1,32 @@
/* gpgme.js - Javascript integration for gpgme
* Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
*
* This file is part of GPGME.
*
* GPGME is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* GPGME is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* global describe, it, expect, Gpgmejs */
/* global bigString, inputvalues */
describe('Long running Encryption/Decryption', function () {
for (let i=0; i < 100; i++) {
it('Successful encrypt/decrypt completely random data ' + (i+1) + '/100', function (done) {
it('Successful encrypt/decrypt completely random data ' +
(i+1) + '/100', function (done) {
let prm = Gpgmejs.init();
let data = bigString(2*1024*1024);
prm.then(function (context) {
@ -8,7 +34,7 @@ describe('Long running Encryption/Decryption', function () {
inputvalues.encrypt.good.fingerprint).then(
function (answer){
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string");
expect(answer.data).to.be.a('string');
expect(answer.data).to.include(
'BEGIN PGP MESSAGE');
expect(answer.data).to.include(
@ -18,12 +44,25 @@ describe('Long running Encryption/Decryption', function () {
expect(result).to.not.be.empty;
expect(result.data).to.be.a('string');
if (result.data.length !== data.length) {
console.log('diff: ' + (result.data.length - data.length));
// console.log('diff: ' +
// (result.data.length - data.length));
for (let i=0; i < result.data.length; i++){
if (result.data[i] !== data[i]){
console.log('position: ' + i);
console.log('result : '+ result.data.charCodeAt(i) + result.data[i-2] + result.data[i-1] + result.data[i] + result.data[i+1] + result.data[i+2]);
console.log('original: ' + data.charCodeAt(i) + data[i-2] + data[i-1] + data[i] + data[i+1] + data[i+2]);
// console.log('position: ' + i);
// console.log('result : ' +
// result.data.charCodeAt(i) +
// result.data[i-2] +
// result.data[i-1] +
// result.data[i] +
// result.data[i+1] +
// result.data[i+2]);
// console.log('original: ' +
// data.charCodeAt(i) +
// data[i-2] +
// data[i-1] +
// data[i] +
// data[i+1] +
// data[i+2]);
break;
}
}
@ -34,6 +73,6 @@ describe('Long running Encryption/Decryption', function () {
});
});
}).timeout(8000);
};
}
});

View File

@ -16,7 +16,14 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* global describe, it, expect, Gpgmejs */
/* global bigString, inputvalues */
describe('Signing', function () {
it('Sign a message', function (done) {
let prm = Gpgmejs.init();

View File

@ -16,8 +16,14 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* global describe, it, expect, Gpgmejs */
/* global inputvalues */
describe('GPGME context', function(){
it('Starting a GpgME instance', function(done){
let prm = Gpgmejs.init();
@ -43,6 +49,6 @@ describe('GPGME does not start with invalid parameters', function(){
expect(error.code).to.equal('PARAM_WRONG');
done();
});
})
});
}
});

View File

@ -6,20 +6,25 @@ receiving an answer
[*] nativeConnection successfull on Windows, macOS, Linux
[X] nativeConnection with delayed, multipart (> 1MB) answer
[*] Message handling (encrypt, decrypt verify, sign)
[x] Message handling (encrypt, decrypt verify, sign)
[x] encrypt, decrypt
[ ] verify
[ ] sign
[x] verify
[x] sign
[*] Key handling (import/export, modifying, status queries)
[x] Import (not importing secret)
[x] Export (not exporting secret)
[x] status queries
[ ] key generation
[ ] modification
[*] Configuration handling
[ ] check for completeness
Communication with other implementations
[ ] option to export SECRET Key into localstore used by e.g. mailvelope
[-] option to export SECRET Key into localstore used by e.g. mailvelope?
current discussion states that this won't be possible due to security
concerns
Management:
[*] Define the gpgme interface
[x] check Permissions (e.g. csp) for the different envs
[X] agree on license
[x] agree on license
[*] tests

View File

@ -17,7 +17,12 @@
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* global chrome */
document.addEventListener('DOMContentLoaded', function() {
chrome.tabs.create({
url: './mainui.html'

View File

@ -17,39 +17,41 @@
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/* global document, Gpgmejs */
document.addEventListener('DOMContentLoaded', function() {
Gpgmejs.init().then(function(gpgmejs){
document.getElementById("buttonencrypt").addEventListener("click",
document.getElementById('buttonencrypt').addEventListener('click',
function(){
let data = document.getElementById('cleartext').value;
let keyId = document.getElementById('pubkey').value;
gpgmejs.encrypt(data, keyId).then(
function(answer){
console.log(answer);
if (answer.data){
console.log(answer.data);
document.getElementById('answer').value = answer.data;
document.getElementById(
'answer').value = answer.data;
}
}, function(errormsg){
alert( errormsg.code + ' ' + errormsg.msg);
});
});
document.getElementById("buttondecrypt").addEventListener("click",
document.getElementById('buttondecrypt').addEventListener('click',
function(){
let data = document.getElementById("ciphertext").value;
let data = document.getElementById('ciphertext').value;
gpgmejs.decrypt(data).then(
function(answer){
console.log(answer);
if (answer.data){
document.getElementById('answer').value = answer.data;
document.getElementById(
'answer').value = answer.data;
}
}, function(errormsg){
alert( errormsg.code + ' ' + errormsg.msg);
});
});
},
function(error){console.log(error)});
});
});

View File

@ -1,7 +1,7 @@
{
"name": "gpgmejs",
"version": "0.0.1",
"description": "javascript part of a nativeMessaging gnupg integration",
"version": "0.0.1-dev",
"description": "Javascript part of the GPGME nativeMessaging integration",
"main": "src/index.js",
"private": true,
"keywords": [],

View File

@ -16,6 +16,9 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
export const availableConf = {

View File

@ -16,16 +16,16 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/**
* A connection port will be opened for each communication between gpgmejs and
* gnupg. It should be alive as long as there are additional messages to be
* expected.
*/
import { permittedOperations } from './permittedOperations'
import { gpgme_error } from "./Errors"
import { GPGME_Message, createMessage } from "./Message";
/* global chrome */
import { permittedOperations } from './permittedOperations';
import { gpgme_error } from './Errors';
import { GPGME_Message, createMessage } from './Message';
/**
* A Connection handles the nativeMessaging interaction.
@ -55,7 +55,7 @@ export class Connection{
return this.post(createMessage('version'));
} else {
let me = this;
return new Promise(function(resolve,reject) {
return new Promise(function(resolve) {
Promise.race([
me.post(createMessage('version')),
new Promise(function(resolve, reject){
@ -63,9 +63,9 @@ export class Connection{
reject(gpgme_error('CONN_TIMEOUT'));
}, 500);
})
]).then(function(result){
]).then(function(){ // success
resolve(true);
}, function(reject){
}, function(){ // failure
resolve(false);
});
});
@ -98,12 +98,10 @@ export class Connection{
* information.
*/
post(message){
if (!this._connection) {
}
if (!message || !message instanceof GPGME_Message){
if (!message || !(message instanceof GPGME_Message)){
this.disconnect();
return Promise.reject(gpgme_error('PARAM_WRONG', 'Connection.post'));
return Promise.reject(gpgme_error(
'PARAM_WRONG', 'Connection.post'));
}
if (message.isComplete !== true){
this.disconnect();
@ -114,10 +112,10 @@ export class Connection{
let answer = new Answer(message);
let listener = function(msg) {
if (!msg){
me._connection.onMessage.removeListener(listener)
me._connection.onMessage.removeListener(listener);
me._connection.disconnect();
reject(gpgme_error('CONN_EMPTY_GPG_ANSWER'));
} else if (msg.type === "error"){
} else if (msg.type === 'error'){
me._connection.onMessage.removeListener(listener);
me._connection.disconnect();
reject(gpgme_error('GNUPG_ERROR', msg.msg));
@ -130,7 +128,7 @@ export class Connection{
} else if (msg.more === true){
me._connection.postMessage({'op': 'getmore'});
} else {
me._connection.onMessage.removeListener(listener)
me._connection.onMessage.removeListener(listener);
me._connection.disconnect();
resolve(answer.message);
}
@ -150,7 +148,7 @@ export class Connection{
}]).then(function(result){
return result;
}, function(reject){
if(!reject instanceof Error) {
if(!(reject instanceof Error)) {
me._connection.disconnect();
return gpgme_error('GNUPG_ERROR', reject);
} else {
@ -160,12 +158,13 @@ export class Connection{
}
});
}
};
}
/**
* A class for answer objects, checking and processing the return messages of
* the nativeMessaging communication.
* @param {String} operation The operation, to look up validity of returning messages
* @param {String} operation The operation, to look up validity of returning
* messages
*/
class Answer{
@ -256,7 +255,8 @@ class Answer{
msg[keys[i]] = this._response[keys[i]];
} else {
msg[keys[i]] = decodeURIComponent(
atob(this._response[keys[i]]).split('').map(function(c) {
atob(this._response[keys[i]]).split('').map(
function(c) {
return '%' +
('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));

View File

@ -16,6 +16,9 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
const err_list = {
@ -102,6 +105,7 @@ export function gpgme_error(code = 'GENERIC_ERROR', info){
return new GPGME_Error(code);
}
if (err_list[code].type === 'warning'){
// eslint-disable-next-line no-console
console.warn(code + ': ' + err_list[code].msg);
}
return null;
@ -119,7 +123,7 @@ class GPGME_Error extends Error{
super(msg);
} else if (err_list.hasOwnProperty(code)){
if (msg){
super(err_list[code].msg + "--" + msg);
super(err_list[code].msg + '--' + msg);
} else {
super(err_list[code].msg);
}

View File

@ -16,14 +16,18 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
import { gpgme_error } from "./Errors";
import { GPGME_Key } from "./Key";
import { gpgme_error } from './Errors';
import { GPGME_Key } from './Key';
/**
* Tries to return an array of fingerprints, either from input fingerprints or
* from Key objects
* @param {Key |Array<Key>| GPGME_Key | Array<GPGME_Key>|String|Array<String>} input
* from Key objects (openpgp Keys or GPGME_Keys are both expected)
* @param {Object |Array<Object>| String|Array<String>} input
* @returns {Array<String>} Array of fingerprints.
*/
@ -64,7 +68,7 @@ export function toKeyIdArray(input){
} else {
return result;
}
};
}
/**
* check if values are valid hexadecimal values of a specified length
@ -72,7 +76,7 @@ export function toKeyIdArray(input){
* @param {int} len the expected length of the value
*/
function hextest(key, len){
if (!key || typeof(key) !== "string"){
if (!key || typeof(key) !== 'string'){
return false;
}
if (key.length !== len){
@ -80,23 +84,18 @@ function hextest(key, len){
}
let regexp= /^[0-9a-fA-F]*$/i;
return regexp.test(key);
};
}
/**
* check if the input is a valid Hex string with a length of 40
*/
export function isFingerprint(string){
return hextest(string, 40);
};
}
/**
* check if the input is a valid Hex string with a length of 16
*/
export function isLongId(string){
return hextest(string, 16);
};
// TODO still not needed anywhere
function isShortId(string){
return hextest(string, 8);
};
}

View File

@ -16,20 +16,14 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/**
* The key class allows to query the information defined in gpgme Key Objects
* (see https://www.gnupg.org/documentation/manuals/gpgme/Key-objects.html)
*
* This is a stub, as the gpgme-json side is not yet implemented
*
*/
import { isFingerprint, isLongId } from './Helpers'
import { gpgme_error } from './Errors'
import { isFingerprint, isLongId } from './Helpers';
import { gpgme_error } from './Errors';
import { createMessage } from './Message';
import { permittedOperations } from './permittedOperations';
/**
* Validates the fingerprint.
@ -44,6 +38,11 @@ export function createKey(fingerprint){
/**
* Representing the Keys as stored in GPG
* It allows to query almost all information defined in gpgme Key Objects
* Refer to validKeyProperties for available information, and the gpgme
* documentation on their meaning
* (https://www.gnupg.org/documentation/manuals/gpgme/Key-objects.html)
*
*/
export class GPGME_Key {
@ -162,11 +161,6 @@ export class GPGME_Key {
}
}
get armored () {
return this.get('armored');
//TODO exception if empty
}
/**
* Reloads the Key from gnupg
*/
@ -188,7 +182,7 @@ export class GPGME_Key {
}
}, function (error) {
reject(gpgme_error('GNUPG_ERROR'), error);
})
});
});
}
@ -249,15 +243,16 @@ export class GPGME_Key {
}
}
} else {
reject(gpgme_error('CONN_UNEXPECTED_ANSWER'))
reject(gpgme_error('CONN_UNEXPECTED_ANSWER'));
}
}, function(error){
})
reject(error);
});
});
}
/**
* Convenience function to be directly used as properties of the Key
* Convenience functions to be directly used as properties of the Key
* Notice that these rely on cached info and may be outdated. Use the async
* get(property, false) if you need the most current info
*/
@ -280,8 +275,8 @@ export class GPGME_Key {
/**
* Deletes the public Key from the GPG Keyring. Note that a deletion of a
* secret key is not supported by the native backend.
* @returns {Promise<Boolean>} Success if key was deleted, rejects with a GPG error
* otherwise
* @returns {Promise<Boolean>} Success if key was deleted, rejects with a
* GPG error otherwise
*/
delete(){
let me = this;
@ -295,7 +290,7 @@ export class GPGME_Key {
resolve(result.success);
}, function(error){
reject(error);
})
});
});
}
}
@ -378,37 +373,37 @@ const validUserIdProperties = {
},
'uid': function(value){
if (typeof(value) === 'string' || value === ''){
return true;;
return true;
}
return false;
},
'validity': function(value){
if (typeof(value) === 'string'){
return true;;
return true;
}
return false;
},
'name': function(value){
if (typeof(value) === 'string' || value === ''){
return true;;
return true;
}
return false;
},
'email': function(value){
if (typeof(value) === 'string' || value === ''){
return true;;
return true;
}
return false;
},
'address': function(value){
if (typeof(value) === 'string' || value === ''){
return true;;
return true;
}
return false;
},
'comment': function(value){
if (typeof(value) === 'string' || value === ''){
return true;;
return true;
}
return false;
},
@ -471,7 +466,7 @@ const validSubKeyProperties = {
'expires': function(value){
return (Number.isInteger(value) && value > 0);
}
}
};
const validKeyProperties = {
//TODO better validation?
'fingerprint': function(value){
@ -546,4 +541,4 @@ const validKeyProperties = {
return typeof(value) === 'boolean';
}
}
};

View File

@ -16,8 +16,12 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
import {createMessage} from './Message';
import {createKey} from './Key';
import { isFingerprint } from './Helpers';
@ -99,18 +103,22 @@ export class GPGME_Keyring {
/**
*
* @param {String} armored Armored Key block of the Kex(s) to be imported into gnupg
* @param {Boolean} prepare_sync prepare the keys for synched use (see getKeys()).
* @returns {Promise<Array<Object>>} An array of objects for the Keys considered.
* Key.key The key itself as a GPGME_Key
* Key.status String:
* @param {String} armored Armored Key block of the Kex(s) to be imported
* into gnupg
* @param {Boolean} prepare_sync prepare the keys for synched use
* (see getKeys()).
* @returns {Promise<Array<Object>>} An array of objects for the Keys
* considered:
* Key.key <Object>: The key itself as a GPGME_Key
* Key.status <String>:
* 'nochange' if the Key was not changed,
* 'newkey' if the Key was imported in gpg, and did not exist previously,
* 'change' if the key existed, but details were updated. For details,
* Key.changes is available.
* Key.changes.userId: Boolean userIds changed
* Key.changes.signature: Boolean signatures changed
* Key.changes.subkey: Boolean subkeys changed
* 'newkey' if the Key was imported in gpg, and did not exist
* previously,
* 'change' if the key existed, but details were updated. For
* details, Key.changes is available.
* Key.changes.userId: <Boolean> userIds changed
* Key.changes.signature: <Boolean> signatures changed
* Key.changes.subkey: <Boolean> subkeys changed
* // TODO: not yet implemented: Information about Keys that failed
* (e.g. malformed Keys, secretKeys are not accepted)
*/
@ -125,7 +133,7 @@ export class GPGME_Keyring {
msg.post().then(function(response){
let infos = {};
let fprs = [];
for (var res=0; res < response.result[0].imports.length; res++) {
for (let res=0; res < response.result[0].imports.length; res++){
let result = response.result[0].imports[res];
let status = '';
if (result.status === 0){

View File

@ -16,9 +16,13 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
import { permittedOperations } from './permittedOperations'
import { gpgme_error } from './Errors'
import { permittedOperations } from './permittedOperations';
import { gpgme_error } from './Errors';
import { Connection } from './Connection';
export function createMessage(operation){
@ -46,7 +50,7 @@ export class GPGME_Message {
}
set operation (op){
if (typeof(op) === "string"){
if (typeof(op) === 'string'){
if (!this._msg){
this._msg = {};
}
@ -67,10 +71,10 @@ export class GPGME_Message {
}
get expected() {
if (this._expected === "base64"){
if (this._expected === 'base64'){
return this._expected;
}
return "string";
return 'string';
}
/**
@ -104,7 +108,6 @@ export class GPGME_Message {
return true;
}
return gpgme_error('PARAM_WRONG');
break;
case 'number':
if (
poparam.allowed.indexOf('number') >= 0
@ -112,13 +115,12 @@ export class GPGME_Message {
return true;
}
return gpgme_error('PARAM_WRONG');
break;
case 'boolean':
if (poparam.allowed.indexOf('boolean') >= 0){
return true;
}
return gpgme_error('PARAM_WRONG');
break;
case 'object':
if (Array.isArray(val)){
if (poparam.array_allowed !== true){
@ -173,7 +175,6 @@ export class GPGME_Message {
let msg_params = Object.keys(this._msg);
for (let i=0; i < reqParams.length; i++){
if (msg_params.indexOf(reqParams[i]) < 0){
console.log(reqParams[i] + ' missing');
return false;
}
}

View File

@ -16,12 +16,16 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
import {GPGME_Message, createMessage} from './Message'
import {toKeyIdArray} from "./Helpers"
import { gpgme_error } from "./Errors"
import { GPGME_Keyring } from "./Keyring";
import {GPGME_Message, createMessage} from './Message';
import {toKeyIdArray} from './Helpers';
import { gpgme_error } from './Errors';
import { GPGME_Keyring } from './Keyring';
export class GpgME {
/**
@ -47,13 +51,19 @@ export class GpgME {
/**
* Encrypt (and optionally sign) a Message
* @param {String|Object} data text/data to be encrypted as String. Also accepts Objects with a getText method
* @param {GPGME_Key|String|Array<String>|Array<GPGME_Key>} publicKeys Keys used to encrypt the message
* @param {GPGME_Key|String|Array<String>|Array<GPGME_Key>} secretKeys (optional) Keys used to sign the message
* @param {Boolean} base64 (optional) The data is already considered to be in base64 encoding
* @param {String|Object} data text/data to be encrypted as String. Also
* accepts Objects with a getText method
* @param {GPGME_Key|String|Array<String>|Array<GPGME_Key>} publicKeys
* Keys used to encrypt the message
* @param {GPGME_Key|String|Array<String>|Array<GPGME_Key>} secretKeys
* (optional) Keys used to sign the message
* @param {Boolean} base64 (optional) The data is already considered to be
* in base64 encoding
* @param {Boolean} armor (optional) Request the output as armored block
* @param {Boolean} wildcard (optional) If true, recipient information will not be added to the message
* @param {Object} additional use additional gpg options (refer to src/permittedOperations)
* @param {Boolean} wildcard (optional) If true, recipient information will
* not be added to the message
* @param {Object} additional use additional gpg options
* (refer to src/permittedOperations)
* @returns {Promise<Object>} Encrypted message:
* data: The encrypted message
* base64: Boolean indicating whether data is base64 encoded.
@ -64,7 +74,7 @@ export class GpgME {
){
let msg = createMessage('encrypt');
if (msg instanceof Error){
return Promise.reject(msg)
return Promise.reject(msg);
}
msg.setParameter('armor', armor);
msg.setParameter('always-trust', true);
@ -80,7 +90,7 @@ export class GpgME {
putData(msg, data);
if (wildcard === true){
msg.setParameter('throw-keyids', true);
};
}
if (additional){
let additional_Keys = Object.keys(additional);
for (let k = 0; k < additional_Keys.length; k++) {
@ -97,14 +107,16 @@ export class GpgME {
/**
* Decrypt a Message
* @param {String|Object} data text/data to be decrypted. Accepts Strings and Objects with a getText method
* @param {Boolean} base64 (optional) Response is expected to be base64 encoded
* @param {String|Object} data text/data to be decrypted. Accepts Strings
* and Objects with a getText method
* @param {Boolean} base64 (optional) Response is expected to be base64
* encoded
* @returns {Promise<Object>} decrypted message:
data: The decrypted data. This may be base64 encoded.
base64: Boolean indicating whether data is base64 encoded.
mime: A Boolean indicating whether the data is a MIME object.
signatures: Array of signature Objects TODO not yet implemented.
// should be an object that can tell if all signatures are valid etc.
// should be an object that can tell if all signatures are valid .
* @async
*/
decrypt(data, base64=false){
@ -124,14 +136,16 @@ export class GpgME {
/**
* Sign a Message
* @param {String|Object} data text/data to be decrypted. Accepts Strings and Objects with a gettext methos
* @param {GPGME_Key|String|Array<String>|Array<GPGME_Key>} keys The key/keys to use for signing
* @param {String|Object} data text/data to be decrypted. Accepts Strings
* and Objects with a gettext methos
* @param {GPGME_Key|String|Array<String>|Array<GPGME_Key>} keys The
* key/keys to use for signing
* @param {*} mode The signing mode. Currently supported:
* 'clearsign': (default) The Message is embedded into the signature
* 'detached': The signature is stored separately
* @param {*} base64 input is considered base64
* @returns {Promise<Object>}
* data: The resulting data. In clearsign mode this includes the signature
* data: The resulting data. Includes the signature in clearsign mode
* signature: The detached signature (if in detached mode)
* @async
*/
@ -154,7 +168,6 @@ export class GpgME {
if (mode === 'detached') {
msg.expected = 'base64';
}
let me = this;
return new Promise(function(resolve,reject) {
msg.post().then( function(message) {
if (mode === 'clearsign'){
@ -169,7 +182,7 @@ export class GpgME {
}
}, function(error){
reject(error);
})
});
});
}
}
@ -180,7 +193,7 @@ export class GpgME {
* @param {*} data The data to enter
*/
function putData(message, data){
if (!message || !message instanceof GPGME_Message ) {
if (!message || !(message instanceof GPGME_Message) ) {
return gpgme_error('PARAM_WRONG');
}
if (!data){

View File

@ -16,16 +16,21 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
import { GpgME } from "./gpgmejs";
import { gpgme_error } from "./Errors";
import { Connection } from "./Connection";
import { defaultConf, availableConf } from "./Config";
import { GpgME } from './gpgmejs';
import { gpgme_error } from './Errors';
import { Connection } from './Connection';
import { defaultConf, availableConf } from './Config';
/**
* Initializes a nativeMessaging Connection and returns a GPGMEjs object
* @param {Object} config Configuration. See Config.js for available parameters. Still TODO
* @param {Object} config Configuration. See Config.js for available parameters.
* Still TODO
*/
function init(config){
let _conf = parseconfiguration(config);
@ -41,7 +46,7 @@ function init(config){
} else {
reject(gpgme_error('CONN_NO_CONNECT'));
}
}, function(error){
}, function(){ //unspecific connection error. Should not happen
reject(gpgme_error('CONN_NO_CONNECT'));
});
});
@ -50,7 +55,7 @@ function init(config){
function parseconfiguration(rawconfig = {}){
if ( typeof(rawconfig) !== 'object'){
return gpgme_error('PARAM_WRONG');
};
}
let result_config = {};
let conf_keys = Object.keys(rawconfig);
@ -75,8 +80,8 @@ function parseconfiguration(rawconfig = {}){
}
}
return result_config;
};
}
export default {
init: init
}
};

View File

@ -16,6 +16,9 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach <mkrambach@intevation.de>
*/
/**
@ -154,7 +157,8 @@ export const permittedOperations = {
},
'mode': {
allowed: ['string'],
allowed_data: ['detached', 'clearsign'] // TODO 'opaque' not used
allowed_data: ['detached', 'clearsign']
// TODO 'opaque' is not used, but available on native app
},
'base64': {
allowed: ['boolean']
@ -172,8 +176,11 @@ export const permittedOperations = {
}
},
// note: For the meaning of the optional keylist flags, refer to
// https://www.gnupg.org/documentation/manuals/gpgme/Key-Listing-Mode.html
keylist:{
required: {},
optional: {
'protocol': {
allowed: ['string'],
@ -182,8 +189,6 @@ export const permittedOperations = {
'chunksize': {
allowed: ['number'],
},
// note: For the meaning of the flags, refer to
// https://www.gnupg.org/documentation/manuals/gpgme/Key-Listing-Mode.html
'secret': {
allowed: ['boolean']
},
@ -305,11 +310,6 @@ export const permittedOperations = {
infos: []
}
},
/**
*TBD get armored secret different treatment from keyinfo!
* TBD key modification?
*/
version: {
required: {},
@ -321,4 +321,11 @@ export const permittedOperations = {
params:[]
}
}
}
/**
* TBD handling of secrets
* TBD key modification?
* TBD: key generation
*/
};

View File

@ -32,7 +32,7 @@ import { GPGME_Keyring } from './src/Keyring';
import {GPGME_Message, createMessage} from './src/Message';
mocha.setup('bdd');
var expect = chai.expect;
const expect = chai.expect;
chai.config.includeStack = true;
function unittests (){
@ -266,7 +266,8 @@ function unittests (){
expect(keyring.getKeys).to.be.a('function');
});
it('Loading Keys from Keyring, to be used synchronously', function(done){
it('Loading Keys from Keyring, to be used synchronously',
function(done){
let keyring = new GPGME_Keyring;
keyring.getKeys(null, true).then(function(result){
expect(result).to.be.an('array');
@ -276,68 +277,37 @@ function unittests (){
'-----END PGP PUBLIC KEY BLOCK-----');
done();
});
});
}
);
it('Loading specific Key from Keyring, to be used synchronously', function(done){
it('Loading specific Key from Keyring, to be used synchronously',
function(done){
let keyring = new GPGME_Keyring;
keyring.getKeys(kp.validKeyFingerprint, true).then(function(result){
keyring.getKeys(kp.validKeyFingerprint, true).then(
function(result){
expect(result).to.be.an('array');
expect(result[0]).to.be.an.instanceof(GPGME_Key);
expect(result[0].get('armored')).to.be.a('string');
expect(result[0].get('armored')).to.include(
'-----END PGP PUBLIC KEY BLOCK-----');
done();
});
});
}
);
}
);
it('Querying non-existing Key from Keyring', function(done){
let keyring = new GPGME_Keyring;
keyring.getKeys(kp.invalidKeyFingerprint, true).then(function(result){
keyring.getKeys(kp.invalidKeyFingerprint, true).then(
function(result){
expect(result).to.be.an('array');
expect(result.length).to.equal(0);
done();
});
});
}
);
});
// describe('Keyring import/export', function(){
// before(function(done) {
// let keyring = new GPGME_Keyring;
// keyring.getKeys(ak.fingerprint, false).then(function(result){
// if (result.length === 1){
// result[0].delete().then(function(delete_result){
// if (delete_result === true){
// done();
// }
// });
// } else {
// done();
// }
// });
// });
// it('Import Public Key', function(done){
// keyring.importKey(ak.key).then(function(result){
// expect(result).to.be.an('array');
// expect(result[0].key).to.be.an.instanceof(GPGME_Key);
// expect(result[0].changed).to.equal('newkey');
// expect(result[0].key.keyring).to.equal(ak.fingerprint);
// done();
// });
// });
// it('Update Public Key', function(done){
// keyring.importKey(ak.key).then(function(result){
// expect(result).to.be.an('array');
// expect(result[0].key).to.be.an.instanceof(GPGME_Key);
// expect(result[0].changed).to.equal('change');
// expect(result[0].changes.userId).to.be.true;
// expect(result[0].changes.subkeys).to.be.false;
// expect(result[0].key.keyring).to.equal(ak.fingerprint);
// done();
// });
// });
// });
});
describe('GPGME_Message', function(){