js: Demoextension update

--

* src/Signature: typo
* src/gpgmejs.js: fixed wrong scope in verification
* right now verify does not succeed in the DemoExtension.
  This is probably a problem in conversion or line ending.
This commit is contained in:
Maximilian Krambach 2018-06-20 13:42:19 +02:00
parent d27703ea4f
commit 88e7f8ec2e
5 changed files with 99 additions and 40 deletions

View File

@ -27,7 +27,7 @@ document.addEventListener('DOMContentLoaded', function() {
Gpgmejs.init().then(function(gpgmejs){ Gpgmejs.init().then(function(gpgmejs){
document.getElementById('buttonencrypt').addEventListener('click', document.getElementById('buttonencrypt').addEventListener('click',
function(){ function(){
let data = document.getElementById('cleartext').value; let data = document.getElementById('inputtext').value;
let keyId = document.getElementById('pubkey').value; let keyId = document.getElementById('pubkey').value;
gpgmejs.encrypt(data, keyId).then( gpgmejs.encrypt(data, keyId).then(
function(answer){ function(answer){
@ -42,7 +42,7 @@ document.addEventListener('DOMContentLoaded', function() {
document.getElementById('buttondecrypt').addEventListener('click', document.getElementById('buttondecrypt').addEventListener('click',
function(){ function(){
let data = document.getElementById('ciphertext').value; let data = document.getElementById('inputtext').value;
gpgmejs.decrypt(data).then( gpgmejs.decrypt(data).then(
function(answer){ function(answer){
if (answer.data){ if (answer.data){
@ -57,11 +57,46 @@ document.addEventListener('DOMContentLoaded', function() {
document.getElementById('getdefaultkey').addEventListener('click', document.getElementById('getdefaultkey').addEventListener('click',
function(){ function(){
gpgmejs.Keyring.getDefaultKey().then(function(answer){ gpgmejs.Keyring.getDefaultKey().then(function(answer){
document.getElementById('defaultkey').textContent = document.getElementById('pubkey').value =
answer.fingerprint; answer.fingerprint;
}, function(errormsg){ }, function(errormsg){
alert(errormsg.message); alert(errormsg.message);
}); });
}); });
document.getElementById('signtext').addEventListener('click',
function(){
let data = document.getElementById('inputtext').value;
let keyId = document.getElementById('pubkey').value;
gpgmejs.sign(data, keyId).then(
function(answer){
if (answer.data){
document.getElementById(
'answer').value = answer.data;
}
}, function(errormsg){
alert( errormsg.message);
});
});
document.getElementById('verifytext').addEventListener('click',
function(){
let data = document.getElementById('inputtext').value;
gpgmejs.verify(data).then(
function(answer){
let vals = '';
if (answer.all_valid === true){
vals = 'Success! ';
} else {
vals = 'Failure! ';
}
vals = vals + (answer.count - answer.failures) + 'of '
+ answer.count + ' signature(s) were successfully '
+ 'verified.\n\n' + answer.data;
document.getElementById('answer').value = vals;
}, function(errormsg){
alert( errormsg.message);
});
});
}); });
}); });

View File

@ -7,38 +7,37 @@
<script src="maindemo.js"></script> <script src="maindemo.js"></script>
</head> </head>
<body> <body>
<div>
<div class="left">
<ul> <ul>
<li> <li>
<span class="label">Text: </span> <span class="label">Input</span>
<input type="text" id='cleartext' /> <textarea rows="5" cols="65" id="inputtext" wrap="hard"></textarea>
</li> </li>
<li> <li>
<span class="label">Public key ID: </span> <span class="label">Fingerprint of Key to use: </span>
<input type="text" id="pubkey" value="" /> </li>
<input type="text" id="pubkey" value="" /> <br>
<button id="getdefaultkey">Set to default signing key</button>
</li> </li>
</ul> </ul>
<button id="buttonencrypt">Encrypt</button><br> </div>
<hr> <div class="right">
<ul> <ul>
<li> <li>
<span class="label">Encrypted armored Text: </span> <span class="label">Result</span>
<textarea rows="5" cols="65" id="ciphertext" wrap="hard"></textarea>
</li>
</ul>
<button id="buttondecrypt">Decrypt</button><br>
<hr>
<h3>Result data:</h3>
<textarea id="answer" rows="5" cols="65" wrap="hard"></textarea> <textarea id="answer" rows="5" cols="65" wrap="hard"></textarea>
<hr>
<ul>
<li>
<span class="label">Default Key:</span>
<button id="getdefaultkey">Get</button><br>
<span id="defaultkey"></span>
</li> </li>
</ul> </ul>
</body> </div>
</div>
<div class="center">
<button id="buttonencrypt">Encrypt input text</button><br>
<button id="buttondecrypt">Decrypt input text</button><br>
<button id="signtext">Sign input text</button> <br>
<button id="verifytext">Verify input text</button><br>
</div>
</body>
</html> </html>

View File

@ -8,3 +8,26 @@ ul li span {
width: 120px; width: 120px;
margin-top: 6px; margin-top: 6px;
} }
div .left {
float: left;
align-items: stretch;
width: 40%;
}
div .center {
width: 50%;
align-content: space-between;
}
div .center button {
align-self: stretch;
}
div .right {
float: right;
align-items: stretch;
width: 40%;
}
div .bottom {
clear:both;
}

View File

@ -35,7 +35,7 @@ export function createSignature(sigObject){
if ( if (
typeof(sigObject) !=='object' || typeof(sigObject) !=='object' ||
!sigObject.hasOwnProperty('summary') || !sigObject.hasOwnProperty('summary') ||
!sigObject.hasOwnProperty('fingerpprint') || !sigObject.hasOwnProperty('fingerprint') ||
!sigObject.hasOwnProperty('timestamp') !sigObject.hasOwnProperty('timestamp')
//TODO check if timestamp is mandatory in specification //TODO check if timestamp is mandatory in specification
){ ){
@ -67,6 +67,7 @@ export function createSignature(sigObject){
} }
} }
} }
console.log('sig created');
return new GPGME_Signature(sigObject); return new GPGME_Signature(sigObject);
} }
@ -178,7 +179,8 @@ const expSum = {
'crl-missing': 'boolean', 'crl-missing': 'boolean',
'crl-too-old': 'boolean', 'crl-too-old': 'boolean',
'bad-policy': 'boolean', 'bad-policy': 'boolean',
'sys-error': 'boolean' 'sys-error': 'boolean',
'sigsum': 'object'
}; };
/** /**

View File

@ -168,7 +168,7 @@ export class GpgME {
/** /**
* Sign a Message * Sign a Message
* @param {String|Object} data text/data to be decrypted. Accepts Strings * @param {String|Object} data text/data to be signed. Accepts Strings
* and Objects with a gettext methos * and Objects with a gettext methos
* @param {GPGME_Key|String|Array<String>|Array<GPGME_Key>} keys The * @param {GPGME_Key|String|Array<String>|Array<GPGME_Key>} keys The
* key/keys to use for signing * key/keys to use for signing
@ -238,7 +238,7 @@ export class GpgME {
*/ */
verify(data, signature, base64 = false){ verify(data, signature, base64 = false){
let msg = createMessage('verify'); let msg = createMessage('verify');
let dt = this.putData(msg, data); let dt = putData(msg, data);
if (dt instanceof Error){ if (dt instanceof Error){
return Promise.reject(dt); return Promise.reject(dt);
} }
@ -317,7 +317,7 @@ function collectSignatures(sigs){
for (let i=0; i< sigs.length; i++){ for (let i=0; i< sigs.length; i++){
let sigObj = createSignature(sigs[i]); let sigObj = createSignature(sigs[i]);
if (sigObj instanceof Error){ if (sigObj instanceof Error){
return gpgme_error('SIG_WRONG'); return gpgme_error(sigObj);
} }
if (sigObj.valid !== true){ if (sigObj.valid !== true){
summary.failures += 1; summary.failures += 1;