Commit 4e0b350

mo khan <mo@mokhan.ca>
2025-03-18 22:35:05
refactor: extract http helpers module
1 parent 6b53e1e
Changed files (1)
bin
bin/ui
@@ -107,7 +107,24 @@ module OAuth
   end
 end
 
+module HTTPHelpers
+  def not_found
+    [404, { 'X-Backend-Server' => 'UI' }, []]
+  end
+
+  def redirect_to(location)
+    if location.start_with?("http")
+      [302, { 'Location' => location }, []]
+    else
+      [302, { 'Location' => "http://ui.example.com:8080#{location}" }, []]
+    end
+  end
+
+end
+
 class UI
+  include ::HTTPHelpers
+
   attr_reader :oauth_client
 
   def initialize(oauth_client)
@@ -150,7 +167,7 @@ class UI
       builder.organization_name = "Acme, Inc"
       builder.organization_url = "https://example.com"
       builder.build_service_provider do |x|
-        x.name_id_formats = [Saml::Kit::Namespaces::EMAIL_ADDRESS]
+        x.name_id_formats = [Saml::Kit::Namespaces::PERSISTENT]
         x.add_assertion_consumer_service("#{$scheme}://#{$host}/saml/assertions", binding: :http_post)
       end
     end
@@ -158,19 +175,6 @@ class UI
     [200, { 'Content-Type' => "application/samlmetadata+xml" }, [xml]]
   end
 
-
-  def not_found
-    [404, { 'X-Backend-Server' => 'UI' }, []]
-  end
-
-  def redirect_to(location)
-    if location.start_with?("http")
-      [302, { 'Location' => location }, []]
-    else
-      [302, { 'Location' => "http://ui.example.com:8080#{location}" }, []]
-    end
-  end
-
   def oauth_callback(request)
     response = oauth_client.exchange(grant_type: "authorization_code", code: request.params['code'])
     [response.code, response.header, [response.body]]
@@ -216,14 +220,16 @@ class UI
     template = <<~ERB
       <!doctype html>
       <html>
-        <head><title></title></head>
+        <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, trim_mode: '-')
+    erb = ERB.new(template)
     html = erb.result(binding)
     [200, { 'Content-Type' => "text/html" }, [html]]
   end