X-Git-Url: https://gitweb.ps.run/ps-cgit/blobdiff_plain/d6e9200cc35411f3f27426b608bcfdef9348e6d3..034e3c7d56ba71ce281886fe8525b16d4559fac1:/filters/simple-authentication.lua
diff --git a/filters/simple-authentication.lua b/filters/simple-authentication.lua
index 4cd4983..23d3457 100644
--- a/filters/simple-authentication.lua
+++ b/filters/simple-authentication.lua
@@ -1,10 +1,15 @@
-- This script may be used with the auth-filter. Be sure to configure it as you wish.
--
-- Requirements:
--- luacrypto >= 0.3
---
+-- luaossl
+--
+-- luaposix
+--
--
-
+local sysstat = require("posix.sys.stat")
+local unistd = require("posix.unistd")
+local rand = require("openssl.rand")
+local hmac = require("openssl.hmac")
--
--
@@ -12,20 +17,22 @@
--
--
+-- A list of password protected repositories along with the users who can access them.
local protected_repos = {
glouglou = { laurent = true, jason = true },
qt = { jason = true, bob = true }
}
+-- A list of users and hashes, generated with `mkpasswd -m sha-512 -R 300000`.
local users = {
- jason = "secretpassword",
- laurent = "s3cr3t",
- bob = "ilikelua"
+ jason = "$6$rounds=300000$YYJct3n/o.ruYK$HhpSeuCuW1fJkpvMZOZzVizeLsBKcGA/aF2UPuV5v60JyH2MVSG6P511UMTj2F3H75.IT2HIlnvXzNb60FcZH1",
+ laurent = "$6$rounds=300000$dP0KNHwYb3JKigT$pN/LG7rWxQ4HniFtx5wKyJXBJUKP7R01zTNZ0qSK/aivw8ywGAOdfYiIQFqFhZFtVGvr11/7an.nesvm8iJUi.",
+ bob = "$6$rounds=300000$jCLCCt6LUpTz$PI1vvd1yaVYcCzqH8QAJFcJ60b6W/6sjcOsU7mAkNo7IE8FRGW1vkjF8I/T5jt/auv5ODLb1L4S2s.CAyZyUC"
}
-local secret = "BE SURE TO CUSTOMIZE THIS STRING TO SOMETHING BIG AND RANDOM"
-
-
+-- Set this to a path this script can write to for storing a persistent
+-- cookie secret, which should be guarded.
+local secret_filename = "/var/cache/cgit/auth-secret"
--
--
@@ -33,15 +40,27 @@ local secret = "BE SURE TO CUSTOMIZE THIS STRING TO SOMETHING BIG AND RANDOM"
--
--
--- Sets HTTP cookie headers based on post
+-- Sets HTTP cookie headers based on post and sets up redirection.
function authenticate_post()
- local password = users[post["username"]]
- -- TODO: Implement time invariant string comparison function to mitigate against timing attack.
- if password == nil or password ~= post["password"] then
- construct_cookie("", "cgitauth")
+ local hash = users[post["username"]]
+ local redirect = validate_value("redirect", post["redirect"])
+
+ if redirect == nil then
+ not_found()
+ return 0
+ end
+
+ redirect_to(redirect)
+
+ if hash == nil or hash ~= unistd.crypt(post["password"], hash) then
+ set_cookie("cgitauth", "")
else
- construct_cookie(post["username"], "cgitauth")
+ -- One week expiration time
+ local username = secure_value("username", post["username"], os.time() + 604800)
+ set_cookie("cgitauth", username)
end
+
+ html("\n")
return 0
end
@@ -54,8 +73,8 @@ function authenticate_cookie()
return 1
end
- local username = validate_cookie(get_cookie(http["cookie"], "cgitauth"))
- if username == nil or not accepted_users[username] then
+ local username = validate_value("username", get_cookie(http["cookie"], "cgitauth"))
+ if username == nil or not accepted_users[username:lower()] then
return 0
else
return 1
@@ -68,6 +87,9 @@ function body()
html("