Sha256: 905bb1df5a97fa0bff21f21c5ae27a0ee2d0c8f66c8300aa887bd8e1d709fbe1

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module KeycloakRack
  # This serves as the primary interface for interacting with Rack and Rails applications,
  # and an instance gets mounted into `keycloak:session` when the middleware processes.
  class Session
    extend Dry::Initializer

    include Dry::Matcher.for(:authenticate!, with: Dry::Matcher::ResultMatcher)

    option :auth_result, Types.Instance(Dry::Monads::Result)
    option :skipped, Types::Bool, default: proc { false }
    option :token, Types.Instance(KeycloakRack::DecodedToken).optional, optional: true
    option :authorize_realm, Types.Interface(:call), default: proc { KeycloakRack::AuthorizeRealm.new self }
    option :authorize_resource, Types.Interface(:call), default: proc { KeycloakRack::AuthorizeResource.new self }

    delegate :has_realm_role?, :has_resource_role?, to: :token, allow_nil: true

    alias skipped? skipped

    # @return [Dry::Monads::Result]
    def authenticate!
      auth_result
    end

    # @return [Dry::Monads::Result]
    def authorize_realm!(*args)
      authorize_realm.call(*args)
    end

    # @return [Dry::Monads::Result]
    def authorize_resource!(*args)
      authorize_resource.call(*args)
    end

    def authenticated?
      auth_result.success? && token.present?
    end

    def anonymous?
      auth_result.success? && token.blank?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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