1 -- This script may be used with the auth-filter. Be sure to configure it as you wish.
5 -- <http://mkottman.github.io/luacrypto/>
11 -- Configure these variables for your settings.
15 local protected_repos = {
16 glouglou = { laurent = true, jason = true },
17 qt = { jason = true, bob = true }
21 jason = "secretpassword",
26 local secret = "BE SURE TO CUSTOMIZE THIS STRING TO SOMETHING BIG AND RANDOM"
32 -- Authentication functions follow below. Swap these out if you want different authentication semantics.
36 -- Sets HTTP cookie headers based on post
37 function authenticate_post()
38 local password = users[post["username"]]
39 -- TODO: Implement time invariant string comparison function to mitigate against timing attack.
40 if password == nil or password ~= post["password"] then
41 construct_cookie("", "cgitauth")
43 construct_cookie(post["username"], "cgitauth")
49 -- Returns 1 if the cookie is valid and 0 if it is not.
50 function authenticate_cookie()
51 accepted_users = protected_repos[cgit["repo"]]
52 if accepted_users == nil then
53 -- We return as valid if the repo is not protected.
57 local username = validate_cookie(get_cookie(http["cookie"], "cgitauth"))
58 if username == nil or not accepted_users[username] then
65 -- Prints the html for the login form.
67 html("<h2>Authentication Required</h2>")
68 html("<form method='post' action='")
69 html_attr(cgit["login"])
72 html("<tr><td><label for='username'>Username:</label></td><td><input id='username' name='username' autofocus /></td></tr>")
73 html("<tr><td><label for='password'>Password:</label></td><td><input id='password' name='password' type='password' /></td></tr>")
74 html("<tr><td colspan='2'><input value='Login' type='submit' /></td></tr>")
75 html("</table></form>")
83 -- Cookie construction and validation helpers.
87 local crypto = require("crypto")
89 -- Returns username of cookie if cookie is valid. Otherwise returns nil.
90 function validate_cookie(cookie)
97 if cookie:len() < 3 or cookie:sub(1, 1) == "|" then
101 for component in string.gmatch(cookie, "[^|]+") do
105 expiration = tonumber(component)
106 if expiration == nil then
119 if hmac == nil or hmac:len() == 0 then
123 -- TODO: implement time invariant comparison to prevent against timing attack.
124 if hmac ~= crypto.hmac.digest("sha1", username .. "|" .. tostring(expiration) .. "|" .. salt, secret) then
128 if expiration <= os.time() then
132 return username:lower()
135 function construct_cookie(username, cookie)
137 if username:len() > 0 then
138 -- One week expiration time
139 local expiration = os.time() + 604800
140 local salt = crypto.hex(crypto.rand.bytes(16))
142 authstr = username .. "|" .. tostring(expiration) .. "|" .. salt
143 authstr = authstr .. "|" .. crypto.hmac.digest("sha1", authstr, secret)
146 html("Set-Cookie: " .. cookie .. "=" .. authstr .. "; HttpOnly")
147 if http["https"] == "yes" or http["https"] == "on" or http["https"] == "1" then
155 -- Wrapper around filter API follows below, exposing the http table, the cgit table, and the post table to the above functions.
160 actions["authenticate-post"] = authenticate_post
161 actions["authenticate-cookie"] = authenticate_cookie
162 actions["body"] = body
164 function filter_open(...)
165 action = actions[select(1, ...)]
168 http["cookie"] = select(2, ...)
169 http["method"] = select(3, ...)
170 http["query"] = select(4, ...)
171 http["referer"] = select(5, ...)
172 http["path"] = select(6, ...)
173 http["host"] = select(7, ...)
174 http["https"] = select(8, ...)
177 cgit["repo"] = select(9, ...)
178 cgit["page"] = select(10, ...)
179 cgit["url"] = select(11, ...)
182 for _ in cgit["url"]:gfind("/") do
183 cgit["login"] = cgit["login"] .. "../"
185 cgit["login"] = cgit["login"] .. "?p=login"
189 function filter_close()
193 function filter_write(str)
200 -- Utility functions follow below, based on keplerproject/wsapi.
204 function url_decode(str)
208 str = string.gsub(str, "+", " ")
209 str = string.gsub(str, "%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
210 str = string.gsub(str, "\r\n", "\n")
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 url_decode(string.match(cookies, ";" .. name .. "=(.-);"))