All files signstore.js

89.58% Statements 43/48
66.66% Branches 2/3
100% Functions 1/1
89.58% Lines 43/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x           2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * 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 dbClient {MongoClient} - the db client used for this request
 *
 * @return {Object} Response returned from pubsub api call
 */
exports.route = async (path, req, dbClient) => {
  const isValid = validateRequest(req);
  if (!isValid.valid) {
    return {
      error: 400,
      message: isValid.message
    }
  }
 
  const isAuthed = await validateSignature(req.token, req.signature, req.payload, dbClient);
  if (!isAuthed) {
    return {
      error: 404,
      message: "Not authenticated!"
    }
  }
 
  const inrReq = new INRRequest(path, req);
  return await inrReq.pubSubIt(dbClient);
}