2015-02-01 15:04:36 +01:00
|
|
|
--
|
|
|
|
-- helpers.lua
|
|
|
|
--
|
|
|
|
-- This is a file called at every request by the `access.lua` file. It contains
|
|
|
|
-- a set of useful functions related to HTTP and LDAP.
|
|
|
|
--
|
|
|
|
|
2015-02-15 13:03:01 +01:00
|
|
|
module('helpers', package.seeall)
|
2015-02-01 15:04:36 +01:00
|
|
|
|
2015-05-16 09:42:26 +02:00
|
|
|
local cache = ngx.shared.cache
|
|
|
|
local conf = config.get_config()
|
2021-12-26 17:01:56 +01:00
|
|
|
local Logging = require("logging")
|
|
|
|
local jwt = require("vendor.luajwtjitsi.luajwtjitsi")
|
2023-07-11 22:41:09 +02:00
|
|
|
local cipher = require('openssl.cipher')
|
|
|
|
local mime = require("mime")
|
2021-12-26 17:01:56 +01:00
|
|
|
|
|
|
|
local appender = function(self, level, message)
|
|
|
|
|
|
|
|
-- Output to log file
|
|
|
|
local fp = io.open(log_file, "a")
|
|
|
|
local str = string.format("[%-6s%s] %s\n", level:upper(), os.date(), message)
|
|
|
|
fp:write(str)
|
|
|
|
fp:close()
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local logger = Logging.new(appender)
|
|
|
|
--logger:setLevel(logger.DEBUG) -- FIXME
|
2017-05-12 13:54:39 +02:00
|
|
|
|
2019-10-03 23:11:52 +02:00
|
|
|
|
2020-01-17 08:01:24 +01:00
|
|
|
-- Import Perl regular expressions library
|
|
|
|
local rex = require "rex_pcre"
|
|
|
|
|
2020-04-01 00:43:59 +02:00
|
|
|
local is_logged_in = false
|
|
|
|
|
2020-03-29 18:02:49 +02:00
|
|
|
function refresh_config()
|
|
|
|
conf = config.get_config()
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_config()
|
|
|
|
return conf
|
|
|
|
end
|
2020-01-17 08:01:24 +01:00
|
|
|
|
|
|
|
-- The 'match' function uses PCRE regex as default
|
|
|
|
-- If '%.' is found in the regex, we assume it's a LUA regex (legacy code)
|
|
|
|
-- 'match' returns the matched text.
|
|
|
|
function match(s, regex)
|
|
|
|
if not string.find(regex, '%%%.') then
|
|
|
|
return rex.match(s, regex)
|
|
|
|
else
|
|
|
|
return string.match(s,regex)
|
|
|
|
end
|
|
|
|
end
|
2019-10-03 23:11:52 +02:00
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Read a FS stored file
|
|
|
|
function read_file(file)
|
|
|
|
local f = io.open(file, "rb")
|
|
|
|
if not f then return false end
|
|
|
|
local content = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
return content
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Lua has no sugar :D
|
2016-04-30 12:40:59 +02:00
|
|
|
function is_in_table(t, v)
|
2015-02-01 15:04:36 +01:00
|
|
|
for key, value in ipairs(t) do
|
|
|
|
if value == v then return key end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-06-14 12:53:16 +02:00
|
|
|
-- Get the index of a value in a table
|
|
|
|
function index_of(t,val)
|
2017-08-10 16:31:00 +02:00
|
|
|
for k,v in ipairs(t) do
|
2015-06-14 12:53:16 +02:00
|
|
|
if v == val then return k end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Test whether a string starts with another
|
2016-04-30 12:40:59 +02:00
|
|
|
function string.starts(String, Start)
|
2019-01-23 10:22:02 +01:00
|
|
|
if not String then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return string.sub(String, 1, string.len(Start)) == Start
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Test whether a string ends with another
|
2016-04-30 12:40:59 +02:00
|
|
|
function string.ends(String, End)
|
2015-02-01 15:04:36 +01:00
|
|
|
return End=='' or string.sub(String, -string.len(End)) == End
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Convert a table of arguments to an URI string
|
2016-04-30 12:40:59 +02:00
|
|
|
function uri_args_string(args)
|
2015-02-01 15:04:36 +01:00
|
|
|
if not args then
|
|
|
|
args = ngx.req.get_uri_args()
|
|
|
|
end
|
|
|
|
String = "?"
|
|
|
|
for k,v in pairs(args) do
|
|
|
|
String = String..tostring(k).."="..tostring(v).."&"
|
|
|
|
end
|
|
|
|
return string.sub(String, 1, string.len(String) - 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Validate authentification
|
|
|
|
--
|
|
|
|
-- Check if the session cookies are set, and rehash server + client information
|
|
|
|
-- to match the session hash.
|
|
|
|
--
|
2021-12-26 17:01:56 +01:00
|
|
|
function check_authentication()
|
|
|
|
|
|
|
|
local token = ngx.var["cookie_" .. conf["cookie_name"]]
|
|
|
|
|
|
|
|
decoded, err = jwt.verify(token, "HS256", cookie_secret)
|
|
|
|
|
|
|
|
if err ~= nil then
|
|
|
|
-- FIXME : log an authentication error to be caught by fail2ban ? or should it happen somewhere else ? (check the old code)
|
|
|
|
authUser = nil
|
2023-07-11 22:41:09 +02:00
|
|
|
authPasswordEnc = nil
|
2021-12-26 17:01:56 +01:00
|
|
|
is_logged_in = false
|
|
|
|
return is_logged_in
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
2023-07-11 22:41:09 +02:00
|
|
|
-- cf. src/authenticators/ldap_ynhuser.py in YunoHost to see how the cookie is actually created
|
2021-12-26 17:01:56 +01:00
|
|
|
authUser = decoded["user"]
|
2023-07-11 22:41:09 +02:00
|
|
|
authPasswordEnc = decoded["pwd"]
|
2021-12-26 17:01:56 +01:00
|
|
|
is_logged_in = true
|
2020-12-24 17:47:55 +01:00
|
|
|
|
2021-12-26 17:01:56 +01:00
|
|
|
-- Gotta update authUser and is_logged_in
|
2020-12-24 17:47:55 +01:00
|
|
|
return is_logged_in
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
2023-07-11 22:41:09 +02:00
|
|
|
-- Extract the user password from cookie,
|
|
|
|
-- needed to create the basic auth header
|
|
|
|
function decrypt_user_password()
|
|
|
|
-- authPasswordEnc is actually a string formatted as <password_enc_b64>|<iv_b64>
|
|
|
|
-- For example: ctl8kk5GevYdaA5VZ2S88Q==|yTAzCx0Gd1+MCit4EQl9lA==
|
|
|
|
-- The password is encoded using AES-256-CBC with the IV being the right-side data
|
|
|
|
local password_enc_b64, iv_b64 = authPasswordEnc:match("([^|]+)|([^|]+)")
|
|
|
|
local password_enc = mime.unb64(password_enc_b64)
|
|
|
|
local iv = mime.unb64(iv_b64)
|
|
|
|
return cipher.new('aes-256-cbc'):decrypt(cookie_secret, iv):final(password_enc)
|
|
|
|
end
|
|
|
|
|
2020-03-04 11:34:24 +01:00
|
|
|
-- Check whether a user is allowed to access a URL using the `permissions` directive
|
2015-02-01 15:04:36 +01:00
|
|
|
-- of the configuration file
|
2020-04-01 00:43:59 +02:00
|
|
|
function has_access(permission, user)
|
2015-05-16 21:03:06 +02:00
|
|
|
user = user or authUser
|
2015-02-01 15:04:36 +01:00
|
|
|
|
2020-04-01 00:43:59 +02:00
|
|
|
if permission == nil then
|
2021-12-26 17:01:56 +01:00
|
|
|
logger:debug("No permission matching request for "..ngx.var.uri)
|
2020-01-29 12:24:25 +01:00
|
|
|
return false
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
|
|
|
|
2020-04-01 00:43:59 +02:00
|
|
|
-- Public access
|
|
|
|
if user == nil or permission["public"] then
|
2020-05-21 22:57:05 +02:00
|
|
|
user = user or "A visitor"
|
2021-12-26 17:01:56 +01:00
|
|
|
logger:debug(user.." tries to access "..ngx.var.uri.." (corresponding perm: "..permission["id"]..")")
|
2020-04-01 00:43:59 +02:00
|
|
|
return permission["public"]
|
|
|
|
end
|
|
|
|
|
2021-12-26 17:01:56 +01:00
|
|
|
logger:debug("User "..user.." tries to access "..ngx.var.uri.." (corresponding perm: "..permission["id"]..")")
|
2020-04-01 00:43:59 +02:00
|
|
|
|
2020-09-21 14:41:23 +02:00
|
|
|
-- The user has permission to access the content if he is in the list of allowed users
|
|
|
|
if element_is_in_table(user, permission["users"]) then
|
2021-12-26 17:01:56 +01:00
|
|
|
logger:debug("User "..user.." can access "..ngx.var.host..ngx.var.uri..uri_args_string())
|
2020-09-21 14:41:23 +02:00
|
|
|
return true
|
|
|
|
else
|
2021-12-26 17:01:56 +01:00
|
|
|
logger:debug("User "..user.." cannot access "..ngx.var.uri)
|
2020-09-21 14:41:23 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function element_is_in_table(element, table)
|
|
|
|
if table then
|
|
|
|
for _, el in pairs(table) do
|
|
|
|
if el == element then
|
2020-01-20 16:59:25 +01:00
|
|
|
return true
|
|
|
|
end
|
2017-05-12 13:54:39 +02:00
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
end
|
2019-10-03 23:11:52 +02:00
|
|
|
|
2020-01-20 16:59:25 +01:00
|
|
|
return false
|
2019-10-03 23:11:52 +02:00
|
|
|
end
|
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Set the authentication headers in order to pass credentials to the
|
|
|
|
-- application underneath.
|
2023-07-11 22:41:09 +02:00
|
|
|
function set_basic_auth_header(user)
|
2020-12-17 17:06:19 +01:00
|
|
|
local user = user or authUser
|
2020-12-23 18:39:54 +01:00
|
|
|
-- Set `Authorization` header to enable HTTP authentification
|
|
|
|
ngx.req.set_header("Authorization", "Basic "..ngx.encode_base64(
|
2023-07-11 22:41:09 +02:00
|
|
|
user..":"..decrypt_user_password()
|
2020-12-17 17:06:19 +01:00
|
|
|
))
|
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
|
2020-12-17 17:06:19 +01:00
|
|
|
|
2015-02-01 15:04:36 +01:00
|
|
|
-- Set cookie and redirect (needed to properly set cookie)
|
2016-04-30 12:40:59 +02:00
|
|
|
function redirect(url)
|
2021-12-26 17:01:56 +01:00
|
|
|
logger:debug("Redirecting to "..url)
|
2021-09-19 21:15:54 +02:00
|
|
|
-- For security reason we don't allow to redirect onto unknown domain
|
|
|
|
-- And if `uri_args.r` contains line break, someone is probably trying to
|
|
|
|
-- pass some additional headers
|
|
|
|
|
|
|
|
-- This should cover the following cases:
|
|
|
|
-- https://malicious.domain.tld/foo/bar
|
|
|
|
-- http://malicious.domain.tld/foo/bar
|
|
|
|
-- https://malicious.domain.tld:1234/foo
|
|
|
|
-- malicious.domain.tld/foo/bar
|
|
|
|
-- (/foo/bar, in which case no need to make sure it's prefixed with https://)
|
|
|
|
if not string.starts(url, "/") and not string.starts(url, "http://") and not string.starts(url, "https://") then
|
|
|
|
url = "https://"..url
|
|
|
|
end
|
2021-11-16 21:40:04 +01:00
|
|
|
local is_known_domain = string.starts(url, "/")
|
2021-11-15 19:02:13 +01:00
|
|
|
for _, domain in ipairs(conf["domains"]) do
|
2021-11-16 21:40:04 +01:00
|
|
|
if is_known_domain then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
-- Replace - character to %- because - is a special char for regex in lua
|
|
|
|
domain = string.gsub(domain, "%-","%%-")
|
2021-11-15 19:02:13 +01:00
|
|
|
is_known_domain = is_known_domain or url:match("^https?://"..domain.."/?") ~= nil
|
|
|
|
end
|
|
|
|
if string.match(url, "(.*)\n") or not is_known_domain then
|
2021-12-26 17:01:56 +01:00
|
|
|
logger:debug("Unauthorized redirection to "..url)
|
2021-09-19 21:15:54 +02:00
|
|
|
url = conf.portal_url
|
|
|
|
end
|
2015-02-01 15:04:36 +01:00
|
|
|
return ngx.redirect(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Set cookie and go on with the response (needed to properly set cookie)
|
2016-04-30 12:40:59 +02:00
|
|
|
function pass()
|
2021-12-26 17:01:56 +01:00
|
|
|
logger:debug("Allowing to pass through "..ngx.var.uri)
|
2015-02-01 15:04:36 +01:00
|
|
|
|
|
|
|
-- When we are in the SSOwat portal, we need a default `content-type`
|
|
|
|
if string.ends(ngx.var.uri, "/")
|
|
|
|
or string.ends(ngx.var.uri, ".html")
|
|
|
|
or string.ends(ngx.var.uri, ".htm")
|
|
|
|
then
|
|
|
|
ngx.header["Content-Type"] = "text/html"
|
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|