Convert keyring to use key interface rather than the packetlist.

This commit is contained in:
Robert Nelson 2013-12-08 19:35:01 -08:00
parent bfba0eca0c
commit f00d3210aa
2 changed files with 56 additions and 22 deletions

View File

@ -56,7 +56,7 @@ var keyring = function() {
} }
this.store = store; this.store = store;
function emailKeyCheck(key, email) { function emailCheck(email, key) {
email = email.toLowerCase(); email = email.toLowerCase();
var keyEmails = key.getUserIds(); var keyEmails = key.getUserIds();
for (var i; i < keyEmails.length; i++) { for (var i; i < keyEmails.length; i++) {
@ -69,18 +69,20 @@ var keyring = function() {
return false; return false;
} }
function idKeyCheck(key, id) { function idCheck(id, key) {
var keyid = key.getKeyId(); var keyids = key.getKeyIds();
if (keyid && keyid.write() == id) { for (var i = 0; i < keyids.length; i++) {
return true; if (openpgp.util.hexstrdump(keyids[i].write()) == id) {
return true;
}
return false;
} }
return false;
} }
function checkForIdentityAndPacketMatch(identityFunction, identityInput, keyType) { function checkForIdentityAndKeyTypeMatch(keys, identityFunction, identityInput, keyType) {
var results = []; var results = [];
for (var p = 0; p < this.keys.length; p++) { for (var p = 0; p < keys.length; p++) {
var key = this.keys[p]; var key = keys[p];
switch (keyType) { switch (keyType) {
case openpgp.enums.packet.public_key: case openpgp.enums.packet.public_key:
if (key.isPublic() && identityFunction(identityInput, key)) { if (key.isPublic() && identityFunction(identityInput, key)) {
@ -96,7 +98,7 @@ var keyring = function() {
} }
return results; return results;
} }
this.checkForIdentityAndPacketMatch = checkForIdentityAndPacketMatch; this.checkForIdentityAndKeyTypeMatch = checkForIdentityAndKeyTypeMatch;
/** /**
* searches all public keys in the keyring matching the address or address part of the user ids * searches all public keys in the keyring matching the address or address part of the user ids
@ -104,7 +106,7 @@ var keyring = function() {
* @return {openpgp.key.Key[]} The public keys associated with provided email address. * @return {openpgp.key.Key[]} The public keys associated with provided email address.
*/ */
function getPublicKeyForAddress(email) { function getPublicKeyForAddress(email) {
return checkForIdentityAndPacketMatch(emailPacketCheck, email, openpgp.enums.packet.public_key); return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.public_key);
} }
this.getPublicKeyForAddress = getPublicKeyForAddress; this.getPublicKeyForAddress = getPublicKeyForAddress;
@ -114,7 +116,7 @@ var keyring = function() {
* @return {openpgp.key.Key[]} private keys found * @return {openpgp.key.Key[]} private keys found
*/ */
function getPrivateKeyForAddress(email) { function getPrivateKeyForAddress(email) {
return checkForIdentityAndPacketMatch(emailPacketCheck, email, openpgp.enums.packet.secret_key); return checkForIdentityAndKeyTypeMatch(this.keys, emailCheck, email, openpgp.enums.packet.secret_key);
} }
this.getPrivateKeyForAddress = getPrivateKeyForAddress; this.getPrivateKeyForAddress = getPrivateKeyForAddress;
@ -124,7 +126,7 @@ var keyring = function() {
* @return {openpgp.key.Key[]} public keys found * @return {openpgp.key.Key[]} public keys found
*/ */
function getKeysForKeyId(keyId) { function getKeysForKeyId(keyId) {
return this.checkForIdentityAndPacketMatch(idPacketCheck, keyId); return this.checkForIdentityAndKeyTypeMatch(this.keys, idCheck, keyId, openpgp.enums.packet.public_key);
} }
this.getKeysForKeyId = getKeysForKeyId; this.getKeysForKeyId = getKeysForKeyId;

View File

@ -61,15 +61,47 @@ describe('Openpgp integration tests', function() {
}); });
}); });
describe('Import key pair', function() { describe('Keyring', function() {
it('should work', function(done) { describe('Import key pair', function() {
// clear any keypair already in the keychain it('should work', function(done) {
keyring.init(); // clear any keypair already in the keychain
// import private key keyring.init();
keyring.importKey(privkey); keyring.importKey(privkey);
// import public key keyring.importKey(pubkey);
keyring.importKey(pubkey); done();
done(); });
});
describe('Retrieve keys', function() {
it('getPublicKeyForAddress() - unknown address', function(done) {
var key = keyring.getPublicKeyForAddress('nobody@example.com');
expect(key).to.be.empty;
done();
});
it('getPrivateKeyForAddress() - unknown address', function(done) {
var key = keyring.getPrivateKeyForAddress('nobody@example.com');
expect(key).to.be.empty;
done();
});
it('getPublicKeyForAddress() - valid address', function(done) {
var key = keyring.getPublicKeyForAddress(user);
expect(key).to.exist;
done();
});
it('getPrivateKeyForAddress() - valid address', function(done) {
var key = keyring.getPrivateKeyForAddress(user);
expect(key).to.exist;
done();
});
it('getKeysForKeyId() - unknown id', function(done) {
var keys = keyring.getKeysForKeyId('000102030405060708');
expect(keys).to.be.empty;
done();
});
it('getKeysForKeyId() - valid id', function(done) {
var keys = keyring.getKeysForKeyId(keyId.toLowerCase());
expect(keys).to.exist.and.have.length(1);
done();
});
}); });
}); });