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 and sets up redirection.
37 function authenticate_post()
38 local password = users[post["username"]]
39 local redirect = validate_value(post["redirect"])
41 if redirect == nil then
48 -- Lua hashes strings, so these comparisons are time invariant.
49 if password == nil or password ~= post["password"] then
50 set_cookie("cgitauth", "")
52 -- One week expiration time
53 local username = secure_value(post["username"], os.time() + 604800)
54 set_cookie("cgitauth", username)
62 -- Returns 1 if the cookie is valid and 0 if it is not.
63 function authenticate_cookie()
64 accepted_users = protected_repos[cgit["repo"]]
65 if accepted_users == nil then
66 -- We return as valid if the repo is not protected.
70 local username = validate_value(get_cookie(http["cookie"], "cgitauth"))
71 if username == nil or not accepted_users[username:lower()] then
78 -- Prints the html for the login form.
80 html("<h2>Authentication Required</h2>")
81 html("<form method='post' action='")
82 html_attr(cgit["login"])
84 html("<input type='hidden' name='redirect' value='")
85 html_attr(secure_value(cgit["url"], 0))
88 html("<tr><td><label for='username'>Username:</label></td><td><input id='username' name='username' autofocus /></td></tr>")
89 html("<tr><td><label for='password'>Password:</label></td><td><input id='password' name='password' type='password' /></td></tr>")
90 html("<tr><td colspan='2'><input value='Login' type='submit' /></td></tr>")
91 html("</table></form>")
100 -- Wrapper around filter API, exposing the http table, the cgit table, and the post table to the above functions.
105 actions["authenticate-post"] = authenticate_post
106 actions["authenticate-cookie"] = authenticate_cookie
107 actions["body"] = body
109 function filter_open(...)
110 action = actions[select(1, ...)]
113 http["cookie"] = select(2, ...)
114 http["method"] = select(3, ...)
115 http["query"] = select(4, ...)
116 http["referer"] = select(5, ...)
117 http["path"] = select(6, ...)
118 http["host"] = select(7, ...)
119 http["https"] = select(8, ...)
122 cgit["repo"] = select(9, ...)
123 cgit["page"] = select(10, ...)
124 cgit["url"] = select(11, ...)
125 cgit["login"] = select(12, ...)
129 function filter_close()
133 function filter_write(str)
140 -- Utility functions based on keplerproject/wsapi.
144 function url_decode(str)
148 str = string.gsub(str, "+", " ")
149 str = string.gsub(str, "%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
150 str = string.gsub(str, "\r\n", "\n")
154 function url_encode(str)
158 str = string.gsub(str, "\n", "\r\n")
159 str = string.gsub(str, "([^%w ])", function (c) return string.format("%%%02X", string.byte(c)) end)
160 str = string.gsub(str, " ", "+")
164 function parse_qs(qs)
166 for key, val in string.gmatch(qs, "([^&=]+)=([^&=]*)&?") do
167 tab[url_decode(key)] = url_decode(val)
172 function get_cookie(cookies, name)
173 cookies = string.gsub(";" .. cookies .. ";", "%s*;%s*", ";")
174 return url_decode(string.match(cookies, ";" .. name .. "=(.-);"))
180 -- Cookie construction and validation helpers.
184 local crypto = require("crypto")
186 -- Returns value of cookie if cookie is valid. Otherwise returns nil.
187 function validate_value(cookie)
194 if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then
198 for component in string.gmatch(cookie, "[^|]+") do
202 expiration = tonumber(component)
203 if expiration == nil then
216 if hmac == nil or hmac:len() == 0 then
220 -- Lua hashes strings, so these comparisons are time invariant.
221 if hmac ~= crypto.hmac.digest("sha1", value .. "|" .. tostring(expiration) .. "|" .. salt, secret) then
225 if expiration ~= 0 and expiration <= os.time() then
229 return url_decode(value)
232 function secure_value(value, expiration)
233 if value == nil or value:len() <= 0 then
238 local salt = crypto.hex(crypto.rand.bytes(16))
239 value = url_encode(value)
240 authstr = value .. "|" .. tostring(expiration) .. "|" .. salt
241 authstr = authstr .. "|" .. crypto.hmac.digest("sha1", authstr, secret)
245 function set_cookie(cookie, value)
246 html("Set-Cookie: " .. cookie .. "=" .. value .. "; HttpOnly")
247 if http["https"] == "yes" or http["https"] == "on" or http["https"] == "1" then
253 function redirect_to(url)
254 html("Status: 302 Redirect\n")
255 html("Cache-Control: no-cache, no-store\n")
256 html("Location: " .. url .. "\n")
260 html("Status: 404 Not Found\n")
261 html("Cache-Control: no-cache, no-store\n\n")