Class: RackEntraIdAuth::MockMiddleware
- Inherits:
-
Object
- Object
- RackEntraIdAuth::MockMiddleware
- Defined in:
- lib/rack_entra_id_auth/mock_middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ MockMiddleware
constructor
A new instance of MockMiddleware.
Constructor Details
#initialize(app) ⇒ MockMiddleware
Returns a new instance of MockMiddleware.
7 8 9 |
# File 'lib/rack_entra_id_auth/mock_middleware.rb', line 7 def initialize (app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rack_entra_id_auth/mock_middleware.rb', line 11 def call (env) request = Rack::Request.new(env) entra_id_request = EntraIdRequest.new(request) # mock a login page if entra_id_request.login? and request.request_method.eql?('GET') log(env, 'Rendering mock login page…') return [ 200, { 'Content-Type' => 'text/html' }, [ login_page(request.url) ] ] end # mock a login request if entra_id_request.login? and request.request_method.eql?('POST') log(env, 'Initializing session and redirecting to relay state URL…') attributes = RackEntraIdAuth.config.mock_attributes[request.params['username']] || {} redirect_url = request.params['relay_state'] || request.params['RelayState'] || RackEntraIdAuth.config.login_relay_state_url || entra_id_request.base_url request.session[RackEntraIdAuth.config.session_key] = RackEntraIdAuth.config.session_value_proc.call(attributes) return found_redirect_response( redirect_url, 'Initializing session and redirecting to relay state URL') end # mock a logout request if entra_id_request.logout? log(env, 'Destroying session and redirecting to relay state URL…') redirect_url = request.params['relay_state'] || request.params['RelayState'] || RackEntraIdAuth.config.logout_relay_state_url || entra_id_request.base_url request.session.send(request.session.respond_to?(:destroy) ? :destroy : :clear) return found_redirect_response( redirect_url, 'Destroying session and redirecting to relay state URL') end response = @app.call(env) # Authenticate 401s if response[0] == 401 log(env, 'Intercepted 401 Unauthorized response, redirecting to mock login URL…') login_url = URI::HTTP.build(host: request.host, port: request.port, path: RackEntraIdAuth.config.login_path, query: URI.encode_www_form({ :RelayState => request.url })) return found_redirect_response( login_url, 'Intercepted 401 Unauthorized response, redirecting to mock login URL') end response end |