<commit_before>local cjson = require('cjson')
local http = require('resty.http')
local jwt = require('resty.jwt')
local validators = require('resty.jwt-validators')

local appHref = os.getenv('AUTH0_ACCOUNT_DOMAIN')
local clientId = os.getenv('AUTH0_CLIENT_ID')
local clientSecret = os.getenv('AUTH0_CLIENT_SECRET')
local connection = os.getenv('AUTH0_CLIENT_CONNECTION')
local whitelistDomains = os.getenv('AUTH0_WHITELIST_DOMAINS')

assert(clientId ~= nil, 'Environment variable AUTH0_CLIENT_ID not set')
assert(clientSecret ~= nil, 'Environment variable AUTH0_CLIENT_SECRET not set')

local M = {}
local Helpers = {}

function M.getAccount(secret, aud, applicationHref)
  applicationHref = applicationHref or appHref
  getAccount(false, secret, aud, applicationHref)
end

function M.requireAccount(secret, aud, applicationHref)
  applicationHref = applicationHref or appHref
  getAccount(true, secret, aud, applicationHref)
end

function getAccount(required, secret, audience, applicationHref)
  local jwtString = Helpers.getBearerToken()
  local auth = jwt:load_jwt(jwtString)

  if not jwtString or not auth.valid then
    return Helpers.exit(required)
  end

  local claimSpec = {
    exp = validators.required(validators.opt_is_not_expired()),
    iss = validators.required(validators.opt_equals(applicationHref)),
    aud = validators.required(validators.opt_equals(audience))
  }
  if type(auth.payload.aud) == 'table' then
    claimSpec.aud = validators.required(
      validators.opt_check(audience, Helpers.checkAud, 'check_aud', 'table'))
  end

  local checkedJwt = jwt:verify(secret, jwtString, claimSpec)
  if not (checkedJwt.verified and checkedJwt.header.alg == 'HS256') then
    return Helpers.exit(required)
  end

  local accountToken = Helpers.getAccountToken()
  local account = jwt:load_jwt(accountToken).payload
  if account then
    ngx.req.set_header('x-auth0-account', cjson.encode(account))
  end
end

function M.changePassword(applicationHref)
  applicationHref = applicationHref or appHref
  ngx.req.read_body()

  local httpc = http.new()
  local headers = ngx.req.get_headers()
  local body = cjson.decode(ngx.req.get_body_data())

  -- Add clientId and connection

  body['client_id'] = clientId
  body['connection'] = connection

  -- Build and send the request

  local request = Helpers.buildRequest(headers, body)
  local res, err = httpc:request_uri(applicationHref .. 'dbconnections/change_password', request)
  if not res or res.status >= 500 then
    return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
  end

  -- Finish the request

  local response = cjson.decode(res.body)
  Helpers.finish(res, response)
end

function M.signup(applicationHref)
  applicationHref = applicationHref or appHref
  ngx.req.read_body()

  local httpc = http.new()
  local headers = ngx.req.get_headers()
  local body = cjson.decode(ngx.req.get_body_data())

  -- Add clientId and connection

  body['client_id'] = clientId
  body['connection'] = connection

  -- Validate email being registered

  local email = body['email']
  Helpers.checkDomainWhitelist(email)

  -- Build and send the request

  local request = Helpers.buildRequest(headers, body)
  local res, err = httpc:request_uri(applicationHref .. 'dbconnections/signup', request)<commit_msg>fix(change password): Fix bug found after implementing test<commit_after>local cjson = require('cjson')
local http = require('resty.http')
local jwt = require('resty.jwt')
local validators = require('resty.jwt-validators')

local appHref = os.getenv('AUTH0_ACCOUNT_DOMAIN')
local clientId = os.getenv('AUTH0_CLIENT_ID')
local clientSecret = os.getenv('AUTH0_CLIENT_SECRET')
local connection = os.getenv('AUTH0_CLIENT_CONNECTION')
local whitelistDomains = os.getenv('AUTH0_WHITELIST_DOMAINS')

assert(clientId ~= nil, 'Environment variable AUTH0_CLIENT_ID not set')
assert(clientSecret ~= nil, 'Environment variable AUTH0_CLIENT_SECRET not set')

local M = {}
local Helpers = {}

function M.getAccount(secret, aud, applicationHref)
  applicationHref = applicationHref or appHref
  getAccount(false, secret, aud, applicationHref)
end

function M.requireAccount(secret, aud, applicationHref)
  applicationHref = applicationHref or appHref
  getAccount(true, secret, aud, applicationHref)
end

function getAccount(required, secret, audience, applicationHref)
  local jwtString = Helpers.getBearerToken()
  local auth = jwt:load_jwt(jwtString)

  if not jwtString or not auth.valid then
    return Helpers.exit(required)
  end

  local claimSpec = {
    exp = validators.required(validators.opt_is_not_expired()),
    iss = validators.required(validators.opt_equals(applicationHref)),
    aud = validators.required(validators.opt_equals(audience))
  }
  if type(auth.payload.aud) == 'table' then
    claimSpec.aud = validators.required(
      validators.opt_check(audience, Helpers.checkAud, 'check_aud', 'table'))
  end

  local checkedJwt = jwt:verify(secret, jwtString, claimSpec)
  if not (checkedJwt.verified and checkedJwt.header.alg == 'HS256') then
    return Helpers.exit(required)
  end

  local accountToken = Helpers.getAccountToken()
  local account = jwt:load_jwt(accountToken).payload
  if account then
    ngx.req.set_header('x-auth0-account', cjson.encode(account))
  end
end

function M.changePassword(applicationHref)
  applicationHref = applicationHref or appHref
  ngx.req.read_body()

  local httpc = http.new()
  local headers = ngx.req.get_headers()
  local body = cjson.decode(ngx.req.get_body_data())

  -- Add clientId and connection

  body['client_id'] = clientId
  body['connection'] = connection

  -- Build and send the request

  local request = Helpers.buildRequest(headers, body)
  local res, err = httpc:request_uri(applicationHref .. 'dbconnections/change_password', request)
  if not res or res.status >= 500 then
    return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
  end

  -- Finish the request

  local response = res.body
  Helpers.finish(res, response)
end

function M.signup(applicationHref)
  applicationHref = applicationHref or appHref
  ngx.req.read_body()

  local httpc = http.new()
  local headers = ngx.req.get_headers()
  local body = cjson.decode(ngx.req.get_body_data())

  -- Add clientId and connection

  body['client_id'] = clientId
  body['connection'] = connection

  -- Validate email being registered

  local email = body['email']
  Helpers.checkDomainWhitelist(email)

  -- Build and send the request

  local request = Helpers.buildRequest(headers, body)
  local res, err = httpc:request_uri(applicationHref .. 'dbconnections/signup', request)
{'fraction_non_alphanumeric': 0.10431873479318735, 'fraction_numerical': 0.004257907542579075, 'mean_word_length': 4.995442114858705, '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': '5539585', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2101, 'n_words': 494}