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 -- A list of password protected repositories along with the users who can access them.
16 local protected_repos = {
17 glouglou = { laurent = true, jason = true },
18 qt = { jason = true, bob = true }
21 -- Please note that, in production, you'll want to replace this simple lookup
22 -- table with either a table of salted and hashed passwords (using something
23 -- smart like scrypt), or replace this table lookup with an external support,
24 -- such as consulting your system's pam / shadow system, or an external
25 -- database, or an external validating web service. For testing, or for
26 -- extremely low-security usage, you may be able, however, to get away with
27 -- compromising on hardcoding the passwords in cleartext, as we have done here.
29 jason = "secretpassword",
34 -- All cookies will be authenticated based on this secret. Make it something
35 -- totally random and impossible to guess. It should be large.
36 local secret = "BE SURE TO CUSTOMIZE THIS STRING TO SOMETHING BIG AND RANDOM"
42 -- Authentication functions follow below. Swap these out if you want different authentication semantics.
46 -- Sets HTTP cookie headers based on post and sets up redirection.
47 function authenticate_post()
48 local password = users[post["username"]]
49 local redirect = validate_value("redirect", post["redirect"])
51 if redirect == nil then
58 -- Lua hashes strings, so these comparisons are time invariant.
59 if password == nil or password ~= post["password"] then
60 set_cookie("cgitauth", "")
62 -- One week expiration time
63 local username = secure_value("username", post["username"], os.time() + 604800)
64 set_cookie("cgitauth", username)
72 -- Returns 1 if the cookie is valid and 0 if it is not.
73 function authenticate_cookie()
74 accepted_users = protected_repos[cgit["repo"]]
75 if accepted_users == nil then
76 -- We return as valid if the repo is not protected.
80 local username = validate_value("username", get_cookie(http["cookie"], "cgitauth"))
81 if username == nil or not accepted_users[username:lower()] then
88 -- Prints the html for the login form.
90 html("<h2>Authentication Required</h2>")
91 html("<form method='post' action='")
92 html_attr(cgit["login"])
94 html("<input type='hidden' name='redirect' value='")
95 html_attr(secure_value("redirect", cgit["url"], 0))
98 html("<tr><td><label for='username'>Username:</label></td><td><input id='username' name='username' autofocus /></td></tr>")
99 html("<tr><td><label for='password'>Password:</label></td><td><input id='password' name='password' type='password' /></td></tr>")
100 html("<tr><td colspan='2'><input value='Login' type='submit' /></td></tr>")
101 html("</table></form>")
110 -- Wrapper around filter API, exposing the http table, the cgit table, and the post table to the above functions.
115 actions["authenticate-post"] = authenticate_post
116 actions["authenticate-cookie"] = authenticate_cookie
117 actions["body"] = body
119 function filter_open(...)
120 action = actions[select(1, ...)]
123 http["cookie"] = select(2, ...)
124 http["method"] = select(3, ...)
125 http["query"] = select(4, ...)
126 http["referer"] = select(5, ...)
127 http["path"] = select(6, ...)
128 http["host"] = select(7, ...)
129 http["https"] = select(8, ...)
132 cgit["repo"] = select(9, ...)
133 cgit["page"] = select(10, ...)
134 cgit["url"] = select(11, ...)
135 cgit["login"] = select(12, ...)
139 function filter_close()
143 function filter_write(str)
150 -- Utility functions based on keplerproject/wsapi.
154 function url_decode(str)
158 str = string.gsub(str, "+", " ")
159 str = string.gsub(str, "%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
160 str = string.gsub(str, "\r\n", "\n")
164 function url_encode(str)
168 str = string.gsub(str, "\n", "\r\n")
169 str = string.gsub(str, "([^%w ])", function(c) return string.format("%%%02X", string.byte(c)) end)
170 str = string.gsub(str, " ", "+")
174 function parse_qs(qs)
176 for key, val in string.gmatch(qs, "([^&=]+)=([^&=]*)&?") do
177 tab[url_decode(key)] = url_decode(val)
182 function get_cookie(cookies, name)
183 cookies = string.gsub(";" .. cookies .. ";", "%s*;%s*", ";")
184 return url_decode(string.match(cookies, ";" .. name .. "=(.-);"))
190 -- Cookie construction and validation helpers.
194 local crypto = require("crypto")
196 -- Returns value of cookie if cookie is valid. Otherwise returns nil.
197 function validate_value(expected_field, cookie)
205 if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then
209 for component in string.gmatch(cookie, "[^|]+") do
215 expiration = tonumber(component)
216 if expiration == nil then
229 if hmac == nil or hmac:len() == 0 then
233 -- Lua hashes strings, so these comparisons are time invariant.
234 if hmac ~= crypto.hmac.digest("sha1", field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt, secret) then
238 if expiration == -1 or (expiration ~= 0 and expiration <= os.time()) then
242 if url_decode(field) ~= expected_field then
246 return url_decode(value)
249 function secure_value(field, value, expiration)
250 if value == nil or value:len() <= 0 then
255 local salt = crypto.hex(crypto.rand.bytes(16))
256 value = url_encode(value)
257 field = url_encode(field)
258 authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt
259 authstr = authstr .. "|" .. crypto.hmac.digest("sha1", authstr, secret)
263 function set_cookie(cookie, value)
264 html("Set-Cookie: " .. cookie .. "=" .. value .. "; HttpOnly")
265 if http["https"] == "yes" or http["https"] == "on" or http["https"] == "1" then
271 function redirect_to(url)
272 html("Status: 302 Redirect\n")
273 html("Cache-Control: no-cache, no-store\n")
274 html("Location: " .. url .. "\n")
278 html("Status: 404 Not Found\n")
279 html("Cache-Control: no-cache, no-store\n\n")