Commit 96746e0d authored by Cédric Moreau's avatar Cédric Moreau

WoT server: receive Memberships

parent 946896bd
......@@ -23,6 +23,7 @@ module.exports = function (wotServer) {
function KeychainBinding (wotServer) {
var that = this;
var conf = wotServer.conf;
// Services
var http = wotServer.HTTPService;
......
......@@ -134,12 +134,10 @@ module.exports = new function() {
var raw = "";
raw += "Version: " + json.version + "\n";
raw += "Currency: " + json.currency + "\n";
raw += "Registry: " + json.type + "\n";
raw += "Issuer: " + json.issuer + "\n";
raw += "Date: " + json.date.timestamp() + "\n";
raw += "Membership: " + json.membership + "\n";
raw += "AmendmentNumber: " + json.amNumber + "\n";
raw += "AmendmentHash: " + json.amHash + "\n";
raw += "UserID: " + json.userid + "\n";
return unix2dos(raw);
};
......
......@@ -13,11 +13,9 @@ function MembershipParser (onError) {
var captures = [
{prop: "version", regexp: /Version: (.*)/},
{prop: "currency", regexp: /Currency: (.*)/},
{prop: "type", regexp: /Registry: (.*)/},
{prop: "issuer", regexp: /Issuer: (.*)/},
{prop: "amNumber", regexp: /AmendmentNumber: (.*)/},
{prop: "amHash", regexp: /AmendmentHash: (.*)/},
{prop: "membership", regexp: /Membership: (.*)/},
{prop: "userid", regexp: /UserID: (.*)/},
{prop: "date", regexp: /Date: (.*)/, parser: parseDateFromTimestamp}
];
var multilineFields = [];
......@@ -36,34 +34,18 @@ function MembershipParser (onError) {
'BAD_MEMBERSHIP': 153,
'BAD_REGISTRY_TYPE': 154,
'BAD_DATE': 155,
'BAD_AM_NUMBER': 156,
'BAD_AM_HASH': 157,
'BAD_USERID': 156,
}
if(!err){
// Version
if(!obj.version || !obj.version.match(/^1$/))
err = {code: codes['BAD_VERSION'], message: "Version unknown"};
}
if(!err){
// Registry document type
if(!obj.type || !obj.type.match("^MEMBERSHIP$"))
err = {code: codes['BAD_REGISTRY_TYPE'], message: "Incorrect Registry field: must be MEMBERSHIP"};
}
if(!err){
// Fingerprint
if(obj.issuer && !obj.issuer.match(/^[A-Z\d]+$/))
err = {code: codes['BAD_FINGERPRINT'], message: "Incorrect issuer field"};
}
if(!err){
// AmendmentNumber
if(!obj.amNumber || !obj.amNumber.match(/^\d+$/))
err = {code: codes['BAD_AM_NUMBER'], message: "Incorrect AmendmentNumber field"};
}
if(!err){
// AmendmentHash
if(obj.amHash && !obj.amHash.match(/^[A-Z\d]+$/))
err = {code: codes['BAD_AM_HASH'], message: "Incorrect AmendmentHash field"};
}
if(!err){
// Membership
if(obj.membership && !obj.membership.match(/^(IN|OUT)$/))
......@@ -74,6 +56,11 @@ function MembershipParser (onError) {
if(obj.date && (typeof obj == 'string' ? !obj.date.match(/^\d+$/) : obj.date.timestamp() <= 0))
err = {code: codes['BAD_DATE'], message: "Incorrect Date field: must be a positive or zero integer"};
}
if(!err){
// UserID
if(!obj.userid || !obj.userid.match(/udid2/))
err = {code: codes['BAD_USERID'], message: "UserID must match udid2 format"};
}
return err && err.message;
};
}
......
......@@ -134,4 +134,12 @@ MembershipSchema.statics.getHistory = function (issuer, done) {
.exec(done);
}
MembershipSchema.statics.getForHashAndIssuer = function (hash, issuer, done) {
this
.find({ issuer: issuer, hash: hash })
.sort({ 'sigDate': -1 })
.exec(done);
}
module.exports = MembershipSchema;
var jpgp = require('../lib/jpgp');
var async = require('async');
var _ = require('underscore');
var jpgp = require('../lib/jpgp');
var async = require('async');
var _ = require('underscore');
var mlogger = require('../lib/logger')('membership');
module.exports.get = function (conn, conf) {
return new KeyService(conn, conf);
};
function KeyService (conn, conf) {
var Membership = conn.model('Membership');
this.load = function (done) {
done();
};
this.submitMembership = function (ms, done) {
var entry = new Membership(ms);
async.waterfall([
function (next){
mlogger.debug('⬇ %s %s', entry.issuer, entry.membership);
// Get already existing Membership with same parameters
Membership.getForHashAndIssuer(entry.hash, entry.issuer, next);
},
function (entries, next){
if (entries.length > 0) {
next('Already received membership');
}
else next();
},
function (next){
// Saves entry
entry.save(function (err) {
next(err);
});
},
function (next){
mlogger.debug('✔ %s %s', entry.issuer, entry.membership);
next(null, entry);
}
], done);
};
}
......@@ -286,7 +286,7 @@ program
program
.command('start')
.description('Start uCoin server using given --currency')
.action(service(LISTEN_HTTP, ucoin.createPKSServer, function (server, conf) {
.action(service(LISTEN_HTTP, ucoin.createWOTServer, function (server, conf) {
// server
// .pipe(router(server.PeeringService.cert.fingerprint, server.conn))
......
......@@ -16,7 +16,7 @@ function WOTServer (dbConf, overrideConf, interceptors, onInit) {
treatment: function (server, obj, next) {
async.waterfall([
function (next){
that.KeychainService.submit(obj, next);
that.KeychainService.submitMembership(obj, next);
},
function (membership, next){
that.emit('membership', membership);
......
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