/**
* Signstore implementation.
*
* Implements methods to handle REST requests and pass to pubsub on
* behalf of the REST client and return the results.
*
* @author michael whobrey <mwhobrey@idealss.com>
*/
const { validateRequest, pubSubIt, validateSignature } = require("./utils");
const INRRequest = require("./request");
/**
* Process incoming route requests.
*
* Validate requests are going to an existing route,
* route them to the correct endpoint, and return an
* appropriate result.
*
* @param path {string} - the request path
* @param req {Object} - the request body
* @param req.token {string} - the token of the client being used to make the request
* @param req.signature {string} - the signature of req.payload + private key
* @param req.payload {Object} - the actual api body to be used to make the pubsub request (structure varies)
* @param payload {string} - verbatim string from request body without any json parsing, only used for signature validation
* @param dbClient {MongoClient} - the db client used for this request
*
* @return {Object} Response returned from pubsub api call
*/
exports.route = async (path, req, payload, dbClient) => {
const isValid = validateRequest(req);
if (!isValid.valid) {
return {
error: 400,
message: isValid.message
}
}
const isAuthed = await validateSignature(req.token, req.signature, payload, dbClient);
if (!isAuthed) {
return {
error: 404,
message: "Not authenticated!"
}
}
const inrReq = new INRRequest(path, req);
try {
return await inrReq.pubSubIt(dbClient);
} catch (err) {
console.error(err);
return {
error: 500,
message: err.message
}
}
}