<commit_before>require 'net/ldap'
require 'yaml'

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details
  filter_parameter_logging :password
  before_filter :authenticate

  protected

  def conf
    @@conf = YAML.load_file "#{Rails.root}/config/deezy.yml"
  end

  def authenticate
    is_mundo_or_local? or can_bind_as_help?
  end

  def can_bind_as_help?
    authenticate_or_request_with_http_basic do |username, password|

      # only help can authenticate, and you still have to type it
      ldconf = conf['auth']['ldap']
      if username == ldap['username']
        ldap = Net::LDAP.new
        ldap.host = ldconf['host']
        ldap.port = ldconf['port'] if ldconf['port']
        ldap.auth(
          ldconf['bind_dn'] % ldconf['username'],
          ldconf['password']
        )
        ldap.bind 
      end
    end
  end

  def is_mundo_or_local?
    conf['auth']['exempt_ips'].include? request.remote_ip 
  end
end<commit_msg>clean up some authentication stuff<commit_after>require 'net/ldap'
require 'yaml'

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details
  filter_parameter_logging :password
  before_filter :authenticate

  protected

  def conf
    @@conf = YAML.load_file "#{Rails.root}/config/deezy.yml"
  end

  def authenticate
    is_exempt? or can_bind_as_help?
  end

  def can_bind_as_help?

    # use authentication if it's configured
    if conf['auth']
      authenticate_or_request_with_http_basic do |username, password|

        # only help can authenticate, and you still have to type it
        ldconf = conf['auth']['ldap']
        if username == ldconf['username']
          ldap = Net::LDAP.new
          ldap.host = ldconf['host']
          ldap.port = ldconf['port'] if ldconf['port']
          ldap.auth(
            ldconf['bind_dn'] % ldconf['username'],
            ldconf['password']
          )
          ldap.bind 
        end
      end
    end
  end

  def is_exempt?
    if conf['auth'] && conf['auth']['exempt_ips']
      conf['auth']['exempt_ips'].include? request.remote_ip 
    else
      true
    end
  end
end
{'fraction_non_alphanumeric': 0.09845890410958905, 'fraction_numerical': 0.0, 'mean_word_length': 2.9880546075085324, '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': '4029544', 'n_tokens_mistral': 763, 'n_tokens_neox': 713, 'n_words': 179}