# 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