Sha256: 61cc077c3716b79eafe89f21248105a4b653b5ce0612ee998b0ffb1866593164

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module KeycloakRack
  # Check if the request should be skipped based on the request method and path.
  #
  # @api private
  # @!visibility private
  class SkipAuthentication
    include Dry::Monads[:result]

    include Import[config: "keycloak-rack.config"]

    delegate :skip_paths, to: :config

    # @return [Dry::Monads::Success(Boolean)]
    def call(env)
      method = env["REQUEST_METHOD"].to_s.downcase
      path   = env["PATH_INFO"]

      return Success(true) if preflight?(method, env)
      return Success(true) if should_skip?(method, path)

      Success(false)
    end

    private

    def should_skip?(method, path)
      method_paths = skip_paths.fetch(method, [])

      method_paths.any? do |path_pattern|
        if path_pattern.kind_of?(Regexp)
          path_pattern.match? path
        else
          path_pattern == path
        end
      end
    end

    def preflight?(method, headers)
      method == "options" && headers["HTTP_ACCESS_CONTROL_REQUEST_METHOD"].present?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
keycloak_rack-1.1.1 lib/keycloak_rack/skip_authentication.rb
keycloak_rack-1.1.0 lib/keycloak_rack/skip_authentication.rb
keycloak_rack-1.0.0 lib/keycloak_rack/skip_authentication.rb