From 0436d46e76b1dfa0d794794adeba7408973de703 Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 26 Mar 2025 15:27:30 -0600 Subject: feat: extract a layout helper for rendering html --- bin/ui | 187 ++++++++++++++++++++++++++++++----------------------------------- 1 file changed, 87 insertions(+), 100 deletions(-) (limited to 'bin/ui') diff --git a/bin/ui b/bin/ui index 14b934d..4abe43f 100755 --- a/bin/ui +++ b/bin/ui @@ -11,7 +11,7 @@ gemfile do gem "rack", "~> 3.0" gem "rack-session", "~> 2.0" gem "rackup", "~> 2.0" - gem "saml-kit", "~> 1.0" + gem "saml-kit", "~> 1.0", git: "github.com:xlgmokha/saml-kit", branch: "main" gem "webrick", "~> 1.0" end @@ -137,6 +137,21 @@ module HTTPHelpers [302, { 'Location' => "#{$scheme}://#{$host}#{location}" }, []] end end + + def with_layout(bind) + template = <<~ERB + + + + + + + #{yield} + + + ERB + ERB.new(template, trim_mode: '-').result(bind) + end end class UI @@ -154,29 +169,24 @@ class UI when Rack::GET case request.path when "/index.html" - template = <<~ERB - - - - - <%- if current_user?(request) -%> - Groups -

Access Token

-
<%= request.session[:access_token] %>
-

ID Token

-
<%= request.session[:id_token] %>
- -
- -
- <%- else -%> - SAML Login - OIDC Login - <%- end -%> - - - ERB - html = ERB.new(template, trim_mode: '-').result(binding) + html = with_layout(binding) do + <<~ERB + <%- if current_user?(request) -%> + Groups +

Access Token

+
<%= request.session[:access_token] %>
+

ID Token

+
<%= request.session[:id_token] %>
+ +
+ +
+ <%- else -%> + SAML Login + OIDC Login + <%- end -%> + ERB + end return [200, { 'Content-Type' => "text/html" }, [html]] when "/groups.html" if current_user?(request) @@ -269,13 +279,8 @@ class UI response = http.get("http://api.example.com:8080/groups.json") if response.code == "200" groups = JSON.parse(response.body, symbolize_names: true) - template = <<~ERB - - - - - - + html = with_layout(binding) do + <<~ERB Home Groups
@@ -303,10 +308,8 @@ class UI <%- end -%> - - - ERB - html = ERB.new(template, trim_mode: '-').result(binding) + ERB + end [200, { 'Content-Type' => "text/html" }, [html]] else [response.code, response.header, [response.body]] @@ -322,35 +325,31 @@ class UI if response.code == "200" projects = JSON.parse(response.body, symbolize_names: true) - template = <<~ERB - - - - - - - Home - Groups - - - - - - - - - <%- projects.each do |project| -%> - - - - - <%- end -%> - -
NameGroup ID
<%= project[:name] %><%= project[:group_id] %>
- - - ERB - html = ERB.new(template, trim_mode: '-').result(binding) + html = with_layout(binding) do + <<~ERB + Home + Groups + + +
+ + + + + + + + + <%- projects.each do |project| -%> + + + + + <%- end -%> + +
NameGroup ID
<%= project[:name] %><%= project[:group_id] %>
+ ERB + end [200, { 'Content-Type' => "text/html" }, [html]] else [response.code, response.header, [response.body]] @@ -366,24 +365,19 @@ class UI @saml_builder = builder end - template = <<~ERB - - - - -

Sending SAML Request (SP -> IdP)

- - -
- <%- saml_params.each do |(key, value)| -%> - - <%- end -%> - -
- - - ERB - html = ERB.new(template, trim_mode: '-').result(binding) + html = with_layout(binding) do + <<~ERB +

Sending SAML Request (SP -> IdP)

+ + +
+ <%- saml_params.each do |(key, value)| -%> + + <%- end -%> + +
+ ERB + end [200, { 'Content-Type' => "text/html" }, [html]] end @@ -403,25 +397,18 @@ class UI request.session[:access_token] = tokens[:access_token] request.session[:refresh_token] = tokens[:access_token] - template = <<~ERB - - - - - - - Home - Groups + html = with_layout(binding) do + <<~ERB + Home + Groups -

Received SAML Response

- - - - - - - ERB - html = ERB.new(template, trim_mode: '-').result(binding) +

Received SAML Response

+ + + + + ERB + end [200, { 'Content-Type' => "text/html" }, [html]] else [response.code, response.header, [response.body]] -- cgit v1.2.3