<commit_before>
#
# tune authentication at will here
#

#
# a wrapper for handling our authentication options
# 
class OpenWFE::RackAuth
  
  def initialize( rack_app )
    @rack_app = rack_app
    
    @config = YAML::load_file( 
      File.join( File.dirname(__FILE__), 'authentication.yaml' ) 
    )[$env]
    
    raise ArgumentError, "No configuration specified for #{$env} environment!" if @config.nil?
  end
  
  #
  # authenticate the request against all our enable authentication options
  def call( env )
    if @config['enabled']
      authenticate_by_ip(env) || authenticate_by_password
    end
    
    @rack_app.call( env )
  end
  
  def authenticate_by_ip(env)
    if @config['whitelisting']
      auth_passed = @config['allowed_hosts'].include?(env['REMOTE_ADDR'])
    end
    
    if !auth_passed && !@config['basic_auth']
      throw :done, [ 401, "get off !" ]
    end
    auth_passed
  end
  
  def authenticate_by_password
    @rack_app = Rack::Auth::Basic.new(@rack_app) do |username, password|
      @config['users'][username] && @config['users'][username] == password
    end
    @rack_app.realm = @config['basic_auth_realm']
  end
  
  # Is this sane?!? It gets the tests to run and I'm not familiar with Rack...
  def method_missing( method_name, *args )
    @rack_app.send( method_name, *args )
  end
end

# Only continue if allowed
$app = OpenWFE::RackAuth.new( $app )

# ($app always pointing to 'top' app, while $rr points to ruote-rest itself)
# (will certainly change)

<commit_msg>whitelisting and/or basic_auth, take 2<commit_after>
require 'rack/auth/basic'

#
# TODO : maybe move the two auth classes to lib/
#        (and leave the real config stuff here)
#

#
# a wrapper for handling our authentication options
#
class OpenWFE::RackWhiteListing

  def initialize (rack_app)

    @rack_app = rack_app
  end

  #
  # authenticate the request against all our enable authentication options
  #
  def call (env)

    unless env['RUOTE_AUTHENTICATED']

      return [ 401, {}, 'get off !' ] \
        unless AUTH_CONF['allowed_hosts'].include?(env['REMOTE_ADDR'])

      env['RUOTE_AUTHENTICATED'] = true
    end

    @rack_app.call(env)
  end

  #def authenticate_by_password
  #  @rack_app = Rack::Auth::Basic.new(@rack_app) do |username, password|
  #    @config['users'][username] && @config['users'][username] == password
  #  end
  #  @rack_app.realm = @config['basic_auth_realm']
  #end
end

class OpenWFE::RackBasicAuth < Rack::Auth::Basic

  def call (env)

    unless env['RUOTE_AUTHENTICATED']

      auth = Rack::Auth::Basic::Request.new(env)

      return unauthorized unless auth.provided?
      return bad_request unless auth.basic?
      return unauthorized unless valid?(auth)

      env['REMOTE_USER'] = auth.username
      env['RUOTE_AUTHENTICATED'] = true
    end

    @app.call(env)
  end
end


#
# the actual configuration job

AUTH_CONF = YAML::load_file(
  File.join( File.dirname(__FILE__), 'authentication.yaml' )
)[$env]

raise(
  ArgumentError, "No configuration specified for #{$env} environment!"
) if AUTH_CONF.nil?


if AUTH_CONF['enabled']
  $app = OpenWFE::RackWhiteListing.new($app) if AUTH_CONF['whitelisting']
  $app = OpenWFE::RackBasicAuth.new($app) if AUTH_CONF['basic_auth']
end

# ($app always pointing to 'top' app, while $rr points to ruote-rest itself)
# (will certainly change)


{'fraction_non_alphanumeric': 0.12086930634117297, 'fraction_numerical': 0.0020839535576064306, 'mean_word_length': 3.67966573816156, 'pattern_counts': {'":': 0, '<': 4, '<?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': '1815990', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1101, 'n_words': 289}