simple-sso/src/ssso_portal.lua

55 lines
1.8 KiB
Lua

local util = require("ssso_util")
local conf = require("ssso_config")
local nginx = require("ssso_nginx")
local sites = require("ssso_sites")
local root = ""
local function set_root(prefix)
root = prefix .. "/portal/"
end
local function contents(relative)
local file = assert(io.open(root .. relative, "r"), "Cannot open portal file " .. root .. relative)
local data = file:read("*all")
file:close()
return data
end
local function inject_data(html, profile)
html = html:gsub("SSSO_USER", util.str_to_html(profile:user()))
html = html:gsub("SSSO_NAME", util.str_to_html(profile:name()))
html = html:gsub("SSSO_EMAIL", util.str_to_html(profile:email()))
local links = ""
local allowed = profile:authorized_links()
table.sort(allowed, function(a1, a2) return a1.label < a2.label end)
for _, allow in ipairs(allowed) do
links = links .. '<li><a href="' .. profile:format(allow.link) .. '"><span>' .. util.str_to_html(profile:format(allow.label)) .. "</span></a></li>"
end
if links ~= "" then
html = html:gsub('<nav id="sites"></nav>', '<nav id="sites"><ul>' .. links .. "</ul></nav>")
else
html = html:gsub('<nav id="sites"></nav>', '<p>No link to display.</p>')
end
return html
end
local function answer_request(req_data, profile)
local portal = conf.get_sso_prefix() .. "/portal"
if req_data:is(portal) then
local html = inject_data(contents("portal.html"), profile)
return nginx.return_contents(html, "text/html; charset=UTF-8")
elseif req_data:is(portal .. ".css") then
return nginx.return_contents(contents("portal.css"), "text/css; charset=UTF-8")
elseif req_data:is(portal .. ".js") then
return nginx.return_contents(contents("portal.js"), "application/javascript; charset=UTF-8")
else
return nginx.answer_not_found(req_data)
end
end
return {
answer_request = answer_request,
set_root = set_root,
}