Commit 2e4e414

mo khan <mo@mokhan.ca>
2025-02-27 18:51:51
Parse the saml response and display it on a webpage
1 parent fc35bf1
Changed files (1)
src
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
+      <!doctype html>
+      <html>
+        <head><title></title></head>
+        <body style="background-color: pink;">
+          <h2>Received SAML Response</h2>
+          <textarea readonly="readonly" disabled="disabled" cols=220 rows=40><%=- saml_response.to_xml(pretty: true) -%></textarea>
+        </body>
+      </html>
+    ERB
+    erb = ERB.new(template, nil, trim_mode: '-')
+    html = erb.result(binding)
+    [200, { 'Content-Type' => "text/html" }, [html]]
+  end
 end
 
 if __FILE__ == $0