SSOwat/access.lua

139 lines
4.1 KiB
Lua
Raw Normal View History

2013-10-15 13:58:16 +02:00
--
-- access.lua
--
-- This file is executed at every request on a protected domain or server.
-- You just have to read this file normally to understand how and when the
-- request is handled: redirected, forbidden, bypassed or served.
2013-10-15 13:58:16 +02:00
--
-- Import helpers
2023-07-13 16:41:17 +02:00
local hlp = require("helpers")
2013-10-15 10:11:39 +02:00
-- Initialize and get configuration
hlp.refresh_config()
local conf = hlp.get_config()
-- Just a note for the client to know that he passed through the SSO
2013-10-16 11:57:53 +02:00
ngx.header["X-SSO-WAT"] = "You've just been SSOed"
2013-10-15 10:11:39 +02:00
local is_logged_in = hlp.check_authentication()
2015-02-15 12:31:23 +01:00
--
2020-09-20 18:00:49 +02:00
-- 3. REDIRECTED URLS
2015-02-15 12:31:23 +01:00
--
-- If the URL matches one of the `redirected_urls` in the configuration file,
-- just redirect to the target URL/URI
--
2020-09-20 18:00:49 +02:00
function detect_redirection(redirect_url)
2015-02-12 12:08:52 +01:00
if hlp.string.starts(redirect_url, "http://")
or hlp.string.starts(redirect_url, "https://") then
return hlp.redirect(redirect_url)
elseif hlp.string.starts(redirect_url, "/") then
return hlp.redirect(ngx.var.scheme.."://"..ngx.var.host..redirect_url)
else
2015-02-12 12:08:52 +01:00
return hlp.redirect(ngx.var.scheme.."://"..redirect_url)
end
end
if conf["redirected_urls"] then
for url, redirect_url in pairs(conf["redirected_urls"]) do
2015-02-12 12:08:52 +01:00
if url == ngx.var.host..ngx.var.uri..hlp.uri_args_string()
or url == ngx.var.scheme.."://"..ngx.var.host..ngx.var.uri..hlp.uri_args_string()
or url == ngx.var.uri..hlp.uri_args_string() then
hlp.logger:debug("Requested URI is in redirected_urls")
detect_redirection(redirect_url)
end
end
end
if conf["redirected_regex"] then
for regex, redirect_url in pairs(conf["redirected_regex"]) do
2020-01-17 08:01:24 +01:00
if hlp.match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex)
or hlp.match(ngx.var.scheme.."://"..ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex)
or hlp.match(ngx.var.uri..hlp.uri_args_string(), regex) then
hlp.logger:debug("Requested URI is in redirected_regex")
detect_redirection(redirect_url)
end
end
end
2014-01-31 21:25:46 +01:00
--
-- 4. IDENTIFY THE RELEVANT PERMISSION
2020-05-21 21:53:04 +02:00
--
-- In particular, the conf is filled with permissions such as:
2020-05-21 21:53:04 +02:00
--
-- "foobar": {
-- "auth_header": false,
-- "label": "Foobar permission",
-- "public": false,
-- "show_tile": true,
-- "uris": [
-- "yolo.test/foobar",
-- "re:^[^/]*/%.well%-known/foobar/.*$",
-- ],
-- "users": ["alice", "bob"]
-- }
--
--
-- And we find the best matching permission by trying to match the request uri
-- against all the uris rules/regexes from the conf and keep the longest matching one.
2020-05-21 21:53:04 +02:00
--
permission = nil
longest_url_match = ""
2021-01-20 01:28:08 +01:00
ngx_full_url = ngx.var.host..ngx.var.uri
for permission_name, permission_infos in pairs(conf["permissions"]) do
if next(permission_infos['uris']) ~= nil then
for _, url in pairs(permission_infos['uris']) do
if string.starts(url, "re:") then
url = string.sub(url, 4, string.len(url))
end
2021-01-20 01:28:08 +01:00
-- We want to match the beginning of the url
if not string.starts(url, "^") then
url = "^"..url
end
2021-01-20 01:28:08 +01:00
local m = hlp.match(ngx_full_url, url)
if m ~= nil and string.len(m) > string.len(longest_url_match) then
longest_url_match = m
permission = permission_infos
permission["id"] = permission_name
end
end
end
end
2020-05-21 21:53:04 +02:00
--
--
-- 5. APPLY PERMISSION
--
--
-- 1st case : client has access
if hlp.has_access(permission) then
2020-01-29 12:24:51 +01:00
2020-04-01 00:43:59 +02:00
if is_logged_in then
2020-12-17 17:06:19 +01:00
-- If Basic Authorization header are enable for this permission,
-- add it to the response
if permission["auth_header"] then
hlp.set_basic_auth_header()
2020-04-01 00:43:59 +02:00
end
end
return hlp.pass()
2013-10-20 17:24:44 +02:00
-- 2nd case : no access ... redirect to portal / login form
2020-05-21 21:51:55 +02:00
else
if is_logged_in then
return hlp.redirect(conf.portal_url)
else
local back_url = "https://" .. ngx.var.host .. ngx.var.uri .. hlp.uri_args_string()
return hlp.redirect(conf.portal_url.."?r="..ngx.encode_base64(back_url))
end
2020-05-21 21:51:55 +02:00
end