<commit_before>'use strict';
var BBPromise = require('bluebird');
var xml2js = require('xml2js');
var OAuth = require('oauth').OAuth;
var crypto = require('crypto');
var _ = require('lodash');
var logger = require('hoist-logger').child({
  cls: 'XeroConnector'
});
var path = require('path');
var authConfigs = require(path.resolve(__dirname, './auth_config.json'));
var keyPairGenerator = require('./key_pair_generator');
var certificateStore = require('hoist-certificate-store');
var http = require('http');
var https = require('https');
var moment = require('moment');


var payrollEndpoints =
  ["Employees",
    "LeaveApplications",
    "PayItems",
    "PayrollCalendars",
    "PayRuns",
    "Payslip",
    "Settings",
    "SuperFunds",
    "SuperFundProducts",
    "Timesheets"
  ];

function XeroConnector(settings) {
  logger.debug({
    settings: settings,
    authConfigs: authConfigs
  }, 'constructed xero-connector');
  this.settings = settings;
  this.parser = BBPromise.promisifyAll(new xml2js.Parser({
    explicitArray: false,
    ignoreAttrs: false,
    async: true
  }));

  var authConfig = authConfigs[settings.authType];
  this.authConfig = authConfig;

  if (authConfig.accessTokenUrl && settings.payroll === true) {
    authConfig.accessTokenUrl = authConfig.accessTokenUrl + "?scope=payroll." + payrollEndpoints.join(',payroll.');
  }

  this.auth = BBPromise.promisifyAll(new OAuth(
    authConfig.requestTokenUrl || null,
    authConfig.accessTokenUrl || null,
    settings.consumerKey,
    settings.consumerSecret,
    '1.0A',
    null,
    authConfig.encryptionType || 'RSA-SHA1'
  ));
  if (settings.authType === 'Partner') {
    this.auth._createClient = _.bind(function (port, hostname, method, path, headers, sslEnabled) {
      headers = headers || {};
      headers['user-agent'] = 'Hoist Apps: ' + this.settings.applicationName;
      var certificates = certificateStore.get('entrust');
      var options = {
        host: hostname,
        port: port,
        path: path,
        method: method,
        headers: headers,
        cert: certificates.cert,
        key: certificates.key
      };
      var httpModel;
      if (sslEnabled) {
        httpModel = https;
      } else {
        httpModel = http;
      }
      return httpModel.request(options);
    }, this);
  }
  /*jshint camelcase: false */
  this.auth._authorize_callback = 'https://bouncer.hoist.io/bounce';
  if (authConfig.requiresSigning) {
    this.auth._signatureMethod = authConfig.signatureMethod;
    this.auth._createSignature = _.bind(function createXeroSignature(signatureBase) {

      logger.debug({
        signatureBase: signatureBase,
        settings: this.settings
      }, 'authorization signature');
      return crypto.createSign(authConfig.signatureMethod)
        .update(signatureBase)
        .sign(this.settings.privateKey, "base64");
    }, this);
  }
  this.auth._performSecureRequestAsync = BBPromise.promisify(this.auth._performSecureRequest, this.auth);
  if (this.settings.authType === 'Private') {
    this.settings.accessKey = this.settings.consumerKey;
    this.settings.accessSecret = this.settings.consumerSecret;
  }
  _.bindAll(this);
}

XeroConnector.defaultSettings = function (authType) {
  if (authType === 'Private' || authType === 'Partner') {
    return keyPairGenerator.generate({
        stamp: Date.now()
      })
      .then(function (keys) {
        return {
          publicKey: keys.public,
          privateKey: keys.private<commit_msg>change signature log level<commit_after>'use strict';
var BBPromise = require('bluebird');
var xml2js = require('xml2js');
var OAuth = require('oauth').OAuth;
var crypto = require('crypto');
var _ = require('lodash');
var logger = require('hoist-logger').child({
  cls: 'XeroConnector'
});
var path = require('path');
var authConfigs = require(path.resolve(__dirname, './auth_config.json'));
var keyPairGenerator = require('./key_pair_generator');
var certificateStore = require('hoist-certificate-store');
var http = require('http');
var https = require('https');
var moment = require('moment');


var payrollEndpoints =
  ["Employees",
    "LeaveApplications",
    "PayItems",
    "PayrollCalendars",
    "PayRuns",
    "Payslip",
    "Settings",
    "SuperFunds",
    "SuperFundProducts",
    "Timesheets"
  ];

function XeroConnector(settings) {
  logger.debug({
    settings: settings,
    authConfigs: authConfigs
  }, 'constructed xero-connector');
  this.settings = settings;
  this.parser = BBPromise.promisifyAll(new xml2js.Parser({
    explicitArray: false,
    ignoreAttrs: false,
    async: true
  }));

  var authConfig = authConfigs[settings.authType];
  this.authConfig = authConfig;

  if (authConfig.accessTokenUrl && settings.payroll === true) {
    authConfig.accessTokenUrl = authConfig.accessTokenUrl + "?scope=payroll." + payrollEndpoints.join(',payroll.');
  }

  this.auth = BBPromise.promisifyAll(new OAuth(
    authConfig.requestTokenUrl || null,
    authConfig.accessTokenUrl || null,
    settings.consumerKey,
    settings.consumerSecret,
    '1.0A',
    null,
    authConfig.encryptionType || 'RSA-SHA1'
  ));
  if (settings.authType === 'Partner') {
    this.auth._createClient = _.bind(function (port, hostname, method, path, headers, sslEnabled) {
      headers = headers || {};
      headers['user-agent'] = 'Hoist Apps: ' + this.settings.applicationName;
      var certificates = certificateStore.get('entrust');
      var options = {
        host: hostname,
        port: port,
        path: path,
        method: method,
        headers: headers,
        cert: certificates.cert,
        key: certificates.key
      };
      var httpModel;
      if (sslEnabled) {
        httpModel = https;
      } else {
        httpModel = http;
      }
      return httpModel.request(options);
    }, this);
  }
  /*jshint camelcase: false */
  this.auth._authorize_callback = 'https://bouncer.hoist.io/bounce';
  if (authConfig.requiresSigning) {
    this.auth._signatureMethod = authConfig.signatureMethod;
    this.auth._createSignature = _.bind(function createXeroSignature(signatureBase) {

      logger.info({
        signatureBase: signatureBase,
        settings: this.settings
      }, 'authorization signature');
      return crypto.createSign(authConfig.signatureMethod)
        .update(signatureBase)
        .sign(this.settings.privateKey, "base64");
    }, this);
  }
  this.auth._performSecureRequestAsync = BBPromise.promisify(this.auth._performSecureRequest, this.auth);
  if (this.settings.authType === 'Private') {
    this.settings.accessKey = this.settings.consumerKey;
    this.settings.accessSecret = this.settings.consumerSecret;
  }
  _.bindAll(this);
}

XeroConnector.defaultSettings = function (authType) {
  if (authType === 'Private' || authType === 'Partner') {
    return keyPairGenerator.generate({
        stamp: Date.now()
      })
      .then(function (keys) {
        return {
          publicKey: keys.public,
          privateKey: keys.private
{'fraction_non_alphanumeric': 0.13575307230637326, 'fraction_numerical': 0.002286367533581023, 'mean_word_length': 3.435361216730038, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '300098', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2109, 'n_words': 410}