1 -- This script may be used with the auth-filter. Be sure to configure it as you wish.
5 -- <http://mkottman.github.io/luacrypto/>
7 -- <https://git.zx2c4.com/lualdap/about/>
13 -- Configure these variables for your settings.
17 -- A list of password protected repositories, with which gentooAccess
18 -- group is allowed to access each one.
19 local protected_repos = {
25 -- All cookies will be authenticated based on this secret. Make it something
26 -- totally random and impossible to guess. It should be large.
27 local secret = "BE SURE TO CUSTOMIZE THIS STRING TO SOMETHING BIG AND RANDOM"
33 -- Authentication functions follow below. Swap these out if you want different authentication semantics.
37 -- Sets HTTP cookie headers based on post and sets up redirection.
38 function authenticate_post()
39 local redirect = validate_value("redirect", post["redirect"])
41 if redirect == nil then
48 local groups = gentoo_ldap_user_groups(post["username"], post["password"])
50 set_cookie("cgitauth", "")
52 -- One week expiration time
53 set_cookie("cgitauth", secure_value("gentoogroups", table.concat(groups, ","), os.time() + 604800))
61 -- Returns 1 if the cookie is valid and 0 if it is not.
62 function authenticate_cookie()
63 local required_group = protected_repos[cgit["repo"]]
64 if required_group == nil then
65 -- We return as valid if the repo is not protected.
69 local user_groups = validate_value("gentoogroups", get_cookie(http["cookie"], "cgitauth"))
70 if user_groups == nil or user_groups == "" then
73 for group in string.gmatch(user_groups, "[^,]+") do
74 if group == required_group then
81 -- Prints the html for the login form.
83 html("<h2>Gentoo LDAP Authentication Required</h2>")
84 html("<form method='post' action='")
85 html_attr(cgit["login"])
87 html("<input type='hidden' name='redirect' value='")
88 html_attr(secure_value("redirect", cgit["url"], 0))
91 html("<tr><td><label for='username'>Username:</label></td><td><input id='username' name='username' autofocus /></td></tr>")
92 html("<tr><td><label for='password'>Password:</label></td><td><input id='password' name='password' type='password' /></td></tr>")
93 html("<tr><td colspan='2'><input value='Login' type='submit' /></td></tr>")
94 html("</table></form>")
101 -- Gentoo LDAP support.
105 local lualdap = require("lualdap")
107 function gentoo_ldap_user_groups(username, password)
108 -- Ensure the user is alphanumeric
109 if username:match("%W") then
113 local who = "uid=" .. username .. ",ou=devs,dc=gentoo,dc=org"
115 local ldap, err = lualdap.open_simple {
116 uri = "ldap://ldap1.gentoo.org",
120 certfile = "/var/www/uwsgi/cgit/gentoo-ldap/star.gentoo.org.crt",
121 keyfile = "/var/www/uwsgi/cgit/gentoo-ldap/star.gentoo.org.key",
122 cacertfile = "/var/www/uwsgi/cgit/gentoo-ldap/ca.pem"
128 local group_suffix = ".group"
129 local group_suffix_len = group_suffix:len()
131 for dn, attribs in ldap:search { base = who, scope = "subtree" } do
132 local access = attribs["gentooAccess"]
133 if dn == who and access ~= nil then
134 for i, v in ipairs(access) do
136 if vlen > group_suffix_len and v:sub(-group_suffix_len) == group_suffix then
137 table.insert(groups, v:sub(1, vlen - group_suffix_len))
150 -- Wrapper around filter API, exposing the http table, the cgit table, and the post table to the above functions.
155 actions["authenticate-post"] = authenticate_post
156 actions["authenticate-cookie"] = authenticate_cookie
157 actions["body"] = body
159 function filter_open(...)
160 action = actions[select(1, ...)]
163 http["cookie"] = select(2, ...)
164 http["method"] = select(3, ...)
165 http["query"] = select(4, ...)
166 http["referer"] = select(5, ...)
167 http["path"] = select(6, ...)
168 http["host"] = select(7, ...)
169 http["https"] = select(8, ...)
172 cgit["repo"] = select(9, ...)
173 cgit["page"] = select(10, ...)
174 cgit["url"] = select(11, ...)
175 cgit["login"] = select(12, ...)
179 function filter_close()
183 function filter_write(str)
190 -- Utility functions based on keplerproject/wsapi.
194 function url_decode(str)
198 str = string.gsub(str, "+", " ")
199 str = string.gsub(str, "%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
200 str = string.gsub(str, "\r\n", "\n")
204 function url_encode(str)
208 str = string.gsub(str, "\n", "\r\n")
209 str = string.gsub(str, "([^%w ])", function(c) return string.format("%%%02X", string.byte(c)) end)
210 str = string.gsub(str, " ", "+")
214 function parse_qs(qs)
216 for key, val in string.gmatch(qs, "([^&=]+)=([^&=]*)&?") do
217 tab[url_decode(key)] = url_decode(val)
222 function get_cookie(cookies, name)
223 cookies = string.gsub(";" .. cookies .. ";", "%s*;%s*", ";")
224 return string.match(cookies, ";" .. name .. "=(.-);")
230 -- Cookie construction and validation helpers.
234 local crypto = require("crypto")
236 -- Returns value of cookie if cookie is valid. Otherwise returns nil.
237 function validate_value(expected_field, cookie)
245 if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then
249 for component in string.gmatch(cookie, "[^|]+") do
255 expiration = tonumber(component)
256 if expiration == nil then
269 if hmac == nil or hmac:len() == 0 then
273 -- Lua hashes strings, so these comparisons are time invariant.
274 if hmac ~= crypto.hmac.digest("sha256", field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt, secret) then
278 if expiration == -1 or (expiration ~= 0 and expiration <= os.time()) then
282 if url_decode(field) ~= expected_field then
286 return url_decode(value)
289 function secure_value(field, value, expiration)
290 if value == nil or value:len() <= 0 then
295 local salt = crypto.hex(crypto.rand.bytes(16))
296 value = url_encode(value)
297 field = url_encode(field)
298 authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt
299 authstr = authstr .. "|" .. crypto.hmac.digest("sha256", authstr, secret)
303 function set_cookie(cookie, value)
304 html("Set-Cookie: " .. cookie .. "=" .. value .. "; HttpOnly")
305 if http["https"] == "yes" or http["https"] == "on" or http["https"] == "1" then
311 function redirect_to(url)
312 html("Status: 302 Redirect\n")
313 html("Cache-Control: no-cache, no-store\n")
314 html("Location: " .. url .. "\n")
318 html("Status: 404 Not Found\n")
319 html("Cache-Control: no-cache, no-store\n\n")