<commit_before># To set authorization for the API, configure ENV["API_AUTH_TOKEN"] and
# api_auth_token ENV["API_IP_WHITELIST"] (see secrets.yml)
class Api::BaseController < ActionController::API
  API_AUTH_TOKEN = Rails.application.secrets[:api_auth_token].freeze

  if Rails.application.secrets[:api_ip_whitelist]
    API_IP_WHITELIST = Rails.application.secrets[:api_ip_whitelist].split(",").map { |ip_cidr| IPAddr.new(ip_cidr) }.freeze
  else
    API_IP_WHITELIST = [].freeze
  end

  include ActionController::HttpAuthentication::Token::ControllerMethods

  before_action :authenticate

  private

  # before_action filter to protect API controller actions.
  def authenticate
    (authenticate_ip && authenticate_token) || render_unauthorized
  end

  def authenticate_ip
    return true if API_IP_WHITELIST.blank?
    API_IP_WHITELIST.any? { |ip_addr| ip_addr.include?(request.remote_ip) }
  end

  def authenticate_token
    return true if API_AUTH_TOKEN.blank?
    authenticate_with_http_token do |token, _options|
      # Compare the tokens in a time-constant manner, to mitigate timing attacks.
      ActiveSupport::SecurityUtils.secure_compare(
        ::Digest::SHA256.hexdigest(token),
        ::Digest::SHA256.hexdigest(API_AUTH_TOKEN)
      )
    end
  end

  def render_unauthorized
    render(json: { message: "authentication failed 🎷" }, status: 403)
  end
end
<commit_msg>For API IP whitelist, read from Fastly-Client-IP header<commit_after># To set authorization for the API, configure ENV["API_AUTH_TOKEN"] and
# api_auth_token ENV["API_IP_WHITELIST"] (see secrets.yml)
class Api::BaseController < ActionController::API
  API_AUTH_TOKEN = Rails.application.secrets[:api_auth_token].freeze

  if Rails.application.secrets[:api_ip_whitelist]
    API_IP_WHITELIST = Rails.application.secrets[:api_ip_whitelist].split(",").map { |ip_cidr| IPAddr.new(ip_cidr) }.freeze
  else
    API_IP_WHITELIST = [].freeze
  end

  include ActionController::HttpAuthentication::Token::ControllerMethods

  before_action :authenticate

  private

  # before_action filter to protect API controller actions.
  def authenticate
    (authenticate_ip && authenticate_token) || render_unauthorized
  end

  def authenticate_ip
    return true if API_IP_WHITELIST.blank?
    # Set this in Fastly.
    remote_ip = request.headers["Fastly-Client-IP"].presence || request.remote_ip
    API_IP_WHITELIST.any? { |ip_addr| ip_addr.include?(remote_ip) }
  end

  def authenticate_token
    return true if API_AUTH_TOKEN.blank?
    authenticate_with_http_token do |token, _options|
      # Compare the tokens in a time-constant manner, to mitigate timing attacks.
      ActiveSupport::SecurityUtils.secure_compare(
        ::Digest::SHA256.hexdigest(token),
        ::Digest::SHA256.hexdigest(API_AUTH_TOKEN)
      )
    end
  end

  def render_unauthorized
    render(json: { message: "authentication failed 🎷" }, status: 403)
  end
end

{'fraction_non_alphanumeric': 0.09227614490772386, 'fraction_numerical': 0.006151742993848257, 'mean_word_length': 4.618042226487524, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '234626', 'n_tokens_mistral': 1000, 'n_tokens_neox': 944, 'n_words': 204}