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://github.com/luaposix/luaposix>
9 local sysstat = require("posix.sys.stat")
10 local unistd = require("posix.unistd")
11 local crypto = require("crypto")
16 -- Configure these variables for your settings.
20 -- A list of password protected repositories along with the users who can access them.
21 local protected_repos = {
22 glouglou = { laurent = true, jason = true },
23 qt = { jason = true, bob = true }
26 -- Please note that, in production, you'll want to replace this simple lookup
27 -- table with either a table of salted and hashed passwords (using something
28 -- smart like scrypt), or replace this table lookup with an external support,
29 -- such as consulting your system's pam / shadow system, or an external
30 -- database, or an external validating web service. For testing, or for
31 -- extremely low-security usage, you may be able, however, to get away with
32 -- compromising on hardcoding the passwords in cleartext, as we have done here.
34 jason = "secretpassword",
39 -- Set this to a path this script can write to for storing a persistent
40 -- cookie secret, which should be guarded.
41 local secret_filename = "/var/cache/cgit/auth-secret"
45 -- Authentication functions follow below. Swap these out if you want different authentication semantics.
49 -- Sets HTTP cookie headers based on post and sets up redirection.
50 function authenticate_post()
51 local password = users[post["username"]]
52 local redirect = validate_value("redirect", post["redirect"])
54 if redirect == nil then
61 -- Lua hashes strings, so these comparisons are time invariant.
62 if password == nil or password ~= post["password"] then
63 set_cookie("cgitauth", "")
65 -- One week expiration time
66 local username = secure_value("username", post["username"], os.time() + 604800)
67 set_cookie("cgitauth", username)
75 -- Returns 1 if the cookie is valid and 0 if it is not.
76 function authenticate_cookie()
77 accepted_users = protected_repos[cgit["repo"]]
78 if accepted_users == nil then
79 -- We return as valid if the repo is not protected.
83 local username = validate_value("username", get_cookie(http["cookie"], "cgitauth"))
84 if username == nil or not accepted_users[username:lower()] then
91 -- Prints the html for the login form.
93 html("<h2>Authentication Required</h2>")
94 html("<form method='post' action='")
95 html_attr(cgit["login"])
97 html("<input type='hidden' name='redirect' value='")
98 html_attr(secure_value("redirect", cgit["url"], 0))
101 html("<tr><td><label for='username'>Username:</label></td><td><input id='username' name='username' autofocus /></td></tr>")
102 html("<tr><td><label for='password'>Password:</label></td><td><input id='password' name='password' type='password' /></td></tr>")
103 html("<tr><td colspan='2'><input value='Login' type='submit' /></td></tr>")
104 html("</table></form>")
113 -- Wrapper around filter API, exposing the http table, the cgit table, and the post table to the above functions.
118 actions["authenticate-post"] = authenticate_post
119 actions["authenticate-cookie"] = authenticate_cookie
120 actions["body"] = body
122 function filter_open(...)
123 action = actions[select(1, ...)]
126 http["cookie"] = select(2, ...)
127 http["method"] = select(3, ...)
128 http["query"] = select(4, ...)
129 http["referer"] = select(5, ...)
130 http["path"] = select(6, ...)
131 http["host"] = select(7, ...)
132 http["https"] = select(8, ...)
135 cgit["repo"] = select(9, ...)
136 cgit["page"] = select(10, ...)
137 cgit["url"] = select(11, ...)
138 cgit["login"] = select(12, ...)
142 function filter_close()
146 function filter_write(str)
153 -- Utility functions based on keplerproject/wsapi.
157 function url_decode(str)
161 str = string.gsub(str, "+", " ")
162 str = string.gsub(str, "%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
163 str = string.gsub(str, "\r\n", "\n")
167 function url_encode(str)
171 str = string.gsub(str, "\n", "\r\n")
172 str = string.gsub(str, "([^%w ])", function(c) return string.format("%%%02X", string.byte(c)) end)
173 str = string.gsub(str, " ", "+")
177 function parse_qs(qs)
179 for key, val in string.gmatch(qs, "([^&=]+)=([^&=]*)&?") do
180 tab[url_decode(key)] = url_decode(val)
185 function get_cookie(cookies, name)
186 cookies = string.gsub(";" .. cookies .. ";", "%s*;%s*", ";")
187 return url_decode(string.match(cookies, ";" .. name .. "=(.-);"))
193 -- Cookie construction and validation helpers.
199 -- Loads a secret from a file, creates a secret, or returns one from memory.
200 function get_secret()
201 if secret ~= nil then
204 local secret_file = io.open(secret_filename, "r")
205 if secret_file == nil then
206 local old_umask = sysstat.umask(63)
207 local temporary_filename = secret_filename .. ".tmp." .. crypto.hex(crypto.rand.bytes(16))
208 local temporary_file = io.open(temporary_filename, "w")
209 if temporary_file == nil then
212 temporary_file:write(crypto.hex(crypto.rand.bytes(32)))
213 temporary_file:close()
214 unistd.link(temporary_filename, secret_filename) -- Intentionally fails in the case that another process is doing the same.
215 unistd.unlink(temporary_filename)
216 sysstat.umask(old_umask)
217 secret_file = io.open(secret_filename, "r")
219 if secret_file == nil then
222 secret = secret_file:read()
224 if secret:len() ~= 64 then
230 -- Returns value of cookie if cookie is valid. Otherwise returns nil.
231 function validate_value(expected_field, cookie)
239 if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then
243 for component in string.gmatch(cookie, "[^|]+") do
249 expiration = tonumber(component)
250 if expiration == nil then
263 if hmac == nil or hmac:len() == 0 then
267 -- Lua hashes strings, so these comparisons are time invariant.
268 if hmac ~= crypto.hmac.digest("sha256", field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt, get_secret()) then
272 if expiration == -1 or (expiration ~= 0 and expiration <= os.time()) then
276 if url_decode(field) ~= expected_field then
280 return url_decode(value)
283 function secure_value(field, value, expiration)
284 if value == nil or value:len() <= 0 then
289 local salt = crypto.hex(crypto.rand.bytes(16))
290 value = url_encode(value)
291 field = url_encode(field)
292 authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt
293 authstr = authstr .. "|" .. crypto.hmac.digest("sha256", authstr, get_secret())
297 function set_cookie(cookie, value)
298 html("Set-Cookie: " .. cookie .. "=" .. value .. "; HttpOnly")
299 if http["https"] == "yes" or http["https"] == "on" or http["https"] == "1" then
305 function redirect_to(url)
306 html("Status: 302 Redirect\n")
307 html("Cache-Control: no-cache, no-store\n")
308 html("Location: " .. url .. "\n")
312 html("Status: 404 Not Found\n")
313 html("Cache-Control: no-cache, no-store\n\n")