<commit_before>'use strict';

const crypto = require('crypto');
const logger = require('heroku-logger');
const helpers = require('../../helpers');

/**
 * Validates incoming Front request.
 * @see https://dev.frontapp.com/#checking-data-integrity
 * @param {object} data
 * @param {string} signature
 * @return {boolean}
 */
function isValidFrontSignature(data, signature) {
  const apiSecret = process.env.FRONT_API_SECRET;
  const hash = crypto.createHmac('sha1', apiSecret).update(JSON.stringify(data)).digest('base64');

  return hash === signature;
}

module.exports = function supportParams() {
  return (req, res, next) => {
    const body = req.body;
    logger.debug('send-message request.params', body);

    const frontSignature = req.headers['x-front-signature'];
    if (!frontSignature) {
      return next();
    }

    if (!isValidFrontSignature(body, frontSignature)) {
      return helpers.sendResponseWithStatusCode(res, 401, 'X-Front-Signature is not valid.');
    }

    // TODO: Create helpers.front and move this to its parseBody method
    req.sendMessageText = body.text;
    const recipients = body.recipients;
    const fromRecipient = recipients.find(recipient => recipient.role === 'from');
    req.agentId = fromRecipient.handle;
    const toRecipient = recipients.find(recipient => recipient.role === 'to');
    req.platformUserId = toRecipient.handle;
    req.platformMessageId = body.id;
    req.outboundTemplate = 'support';
    req.frontConversationUrl = body._links.related.conversation;

    return next();
  };
};
<commit_msg>renamed sendMessageText to messageText in send-message/params-support. This makes it easier to DRY code in the middleware.<commit_after>'use strict';

const crypto = require('crypto');
const logger = require('heroku-logger');
const helpers = require('../../helpers');

/**
 * Validates incoming Front request.
 * @see https://dev.frontapp.com/#checking-data-integrity
 * @param {object} data
 * @param {string} signature
 * @return {boolean}
 */
function isValidFrontSignature(data, signature) {
  const apiSecret = process.env.FRONT_API_SECRET;
  const hash = crypto.createHmac('sha1', apiSecret).update(JSON.stringify(data)).digest('base64');

  return hash === signature;
}

module.exports = function supportParams() {
  return (req, res, next) => {
    const body = req.body;
    logger.debug('send-message request.params', body);

    const frontSignature = req.headers['x-front-signature'];
    if (!frontSignature) {
      return next();
    }

    if (!isValidFrontSignature(body, frontSignature)) {
      return helpers.sendResponseWithStatusCode(res, 401, 'X-Front-Signature is not valid.');
    }

    // TODO: Create helpers.front and move this to its parseBody method
    req.messageText = body.text;
    const recipients = body.recipients;
    const fromRecipient = recipients.find(recipient => recipient.role === 'from');
    req.agentId = fromRecipient.handle;
    const toRecipient = recipients.find(recipient => recipient.role === 'to');
    req.platformUserId = toRecipient.handle;
    req.platformMessageId = body.id;
    req.outboundTemplate = 'support';
    req.frontConversationUrl = body._links.related.conversation;

    return next();
  };
};

{'fraction_non_alphanumeric': 0.1376430559851531, 'fraction_numerical': 0.0037117228580266005, 'mean_word_length': 4.045241809672387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3824001', 'n_tokens_mistral': 964, 'n_tokens_neox': 931, 'n_words': 225}