Commit 34231be7 authored by Cédric Moreau's avatar Cédric Moreau

Memberships: better filter + mark as eligible on reception

parent 99a7b8f6
......@@ -9,10 +9,10 @@ module.exports = {
},
fromArmored: function (armored){
var readKeys = openpgp.key.readArmored(asciiArmored).keys;
var readKeys = openpgp.key.readArmored(armored).keys;
var packets = new PacketList();
if(readKeys.length == 1){
packets = readKeys[0].toPacketList();
packets = readKeys[0].toPacketlist();
}
return new KeyHelper(packets);
}
......
......@@ -142,4 +142,11 @@ MembershipSchema.statics.getForHashAndIssuer = function (hash, issuer, done) {
.exec(done);
}
MembershipSchema.statics.removeEligible = function (issuer, done) {
this
.find({ issuer: issuer, eligible: true })
.remove(done);
}
module.exports = MembershipSchema;
......@@ -42,12 +42,33 @@ function KeyService (conn, conf, PublicKeyService) {
Membership.getForHashAndIssuer(entry.hash, entry.issuer, next);
},
function (entries, next){
if (entries.length > 0) {
if (entries.length > 0 && entries[0].date > entry.date) {
next('Already received membership');
}
else next();
else Key.isMember(entry.issuer, next);
},
function (next){
function (isMember, next){
var isJoin = entry.membership == 'IN';
if (!isMember && isJoin) {
hasEligiblePubkey(entry.issuer, next);
}
else if (isMember && !isJoin) {
next(null, true);
} else {
if (isJoin)
next('A member cannot join in.');
else
next('A non-member cannot leave.');
}
},
function (isClean, next){
if (!isClean) {
next('Needs an eligible public key (with udid2)');
return;
}
Membership.removeEligible(entry.issuer, next);
},
function (nbDeleted, next) {
// Saves entry
entry.save(function (err) {
next(err);
......@@ -60,6 +81,23 @@ function KeyService (conn, conf, PublicKeyService) {
], done);
};
function hasEligiblePubkey (fpr, done) {
async.waterfall([
function (next){
PublicKey.getTheOne(fpr, next);
},
function (pubkey, next){
if (pubkey.keychain)
next(null, true); // Key is already in the chain
else {
// Key is not in the keychain: valid if it has a valid udid2 (implying pubkey + self certificatio)
var wrappedKey = require('../lib/keyhelper').fromArmored(pubkey.raw);
next(null, wrappedKey.hasValidUdid2());
}
},
], done);
}
this.submitKeyBlock = function (kb, done) {
var block = new KeyBlock(kb);
var currentBlock = null;
......@@ -743,7 +781,6 @@ function KeyService (conn, conf, PublicKeyService) {
next();
});
}, function (err) {
console.log(raw);
block.signature = sig;
var end = new Date().timestamp();
var duration = moment.duration((end - start)) + 's';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment