<commit_before>import auth from 'basic-auth'
import Q from 'q'
import logger from 'winston'
import crypto from 'crypto'
import bcrypt from 'bcryptjs'
import SDC from 'statsd-client'
import os from 'os'
import { ClientModel } from '../model/clients'
import { config } from '../config'

const statsdServer = config.get('statsd')
const application = config.get('application')

const domain = `${os.hostname()}.${application.name}.appMetrics`
const sdc = new SDC(statsdServer)

const bcryptCompare = (pass, client, callback) => bcrypt.compare(pass, client.passwordHash, callback)

function cryptoCompare (pass, client, callback) {
  const hash = crypto.createHash(client.passwordAlgorithm)
  hash.update(pass)
  hash.update(client.passwordSalt)
  if (hash.digest('hex') === client.passwordHash) {
    return callback(null, true)
  } else {
    return callback(null, false)
  }
}

function comparePasswordWithClientHash (pass, client, callback) {
  if (Array.from(crypto.getHashes()).includes(client.passwordAlgorithm)) {
    return cryptoCompare(pass, client, callback)
  } else {
    return bcryptCompare(pass, client, callback)
  }
}

export function authenticateUser (ctx, done) {
  const user = auth(ctx)
<commit_msg>Add fix to accommodate breaking change from basic-auth package upgrade<commit_after>import auth from 'basic-auth'
import Q from 'q'
import logger from 'winston'
import crypto from 'crypto'
import bcrypt from 'bcryptjs'
import SDC from 'statsd-client'
import os from 'os'
import { ClientModel } from '../model/clients'
import { config } from '../config'

const statsdServer = config.get('statsd')
const application = config.get('application')

const domain = `${os.hostname()}.${application.name}.appMetrics`
const sdc = new SDC(statsdServer)

const bcryptCompare = (pass, client, callback) => bcrypt.compare(pass, client.passwordHash, callback)

function cryptoCompare (pass, client, callback) {
  const hash = crypto.createHash(client.passwordAlgorithm)
  hash.update(pass)
  hash.update(client.passwordSalt)
  if (hash.digest('hex') === client.passwordHash) {
    return callback(null, true)
  } else {
    return callback(null, false)
  }
}

function comparePasswordWithClientHash (pass, client, callback) {
  if (Array.from(crypto.getHashes()).includes(client.passwordAlgorithm)) {
    return cryptoCompare(pass, client, callback)
  } else {
    return bcryptCompare(pass, client, callback)
  }
}

export function authenticateUser (ctx, done) {
  const user = auth(ctx.req)

{'fraction_non_alphanumeric': 0.12344689378757515, 'fraction_numerical': 0.0, 'mean_word_length': 5.147783251231527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '351410', 'n_tokens_mistral': 744, 'n_tokens_neox': 739, 'n_words': 211}