Sha256: a8c6a98a74c07844a4c7ee257d1626db9d5911ca2f77cbaa176a537af2cc4138

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module ShibRack
  # Rack application which handles incoming sessions authenticated by
  # Shibboleth SP.
  class Handler
    attr_reader :error_handler
    private :error_handler

    def initialize(opts)
      @receiver = constantize(opts[:receiver])
      @error_handler = constantize(opts[:error_handler])&.new || self
    end

    def call(env)
      return login(env) if env['PATH_INFO'] == '/login'
      return logout(env) if env['PATH_INFO'] == '/logout'
      [404, {}, ["Not found: #{env['PATH_INFO']}"]]
    end

    def handle(_env, _exception)
      [
        400, { 'Content-Type' => 'text/plain' }, [
          'Sorry, your attempt to log in to this service was not successful. ',
          'Please contact the service owner for assistance, and include the ',
          'link you used to access this service.'
        ]
      ]
    end

    private

    def login(env)
      receiver.receive(env)
    rescue => e
      error_handler.handle(env, e)
    end

    def logout(env)
      receiver.logout(env)
    end

    def constantize(klass_name)
      return nil if klass_name.nil?
      klass_name.split('::').reduce(Kernel) { |acc, elem| acc.const_get(elem) }
    end

    def receiver
      @receiver.new
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shib-rack-0.3.0 lib/shib_rack/handler.rb
shib-rack-0.2.0 lib/shib_rack/handler.rb