aboutsummaryrefslogtreecommitdiffstats
path: root/lang/js/src
diff options
context:
space:
mode:
authorMaximilian Krambach <[email protected]>2018-05-14 14:23:24 +0000
committerMaximilian Krambach <[email protected]>2018-05-14 14:23:24 +0000
commit987b31746809dfe04966e37edd759a448a28d975 (patch)
tree8f1bc971f712c55740af924333557e21c9a99d86 /lang/js/src
parentjs: more testing of nativeMessaging connection (diff)
downloadgpgme-987b31746809dfe04966e37edd759a448a28d975.tar.gz
gpgme-987b31746809dfe04966e37edd759a448a28d975.zip
js: Tests and improvements for openpgp mode
-- * Added openpgp - Mode tests to the browsertest Extension. These tests require openpgp, which should not be a hard dependency for the main project. Packing openpgpjs into the extension is still TODO * Fixes: - openpgp mode API now correctly handles parameters as an object, similar to openpgpjs - proper check and parsing of openpgpjs Message Objects
Diffstat (limited to 'lang/js/src')
-rw-r--r--lang/js/src/Connection.js1
-rw-r--r--lang/js/src/gpgmejs.js18
-rw-r--r--lang/js/src/gpgmejs_openpgpjs.js77
3 files changed, 59 insertions, 37 deletions
diff --git a/lang/js/src/Connection.js b/lang/js/src/Connection.js
index 64621f60..1931a55b 100644
--- a/lang/js/src/Connection.js
+++ b/lang/js/src/Connection.js
@@ -181,7 +181,6 @@ class Answer{
if (!this._response.hasOwnProperty(key)){
this._response[key] = '';
}
- // console.log(msg[key]);
this._response[key] += msg[key];
}
//params should not change through the message
diff --git a/lang/js/src/gpgmejs.js b/lang/js/src/gpgmejs.js
index c1a01377..d106f4f7 100644
--- a/lang/js/src/gpgmejs.js
+++ b/lang/js/src/gpgmejs.js
@@ -80,7 +80,6 @@ export class GpgME {
let pubkeys = toKeyIdArray(publicKeys);
msg.setParameter('keys', pubkeys);
-
putData(msg, data);
if (wildcard === true){msg.setParameter('throw-keyids', true);
};
@@ -171,19 +170,32 @@ function putData(message, data){
return gpgme_error('PARAM_WRONG');
} else if (data instanceof Uint8Array){
message.setParameter('base64', true);
+ // TODO: btoa turns the array into a string
+ // of comma separated of numbers
+ // atob(data).split(',') would result in a "normal" array of numbers
+ // atob(btoa(data)).split(',') would result in a "normal" array of numbers
+ // would result in a "normal" array of numbers
message.setParameter ('data', btoa(data));
+
} else if (typeof(data) === 'string') {
message.setParameter('base64', false);
message.setParameter('data', data);
- } else if ( typeof(data) === 'object' && data.hasOwnProperty('getText')){
+ } else if (
+ typeof(data) === 'object' &&
+ typeof(data.getText) === 'function'
+ ){
let txt = data.getText();
if (txt instanceof Uint8Array){
message.setParameter('base64', true);
message.setParameter ('data', btoa(txt));
}
- else {
+ else if (typeof(txt) === 'string'){
+ message.setParameter('base64', false);
+ message.setParameter ('data', txt);
+ } else {
return gpgme_error('PARAM_WRONG');
}
+
} else {
return gpgme_error('PARAM_WRONG');
}
diff --git a/lang/js/src/gpgmejs_openpgpjs.js b/lang/js/src/gpgmejs_openpgpjs.js
index b233f0fa..9c8cd2cc 100644
--- a/lang/js/src/gpgmejs_openpgpjs.js
+++ b/lang/js/src/gpgmejs_openpgpjs.js
@@ -25,10 +25,10 @@
*/
import { GpgME } from "./gpgmejs";
- import {GPGME_Keyring} from "./Keyring"
+ import {GPGME_Keyring} from "./Keyring";
import { GPGME_Key, createKey } from "./Key";
- import { isFingerprint } from "./Helpers"
- import { gpgme_error } from "./Errors"
+ import { isFingerprint } from "./Helpers";
+ import { gpgme_error } from "./Errors";
import { Connection } from "./Connection";
@@ -60,8 +60,8 @@ import { Connection } from "./Connection";
/**
* Encrypt Message
* Supported:
- * @param {String|Uint8Array} data
- * //an openpgp Message also accepted here. TODO: is this wanted?
+ * @param {String|Message} data
+ * an openpgp Message is accepted here.
* @param {Key|Array<Key>} publicKeys
* //Strings of Fingerprints
* @param {Boolean} wildcard
@@ -86,36 +86,39 @@ import { Connection } from "./Connection";
* @async
* @static
*/
- encrypt({data = '', publicKeys = '', privateKeys, passwords=null,
- sessionKey = null, filename, compression, armor=true, detached=false,
- signature=null, returnSessionKey=null, wildcard=false, date=null}) {
- if (passwords !== null
- || sessionKey !== null
- || signature !== null
- || returnSessionKey !== null
- || date !== null
+ encrypt(options) {
+ if (!options || typeof(options) !== 'object'){
+ return Promise.reject(gpgme_error('PARAM_WRONG'));
+ }
+ if (options.passwords
+ || options.sessionKey
+ || options.signature
+ || options.returnSessionKey
+ || (options.hasOwnProperty('date') && options.date !== null)
){
- return Promise.reject(GPMGEJS_Error('NOT_IMPLEMENTED'));
+ return Promise.reject(gpgme_error('NOT_IMPLEMENTED'));
}
- if ( privateKeys
- || compression
- || armor === false
- || detached == true){
- return Promise.reject(gpgme_error('NOT_YET_IMPLEMENTED'));
+ if ( options.privateKeys
+ || options.compression
+ || (options.hasOwnProperty('armor') && options.armor === false)
+ || (options.hasOwnProperty('detached') && options.detached == true)
+ ){
+ return Promise.reject(gpgme_error('NOT_YET_IMPLEMENTED'));
}
- if (filename){
+ if (options.filename){
if (this._config.unconsidered_params === 'warn'){
- GPMGEJS_Error('PARAM_IGNORED');
+ gpgme_error('PARAM_IGNORED');
} else if (this._config.unconsidered_params === 'error'){
- return Promise.reject(GPMGEJS_Error('NOT_IMPLEMENTED'));
+ return Promise.reject(gpgme_error('NOT_IMPLEMENTED'));
}
}
- return this._GpgME.encrypt(data, translateKeys(publicKeys), wildcard);
+ return this._GpgME.encrypt(
+ options.data, options.publicKeys, options.wildcard);
}
/** Decrypt Message
* supported openpgpjs parameters:
- * @param {Message|Uint8Array|String} message Message object from openpgpjs
+ * @param {Message|String} message Message object from openpgpjs
* Unsupported:
* @param {String|Array<String>} passwords
* @param {Key|Array<Key>} privateKeys
@@ -128,26 +131,33 @@ import { Connection } from "./Connection";
* @param {Key|Array<Key>} publicKeys
*
* @returns {Promise<Object>} decrypted and verified message in the form:
- * { data:Uint8Array|String, filename:String, signatures:[{ keyid:String, valid:Boolean }] }
+ * { data:String, filename:String, signatures:[{ keyid:String, valid:Boolean }] }
* @async
* @static
*/
- decrypt({ message, privateKeys, passwords=null, sessionKeys,
- publicKeys, format='utf8', signature=null, date= null}) {
- if (passwords !== null || sessionKeys || privateKeys){
+ decrypt(options) {
+ if (options.passwords
+ || options.sessionKeys
+ || options.privateKeys
+ ){
return Promise.reject(gpgme_error('NOT_IMPLEMENTED'));
}
- if ( format !== 'utf8' || signature){
+ if ((options.hasOwnProperty('format') && options.format !== 'utf8')
+ || options.signature
+ ){
return Promise.reject(gpgme_error('NOT_YET_IMPLEMENTED'));
}
- if (date !== null || publicKeys){
+ if ((options.hasOwnProperty('date') && options.date !== null)
+ || options.publicKeys
+ ){
if (this._config.unconsidered_params === 'warn'){
GPMGEJS_Error('PARAM_IGNORED');
} else if (this._config.unconsidered_params === 'reject'){
return Promise.reject(GPMGEJS_Error('NOT_IMPLEMENTED'));
}
}
- return this._GpgME.decrypt(message);
+ return this._GpgME.decrypt(options.message);
+
// TODO: translate between:
// openpgp:
// { data:Uint8Array|String,
@@ -276,14 +286,15 @@ class GPGME_Key_openpgpmode {
* @returns {Array<GPGME_Key_openpgpmode>}
*/
function translateKeys(input){
+ //TODO: does not check if inpout is okay!
if (!input){
return null;
}
if (!Array.isArray(input)){
input = [input];
}
- let resultset;
- for (let i=0; i< input.length; i++){
+ let resultset = [];
+ for (let i=0; i< input.length; i++) {
resultset.push(new GPGME_Key_openpgpmode(input[i]));
}
return resultset;