<commit_before>-- Copyright (C) Mashape, Inc.

local utils = require "apenode.core.utils"
require "stringy"

local _M = {}

function _M.execute()
  ngx.log(ngx.DEBUG, "Access Filter")

  local api = ngx.ctx.api

  local application_key = get_application_key(ngx.req, api)
  local application = dao.applications:get_by_key(application_key)
  if not dao.applications:is_valid(application, api) then
    utils.show_error(403, "Your authentication credentials are invalid")
  end

  ngx.ctx.authenticated_entity = application
end

function get_application_key(request, api)
  -- Let's check if the credential is in a request parameter
  if api.authentication_key_names then
    for i, authentication_key_name in ipairs(api.authentication_key_names) do
      local application_key = do_get_application_key(authentication_key_name, request, api)
      if application_key then return application_key end
    end
  end

  return nil
end


function do_get_application_key(authentication_key_name, request, api)
  local application_key = nil

  if authentication_key_name then
    -- Try to get it from the querystring
    application_key = request.get_uri_args()[authentication_key_name]
    local content_type = ngx.req.get_headers()["content-type"]
    if not application_key and content_type then -- If missing from querystring, get it from the body
      content_type = string.lower(content_type) -- Lower it for easier comparison
      if content_type == "application/x-www-form-urlencoded" or stringy.startswith(content_type, "multipart/form-data") then
        local post_args = request.get_post_args()
        if post_args then
          application_key = post_args[authentication_key_name]
        end
      elseif content_type == "application/json" then
        -- Call ngx.req.read_body to read the request body first or turn on the lua_need_request_body directive to avoid errors.
        request.read_body()
        local body_data = request.get_body_data()
        if body_data and string.len(body_data) > 0 then
          local json = cjson.decode(body_data)
          application_key = json[authentication_key_name]
        end
      end<commit_msg>read body<commit_after>-- Copyright (C) Mashape, Inc.

local utils = require "apenode.core.utils"
require "stringy"

local _M = {}

function _M.execute()
  ngx.log(ngx.DEBUG, "Access Filter")

  local api = ngx.ctx.api

  local application_key = get_application_key(ngx.req, api)
  local application = dao.applications:get_by_key(application_key)
  if not dao.applications:is_valid(application, api) then
    utils.show_error(403, "Your authentication credentials are invalid")
  end

  ngx.ctx.authenticated_entity = application
end

function get_application_key(request, api)
  -- Let's check if the credential is in a request parameter
  if api.authentication_key_names then
    for i, authentication_key_name in ipairs(api.authentication_key_names) do
      local application_key = do_get_application_key(authentication_key_name, request, api)
      if application_key then return application_key end
    end
  end

  return nil
end


function do_get_application_key(authentication_key_name, request, api)
  local application_key = nil

  if authentication_key_name then
    -- Try to get it from the querystring
    application_key = request.get_uri_args()[authentication_key_name]
    local content_type = ngx.req.get_headers()["content-type"]
    if not application_key and content_type then -- If missing from querystring, get it from the body
      content_type = string.lower(content_type) -- Lower it for easier comparison
      if content_type == "application/x-www-form-urlencoded" or stringy.startswith(content_type, "multipart/form-data") then
        request.read_body()
        local post_args = request.get_post_args()
        if post_args then
          application_key = post_args[authentication_key_name]
        end
      elseif content_type == "application/json" then
        -- Call ngx.req.read_body to read the request body first or turn on the lua_need_request_body directive to avoid errors.
        request.read_body()
        local body_data = request.get_body_data()
        if body_data and string.len(body_data) > 0 then
          local json = cjson.decode(body_data)
          application_key = json[authentication_key_name]
        end
      end
{'fraction_non_alphanumeric': 0.07218634686346863, 'fraction_numerical': 0.0018450184501845018, 'mean_word_length': 3.7869757174392937, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5882300', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1277, 'n_words': 359}