Sha256: d255433b11c3ade0088e922d1786875b304fb36756c062c0d04e2de923181571

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 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) { |a, e| a.const_get(e) }
    end

    def receiver
      @receiver.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shib-rack-0.1.0 lib/shib_rack/handler.rb