<commit_before>module Rails
  module Auth
    # Configures Rails::Auth middleware for use in a Rails application
    module ConfigBuilder
      extend self

      # Application-level configuration (i.e. config/application.rb)
      def application(config, acl_file: Rails.root.join("config/acl.yml"), matchers: {})
        config.x.rails_auth.acl = Rails::Auth::ACL.from_yaml(
          File.read(acl_file.to_s),
          matchers: matchers
        )

        config.middleware.use Rails::Auth::ACL::Middleware, acl: config.x.acl
      end

      # Development configuration (i.e. config/environments/development.rb)
      def development(config, development_credentials: {}, error_page: :debug)
        error_page_middleware(config, error_page)
        credential_injector_middleware(config, development_credentials) unless development_credentials.empty?
      end

      # Test configuration (i.e. config/environments/test.rb)
      def test(config)
        # Simulated credentials to be injected with InjectorMiddleware
        credential_injector_middleware(config, config.x.rails_auth.test_credentials ||= {})
      end

      def production(
        config,
        cert_filters: nil,
        require_cert: false,
        ca_file: nil,
        error_page: Rails.root.join("public/403.html"),
        monitor: nil
      )
        raise ArgumentError, "no cert_filters given but require_cert is true" if require_cert && !cert_filters
        raise ArgumentError, "no ca_file given but cert_filters were set"     if cert_filters && !ca_file

        error_page_middleware(config, error_page)

        if cert_filters
          config.middleware.insert_before Rails::Auth::ACL::Middleware,
                                          Rails::Auth::X509::Middleware,
                                          require_cert: require_cert,
                                          cert_filters: cert_filters,
                                          ca_file:      ca_file,
                                          logger:       Rails.logger
        end

        return unless monitor
        config.middleware.insert_before Rails::Auth::ACL::Middleware,
                                        Rails::Auth::Monitor::Middleware,
                                        monitor
      end

      private

      # Adds error page middleware to the chain
      def error_page_middleware(config, error_page)
        case error_page
        when :debug
          config.middleware.insert_before Rails::Auth::ACL::Middleware,
                                          Rails::Auth::ErrorPage::DebugMiddleware,
                                          acl: config.x.rails_auth.acl
        when Pathname, String
          config.middleware.insert_before Rails::Auth::ACL::Middleware,
                                          Rails::Auth::ErrorPage::Middleware,
                                          page_body: Pathname(error_page).read
        when FalseClass, NilClass
        else raise TypeError, "bad error page mode: #{mode.inspect}"
        end
      end

      # Adds Rails::Auth::Credentials::InjectorMiddleware to the chain with the given credentials
      def credential_injector_middleware(config, credentials)
        config.middleware.insert_before Rails::Auth::ACL::Middleware,
                                        Rails::Auth::Credentials::InjectorMiddleware,
                                        credentials
      end
    end
  end
end
<commit_msg>Update config_builder.rb<commit_after>module Rails
  module Auth
    # Configures Rails::Auth middleware for use in a Rails application
    module ConfigBuilder
      extend self

      # Application-level configuration (i.e. config/application.rb)
      def application(config, acl_file: Rails.root.join("config/acl.yml"), matchers: {})
        config.x.rails_auth.acl = Rails::Auth::ACL.from_yaml(
          File.read(acl_file.to_s),
          matchers: matchers
        )

        config.middleware.use Rails::Auth::ACL::Middleware, acl: config.x.rails_auth.acl
      end

      # Development configuration (i.e. config/environments/development.rb)
      def development(config, development_credentials: {}, error_page: :debug)
        error_page_middleware(config, error_page)
        credential_injector_middleware(config, development_credentials) unless development_credentials.empty?
      end

      # Test configuration (i.e. config/environments/test.rb)
      def test(config)
        # Simulated credentials to be injected with InjectorMiddleware
        credential_injector_middleware(config, config.x.rails_auth.test_credentials ||= {})
      end

      def production(
        config,
        cert_filters: nil,
        require_cert: false,
        ca_file: nil,
        error_page: Rails.root.join("public/403.html"),
        monitor: nil
      )
        raise ArgumentError, "no cert_filters given but require_cert is true" if require_cert && !cert_filters
        raise ArgumentError, "no ca_file given but cert_filters were set"     if cert_filters && !ca_file

        error_page_middleware(config, error_page)

        if cert_filters
          config.middleware.insert_before Rails::Auth::ACL::Middleware,
                                          Rails::Auth::X509::Middleware,
                                          require_cert: require_cert,
                                          cert_filters: cert_filters,
                                          ca_file:      ca_file,
                                          logger:       Rails.logger
        end

        return unless monitor
        config.middleware.insert_before Rails::Auth::ACL::Middleware,
                                        Rails::Auth::Monitor::Middleware,
                                        monitor
      end

      private

      # Adds error page middleware to the chain
      def error_page_middleware(config, error_page)
        case error_page
        when :debug
          config.middleware.insert_before Rails::Auth::ACL::Middleware,
                                          Rails::Auth::ErrorPage::DebugMiddleware,
                                          acl: config.x.rails_auth.acl
        when Pathname, String
          config.middleware.insert_before Rails::Auth::ACL::Middleware,
                                          Rails::Auth::ErrorPage::Middleware,
                                          page_body: Pathname(error_page).read
        when FalseClass, NilClass
        else raise TypeError, "bad error page mode: #{mode.inspect}"
        end
      end

      # Adds Rails::Auth::Credentials::InjectorMiddleware to the chain with the given credentials
      def credential_injector_middleware(config, credentials)
        config.middleware.insert_before Rails::Auth::ACL::Middleware,
                                        Rails::Auth::Credentials::InjectorMiddleware,
                                        credentials
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.07568345323741008, 'fraction_numerical': 0.0017266187050359713, 'mean_word_length': 1.832518337408313, 'pattern_counts': {'":': 0, '<': 3, '<?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': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4989426', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1657, 'n_words': 381}