From 2e4e414fc68752b2e2d1e3347c4fce54ddf5f26e Mon Sep 17 00:00:00 2001 From: mo khan Date: Thu, 27 Feb 2025 11:51:51 -0700 Subject: Parse the saml response and display it on a webpage --- src/sp/main.rb | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/sp/main.rb b/src/sp/main.rb index 9ba9268a..68a0e3db 100644 --- a/src/sp/main.rb +++ b/src/sp/main.rb @@ -70,7 +70,7 @@ class ServiceProvider case path when "/assertions" # TODO:: Render the SAMLResponse from the IdP - return not_found + return assertions(Rack::Request.new(env)) else return not_found end @@ -85,15 +85,12 @@ class ServiceProvider end def post_to_idp(request) - entity_id = 'http://localhost:8282/metadata.xml' - idp = Saml::Kit.registry.metadata_for(entity_id) + idp = Saml::Kit.registry.metadata_for('http://localhost:8282/metadata.xml') relay_state = Base64.strict_encode64(JSON.generate(redirect_to: '/dashboard')) @saml_builder = nil uri, saml_params = idp.login_request_for(binding: :http_post, relay_state: relay_state) do |builder| @saml_builder = builder - # builder.issuer = params[:issuer] if params[:issuer].present? - # builder.assertion_consumer_service_url = callback_url end template = <<~ERB @@ -117,6 +114,27 @@ class ServiceProvider html = erb.result(binding) [200, { 'Content-Type' => "text/html" }, [html]] end + + def assertions(request) + sp = Saml::Kit.registry.metadata_for('http://localhost:8283/metadata.xml') + saml_binding = sp.assertion_consumer_service_for(binding: :http_post) + saml_response = saml_binding.deserialize(request.params) + raise saml_response.errors unless saml_response.valid? + + template = <<~ERB + + + + +

Received SAML Response

+ + + + ERB + erb = ERB.new(template, nil, trim_mode: '-') + html = erb.result(binding) + [200, { 'Content-Type' => "text/html" }, [html]] + end end if __FILE__ == $0 -- cgit v1.2.3