refactor: remove code duplication

Yves G 2021-10-03 14:05:10 +02:00
parent 7e2e2bcf49
commit b045c60e19
3 changed files with 58 additions and 83 deletions

View File

@ -1,4 +1,4 @@
local ngx = require("ngx") local ngx = require("ngx")
local b64 = require("ssso_base64") local b64 = require("ssso_base64")
local util = require("ssso_util") local util = require("ssso_util")
local conf = require("ssso_config") local conf = require("ssso_config")

View File

@ -1,7 +1,6 @@
local util = require("ssso_util") local util = require("ssso_util")
local conf = require("ssso_config") local conf = require("ssso_config")
local nginx = require("ssso_nginx") local nginx = require("ssso_nginx")
local sites = require("ssso_sites")
local root = "" local root = ""

View File

@ -62,25 +62,36 @@ local function handle_request(req_data, auth)
end end
end end
local function format_pattern(pattern) local function parse_known_sites(user, denied_handler, allowed_handler)
local a_type local f, site, go_on
local ok = { for _, known in ipairs(known_sites) do
r = pattern.lua_regex or {}, f = io.open(known, "r")
a = {}, if f then
} site = json.decode(f:read("*all"))
for _, action in ipairs(pattern.actions or {}) do f:close()
if action.type == "header" then for _, pat in ipairs(site.patterns) do
a_type = "H" go_on = true
elseif action.type == "cookie" then for _, denied in ipairs(pat.deny or {}) do
a_type = "C" if denied == user then
else go_on = false
a_type = nil denied_handler(pat)
end end
if a_type then end
table.insert(ok.a, {a_type, action.name, action.value}) if go_on then
if pat.public then
allowed_handler(pat)
else
for _, allowed in ipairs(pat.allow or {}) do
if allowed == "*" or allowed == user then
allowed_handler(pat)
break
end
end
end
end
end
end end
end end
return ok
end end
local class__profile = {} local class__profile = {}
@ -102,43 +113,35 @@ function class__profile:build_from_lists(user, password, name, email, ok_list, k
end end
function class__profile:build_from_conf(user, password, name, email) function class__profile:build_from_conf(user, password, name, email)
local f, site, go_on
local ok_list = {} local ok_list = {}
local ko_list = {} local ko_list = {}
local delegate_identity = id.class__identity:build(user, password, name, email) local delegate_identity = id.class__identity:build(user, password, name, email)
for _, known in ipairs(known_sites) do parse_known_sites(user,
f = io.open(known, "r") function (ko_pat)
if f then for _, re in ipairs(ko_pat.lua_regex) do
site = json.decode(f:read("*all")) table.insert(ko_list, re)
f:close() end
for _, pat in ipairs(site.patterns) do end,
go_on = true function (ok_pat)
for _, denied in ipairs(pat.deny or {}) do local a_type
if denied == user then local ok = {
go_on = false r = ok_pat.lua_regex or {},
for _, re in ipairs(pat.lua_regex) do a = {},
table.insert(ko_list, re) }
end for _, action in ipairs(ok_pat.actions or {}) do
break if action.type == "header" then
end a_type = "H"
elseif action.type == "cookie" then
a_type = "C"
else
a_type = nil
end end
if go_on then if a_type then
if pat.public then table.insert(ok.a, {a_type, action.name, action.value})
local ok = format_pattern(pat)
table.insert(ok_list, ok)
else
for _, allowed in ipairs(pat.allow or {}) do
if allowed == "*" or allowed == user then
local ok = format_pattern(pat)
table.insert(ok_list, ok)
break
end
end
end
end end
end end
end table.insert(ok_list, ok)
end end)
return self:build(delegate_identity, ok_list, ko_list) return self:build(delegate_identity, ok_list, ko_list)
end end
@ -201,40 +204,13 @@ end
function class__profile:authorized_links() function class__profile:authorized_links()
local links = {} local links = {}
local f, site, go_on parse_known_sites(self:user(),
local user = self:user() function (_) end,
for _, name in ipairs(known_sites) do function (ok_pat)
f = io.open(name, "r") for link, label in pairs(ok_pat.portal or {}) do
if f then table.insert(links, {link = link, label = label})
site = json.decode(f:read("*all"))
f:close()
for _, pat in ipairs(site.patterns) do
go_on = true
for _, denied in ipairs(pat.deny or {}) do
if denied == user then
go_on = false
break
end
end
if go_on then
if pat.public then
for link, label in pairs(pat.portal or {}) do
table.insert(links, {link = link, label = label})
end
else
for _, allowed in ipairs(pat.allow or {}) do
if allowed == "*" or allowed == user then
for link, label in pairs(pat.portal or {}) do
table.insert(links, {link = link, label = label})
end
break
end
end
end
end
end end
end end)
end
return links return links
end end