<commit_before>const Errors = require('common-errors');
const validator = require('../validator.js');
const proxyaddr = require('proxy-addr');
const { getRoute, getTimeout, get: getConfig } = require('../config.js');

exports.post = {
  path: '/restore',
  handlers: {
    '1.0.0': function requestReset(req, res, next) {
      const { log, amqp } = req;
      const config = getConfig();
      const ROUTE_NAME = 'requestPassword';

      log.debug('requesting to restore a password');

      return validator.filter(ROUTE_NAME, req.body)
        .then(function attemptToRegister(body) {
          const message = {
            type: 'email',
            username: body.data.id,
            remoteip: proxyaddr(req, config.trustProxy),
          };

          return amqp
            .publishAndWait(getRoute(ROUTE_NAME), message, { timeout: getTimeout(ROUTE_NAME) })
            .then(() => {
              res.send(202);
            });
        })
        .asCallback(next);
    },
  },
};

exports.patch = {
  path: '/reset',
  handlers: {
    '1.0.0': function validateReset(req, res, next) {
      const { log, amqp } = req;
      const config = getConfig();
      const ROUTE_NAME = 'updatePassword';

      log.debug('requesting to restore a password');

      return validator.filter('resetPassword', req.body)
        .then(function attemptToRegister(body) {
          const attr = body.data.attributes;
          if (attr.password !== <PASSWORD>) {
            throw new Errors.ValidationError('data.attributes.password must match data.attributes.passwordRepeat', 400);
          }

          const message = {
            newPassword: <PASSWORD>,
            remoteip: proxyaddr(req, config.trustProxy),
          };

          if (attr.token) {
            message.resetToken = attr.token;
          } else if (req.user) {
            message.currentPassword = <PASSWORD>;
            message.username = req.user.id;
          } else {
            throw new Errors.HttpStatusError(401, 'authentication is required');
          }

          return amqp
            .publishAndWait(getRoute(ROUTE_NAME), message, { timeout: getTimeout(ROUTE_NAME) })
            .then(() => {
              res.send(204);
            });
        })
        .asCallback(next);
    },
  },
};<commit_msg>Add auth check for passwords<commit_after>const Errors = require('common-errors');
const validator = require('../validator.js');
const proxyaddr = require('proxy-addr');
const auth = Promise.promisify(require('../middleware/auth.js'));
const { getRoute, getTimeout, get: getConfig } = require('../config.js');

exports.post = {
  path: '/restore',
  handlers: {
    '1.0.0': function requestReset(req, res, next) {
      const { log, amqp } = req;
      const config = getConfig();
      const ROUTE_NAME = 'requestPassword';

      log.debug('requesting to restore a password');

      return validator.filter(ROUTE_NAME, req.body)
        .then(function attemptToRegister(body) {
          const message = {
            type: 'email',
            username: body.data.id,
            remoteip: proxyaddr(req, config.trustProxy),
          };

          return amqp
            .publishAndWait(getRoute(ROUTE_NAME), message, { timeout: getTimeout(ROUTE_NAME) })
            .then(() => {
              res.send(202);
            });
        })
        .asCallback(next);
    },
  },
};

exports.patch = {
  path: '/reset',
  handlers: {
    '1.0.0': function validateReset(req, res, next) {
      const { log, amqp } = req;
      const config = getConfig();
      const ROUTE_NAME = 'updatePassword';

      log.debug('requesting to restore a password');

      return validator.filter('resetPassword', req.body)
        .then(function attemptToRegister(body) {
          const attr = body.data.attributes;
          if (attr.password !== attr.password<PASSWORD>) {
            throw new Errors.ValidationError('data.attributes.password must match data.attributes.passwordRepeat', 400);
          }

          const message = {
            newPassword: attr.password,
            remoteip: proxyaddr(req, config.trustProxy),
          };

          if (attr.token) {
            message.resetToken = attr.token;
            return message;
          }

          return auth(req, res).then(function userIsAuthenticated() {
            message.currentPassword = <PASSWORD>;
            message.username = req.user.id;
            return message;
          });
        })
        .then(function updateBackend(message) {
          return amqp
            .publishAndWait(getRoute(ROUTE_NAME), message, { timeout: getTimeout(ROUTE_NAME) })
            .then(() => {
              res.send(204);
            });
        })
        .asCallback(next);
    },
  },
};
{'fraction_non_alphanumeric': 0.13375394321766562, 'fraction_numerical': 0.00694006309148265, 'mean_word_length': 2.074337427278604, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 0, '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': '1845254', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1340, 'n_words': 283}