.expect(200, function(err, res) { validator(res.body, function(err, info){ if (err) return done(err); info.version.should.equal('1.0.0'); done(); }); });
function validate(workspace, callback) { validator(workspace.assertion, function(error, info) { if (error) return callback(error); workspace.validation = info; return callback(null, workspace); }); }
return getAssertion(thing, function (error, assertion) { if (error) return callback(error); return validator(assertion, function(err, info){ callback(err, {assertion: assertion, info: info}); }); });
var Request = fibrous(function Request(backpack, urlOrSignature, done) { var self = { result: null, error: null, badgeInfo: null, canBeAccepted: false }; var extend = _.extend.bind(_, self); self.accept = fibrous(function(accept, cb) { assert(self.canBeAccepted); self.canBeAccepted = false; if (!cb) { cb = accept; accept = undefined; } if (typeof(accept) == "undefined") accept = true; if (accept) { backpack.receive(self.badgeInfo, function(err) { cb(err, extend({ result: err ? "backpack_error" : "accepted", error: err || null })); }); } else cb(null, extend({result: "rejected"})); }); self.reject = self.accept.bind(self, false); validator(urlOrSignature, function(err, info) { if (err) return done(null, extend({result: "invalid", error: err})); self.badgeInfo = info; if (!validator.doesRecipientMatch(info, backpack.owner)) return done(null, extend({ result: "recipient_mismatch", error: new Error("badge recipient is not " + backpack.owner) })); backpack.has(info.guid, function(err, isInBackpack) { if (err) return done(err, extend({ result: "backpack_error", error: err })); if (isInBackpack) return done(null, extend({ result: "exists", error: new Error("badge is already in backpack") })); return done(null, extend({canBeAccepted: true})); }); }); return self; });
module.exports = function analyze(thing, callback) { if (/^http/.exec(thing)) { return getAssertion(thing, function (error, assertion) { if (error) return callback(error); return validator(assertion, function(err, info){ callback(err, {assertion: assertion, info: info}); }); }); } if (isObject(thing) || validator.isSignedBadge(thing)) return validator(thing, function(err, info){ callback(err, {assertion: thing, info: info}); }); return callback(makeError({ name: 'TypeError', message: 'invalid input: requires valid url, assertion, or signature', code: 'input', })); }
exports.validate = function validate(req, res, next) { var respond; if (req.accepts(['html', 'json']) === 'json') { respond = function respondJson(obj) { return res.json(200, obj); }; } else { respond = function respondHtml(obj) { var template = req.xhr ? 'response.html' : 'index.html'; return res.render(template, { valid: obj.status === 'valid', response: obj }); }; } const assertionString = req.body.assertion || null; if (!assertionString) return respond({ status: 'invalid', reason: 'missing assertion', error: {} }); res.locals.assertionString = assertionString; var assertionOrSignature; try { assertionOrSignature = JSON.parse(assertionString); } catch(e) { /* Assume signature and move on */ assertionOrSignature = assertionString; } res.locals.assertion = assertionOrSignature; const startTime = new Date(); log.info({ assertion: assertionOrSignature }, 'Validating'); validator(assertionOrSignature, function (err, info) { const responseTime = new Date() - startTime; log.info({ assertion: assertionOrSignature, err: err, responseTime: responseTime, validation: info }, 'Validation result'); if (info && 'resources' in info) delete info.resources; if (err) return respond({ status: 'invalid', reason: err.message, error: err, info: info }); return respond({ status: 'valid', info: info }); }); };