<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var P = require('./promise')
var Pool = require('./pool')

module.exports = function (log, error) {

  // Perform a deep clone of payload and remove user password.
  function sanitizePayload(payload) {
    // Once we move to Node4, use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
    var clonePayload = JSON.parse(JSON.stringify(payload))

    if (clonePayload.authPW) {
      delete clonePayload.authPW
    }

    return clonePayload
  }

  function Customs(url) {
    if (url === 'none') {
      this.pool = {
        post: function () { return P.resolve({ block: false })},
        close: function () {}
      }
    }
    else {
      this.pool = new Pool(url, { timeout: 1000 })
    }
  }

  Customs.prototype.check = function (request, email, action) {
    log.trace({ op: 'customs.check', email: email, action: action })
    return this.pool.post(
      '/check',
      {
        ip: request.app.clientAddress,
        email: email,
        action: action,
        headers: request.headers,
        query: request.query,
        payload: sanitizePayload(request.payload)
      }
    )
    .then(
      function (result) {
        if (result.block) {
          if (result.retryAfter) {
            throw error.tooManyRequests(result.retryAfter)
          }
          throw error.requestBlocked()
        }
      },
      function (err) {
        log.error({ op: 'customs.check.1', email: email, action: action, err: err })
        // If this happens, either:
        // - (1) the url in config doesn't point to a real customs server
        // - (2) the customs server returned an internal server error
        // Either way, allow the request through so we fail open.
      }
    )
  }

  Customs.prototype.flag = function (ip, info) {
    var email = info.email
    var errno = info.errno || error.ERRNO.UNEXPECTED_ERROR
    log.trace({ op: 'customs.flag', ip: ip, email: email, errno: errno })
    return this.pool.post(
      '/failedLoginAttempt',
      {
        ip: ip,
        email: email,
        errno: errno
      }
    )
    .then(
      function (result) {
        return
      },
      function (err) {
        log.error({ op: 'customs.flag.1', email: email, err: err })
        // If this happens, either:
        // - (1) the url in config doesn't point to a real customs server
        // - (2) the customs server returned an internal server error
        // Either way, allow the request through so we fail open.
        return
      }
    )
  }

  Customs.prototype.reset = function (email) {
    log.trace({ op: 'customs.reset', email: email })
    return this.pool.post(<commit_msg>fix(server): Return undefined from Customs.prototype.flag if everyting is OK<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var P = require('./promise')
var Pool = require('./pool')

module.exports = function (log, error) {

  // Perform a deep clone of payload and remove user password.
  function sanitizePayload(payload) {
    // Once we move to Node4, use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
    var clonePayload = JSON.parse(JSON.stringify(payload))

    if (clonePayload.authPW) {
      delete clonePayload.authPW
    }

    return clonePayload
  }

  function Customs(url) {
    if (url === 'none') {
      this.pool = {
        post: function () { return P.resolve({ block: false })},
        close: function () {}
      }
    }
    else {
      this.pool = new Pool(url, { timeout: 1000 })
    }
  }

  Customs.prototype.check = function (request, email, action) {
    log.trace({ op: 'customs.check', email: email, action: action })
    return this.pool.post(
      '/check',
      {
        ip: request.app.clientAddress,
        email: email,
        action: action,
        headers: request.headers,
        query: request.query,
        payload: sanitizePayload(request.payload)
      }
    )
    .then(
      function (result) {
        if (result.block) {
          if (result.retryAfter) {
            throw error.tooManyRequests(result.retryAfter)
          }
          throw error.requestBlocked()
        }
      },
      function (err) {
        log.error({ op: 'customs.check.1', email: email, action: action, err: err })
        // If this happens, either:
        // - (1) the url in config doesn't point to a real customs server
        // - (2) the customs server returned an internal server error
        // Either way, allow the request through so we fail open.
      }
    )
  }

  Customs.prototype.flag = function (ip, info) {
    var email = info.email
    var errno = info.errno || error.ERRNO.UNEXPECTED_ERROR
    log.trace({ op: 'customs.flag', ip: ip, email: email, errno: errno })
    return this.pool.post(
      '/failedLoginAttempt',
      {
        ip: ip,
        email: email,
        errno: errno
      }
    )
    .then(
      function () {},
      function (err) {
        log.error({ op: 'customs.flag.1', email: email, err: err })
        // If this happens, either:
        // - (1) the url in config doesn't point to a real customs server
        // - (2) the customs server returned an internal server error
        // Either way, allow the request through so we fail open.
      }
    )
  }

  Customs.prototype.reset = function (email) {
    log.trace({ op: 'customs.reset', email: email })
    return this.pool.post(
{'fraction_non_alphanumeric': 0.12158422691110342, 'fraction_numerical': 0.005188516084399862, 'mean_word_length': 2.153217011995638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5166124', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1686, 'n_words': 534}