# frozen_string_literal: true require 'cgi' require 'uri' module ShibRack # Handler for authenticating to an application in development mode, using # sample identities for local testing. class DevelopmentHandler < Handler class < v) } end end end def call(env) case env['PATH_INFO'] when '/login' login(env) when '/development' development else super end end private def login(env) params = CGI.parse(env['QUERY_STRING']) st, = params['identity'] identity = st && DevelopmentHandler.resolve_identity(st) if identity restore_original_query(env) return super(env.merge(identity)) end env['rack.session']['original_params'] = params [302, { 'Location' => "#{env['SCRIPT_NAME']}/development" }, []] end def restore_original_query(env) original_params = env['rack.session']['original_params'] return unless original_params env['QUERY_STRING'] = URI.encode_www_form(original_params) env['rack.session']['original_params'] = nil end def development [ 200, { 'Content-Type' => 'text/html; charset=utf-8' }, [development_html] ] end def development_html <<~EOF Development Login

Please select an identity

#{identities_html} EOF end def identities_html DevelopmentHandler.identities.map { |i| identity_html(i) }.join end def identity_html(identity) <<~EOF

#{JSON.pretty_generate(identity)}
EOF end end end