From 4d0c6c388269d52eb20b5fc420965d124c38aa4b Mon Sep 17 00:00:00 2001 From: mo khan Date: Thu, 20 Mar 2025 09:13:08 -0600 Subject: feat: add html pages to ui service that fetches data from rest api --- bin/ui | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 135 insertions(+), 9 deletions(-) (limited to 'bin/ui') diff --git a/bin/ui b/bin/ui index 115fae6..2038628 100755 --- a/bin/ui +++ b/bin/ui @@ -108,6 +108,10 @@ module OAuth end module HTTPHelpers + def current_user?(request) + request.session[:id_token] + end + def not_found [404, { 'X-Backend-Server' => 'UI' }, []] end @@ -119,7 +123,6 @@ module HTTPHelpers [302, { 'Location' => "http://ui.example.com:8080#{location}" }, []] end end - end class UI @@ -132,10 +135,22 @@ class UI end def call(env) - path = env['PATH_INFO'] - case env['REQUEST_METHOD'] - when 'GET' - case path + request = Rack::Request.new(env) + case request.request_method + when Rack::GET + case request.path + when "/groups.html" + if current_user?(request) + return get_groups(request) + else + return redirect_to("/oidc/new") + end + when /\A\/groups\/\d+\/projects.html\z/ + if current_user?(request) + return get_projects(request) + else + return redirect_to("/oidc/new") + end when "/oauth/callback" return oauth_callback(Rack::Request.new(env)) when "/oidc/new" @@ -147,8 +162,8 @@ class UI else return redirect_to("/saml/new") end - when 'POST' - case path + when Rack::POST + case request.path when "/saml/assertions" return saml_assertions(Rack::Request.new(env)) else @@ -177,7 +192,119 @@ class UI def oauth_callback(request) response = oauth_client.exchange(grant_type: "authorization_code", code: request.params['code']) - [response.code, response.header, [response.body]] + if response.code == "200" + tokens = JSON.parse(response.body, symbolize_names: true) + request.session[:access_token] = tokens[:access_token] + request.session[:id_token] = tokens[:id_token] + request.session[:refresh_token] = tokens[:access_token] + + template = <<~ERB + + + + +
<%= response.body %>
+
<%= JSON.pretty_generate(request.session[:access_token]) %>
+ Groups + + + ERB + html = ERB.new(template, trim_mode: '-').result(binding) + [200, { 'Content-Type' => "text/html" }, [html]] + else + [response.code, response.header, [response.body]] + end + end + + def get_groups(request) + http = Net::Hippie::Client.new(headers: ::Net::Hippie::Client::DEFAULT_HEADERS.merge({ + 'Authorization' => Net::Hippie.bearer_auth(request.session[:access_token]) + })) + + response = http.get("http://api.example.com:8080/groups.json") + if response.code == "200" + groups = JSON.parse(response.body, symbolize_names: true) + template = <<~ERB + + + + + + + Groups + + + + + + + + + + + + <%- groups.each do |group| -%> + + + + + + + + <%- end -%> + +
IDNameOrganization IDParent ID 
<%= group[:id] %><%= group[:name] %><%= group[:organization_id] %><%= group[:parent_id] %>Projects
+ + + ERB + html = ERB.new(template, trim_mode: '-').result(binding) + [200, { 'Content-Type' => "text/html" }, [html]] + else + [response.code, response.header, [response.body]] + end + end + + def get_projects(request) + http = Net::Hippie::Client.new(headers: ::Net::Hippie::Client::DEFAULT_HEADERS.merge({ + 'Authorization' => Net::Hippie.bearer_auth(request.session[:access_token]) + })) + + response = http.get("http://api.example.com:8080/projects.json") + if response.code == "200" + projects = JSON.parse(response.body, symbolize_names: true) + + template = <<~ERB + + + + + + + Groups + + + + + + + + + <%- projects.each do |project| -%> + + + + + <%- end -%> + +
NameGroup ID
<%= project[:name] %><%= project[:group_id] %>
+ + + ERB + html = ERB.new(template, trim_mode: '-').result(binding) + [200, { 'Content-Type' => "text/html" }, [html]] + else + [response.code, response.header, [response.body]] + end end def saml_post_to_idp(request) @@ -216,7 +343,6 @@ class UI saml_response = saml_binding.deserialize(request.params) raise saml_response.errors unless saml_response.valid? - request.session[:access_token] = saml_response.attributes[:access_token] template = <<~ERB -- cgit v1.2.3