changeset 1210:3db95cc00d09

luanhost - add digest authentication
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 09 Mar 2018 15:21:12 -0700
parents cff4a7d778a6
children f67628bd3582
files src/luan/host/Util.luan
diffstat 1 files changed, 26 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
diff -r cff4a7d778a6 -r 3db95cc00d09 src/luan/host/Util.luan
--- a/src/luan/host/Util.luan	Fri Mar 09 14:56:18 2018 -0700
+++ b/src/luan/host/Util.luan	Fri Mar 09 15:21:12 2018 -0700
@@ -1,10 +1,14 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local do_file = Luan.do_file or error()
+local ipairs = Luan.ipairs or error()
 local stringify = Luan.stringify or error()
 local Io = require "luan:Io.luan"
 local String = require "luan:String.luan"
 local lower = String.lower or error()
+local format = String.format or error()
+local Binary = require "luan:Binary.luan"
+local bytes = Binary.byte or error()
 local Hosting = require "luan:host/Hosting.luan"
 
 java()
@@ -19,11 +23,7 @@
 	return do_file(Hosting.sites_dir..domain.."/info.luan").password or error()
 end
 
-function Util.write_password(domain,password)
-	local dir = Hosting.sites_dir..lower(domain)
-	Io.schemes.file(dir.."/info.luan").write_text( %>
-return <%= stringify{password=password} %>
-<%	)
+local function basic_authentication(dir,password)
 	local sha1 = MessageDigest.getInstance("SHA1").digest(password.getBytes())
 	local encoded = Base64.getEncoder().encodeToString(sha1)
 	Io.schemes.file(dir.."/password.nginx").write_text( %>
@@ -31,4 +31,25 @@
 <%	)
 end
 
+local function digest_authentication(dir,password)
+	local s = "admin:Restricted:"..password
+	local md5 = MessageDigest.getInstance("MD5").digest(s.getBytes())
+	md5 = {bytes(md5,1,#md5)}
+	local encoded = ""
+	for _, n in ipairs(md5) do
+		encoded = encoded..format("%02x",n)
+	end
+	Io.schemes.file(dir.."/password.nginx").write_text( %>
+admin:Restricted:<%=encoded%>
+<%	)
+end
+
+function Util.write_password(domain,password)
+	local dir = Hosting.sites_dir..lower(domain)
+	Io.schemes.file(dir.."/info.luan").write_text( %>
+return <%= stringify{password=password} %>
+<%	)
+	digest_authentication(dir,password)
+end
+
 return Util