diff src/get_password.html.luan @ 0:dfc36e7ed22c

init
author Vadim Filimonov <fffilimonov@yandex.ru>
date Thu, 12 May 2022 13:51:59 +0400
parents
children a6be8817c05b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/get_password.html.luan	Thu May 12 13:51:59 2022 +0400
@@ -0,0 +1,108 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Io = require "luan:Io.luan"
+local output_of = Io.output_of or error()
+local Http = require "luan:http/Http.luan"
+local Mail = require "luan:mail/Mail.luan"
+local Shared = require "site:/lib/Shared.luan"
+local head = Shared.head or error()
+local header = Shared.header or error()
+local config = Shared.config or error()
+local new_password = Shared.new_password or error()
+local get_raw_config = Shared.get_raw_config or error()
+local save_raw_config = Shared.save_raw_config or error()
+
+
+local send_mail = Mail.sender{
+	host = "smtpcorp.com"
+	username = "smtp@luan.software"
+	password = "luanhost"
+	port = 2525
+}.send
+
+local function handle(email)
+	local change = Http.request.parameters.change ~= nil
+	local password = config.users[email]
+	if password == nil or change then
+		password = new_password()
+		local raw_config = get_raw_config()
+		raw_config.users[email] = password
+		save_raw_config(raw_config)
+	end
+	local function body()
+%>
+Your password is "<%=password%>".
+<%
+	end_function
+	send_mail{
+		From = "Mercurial Hosting <hg@reactionary.software>"
+		To = email
+		Subject = "Your Password"
+		body = output_of(body)
+	}
+	return nil
+end
+
+local function response(content)
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>Mercurial - Get Password</title>
+	</head>
+	<body>
+<%		header() %>
+		<div content>
+			<h1>Get Password</h1>
+			<%=content%>
+		</div>
+	</body>
+</html>
+<%
+end
+
+return function()
+	Io.stdout = Http.response.text_writer()
+	local email = Http.request.parameters.email
+	if email ~= nil then
+		local error_msg = handle(email)
+		if error_msg == nil then
+			response([[<p>Your password has been emailed to <b>]]..email..[[</b>.  Use your email and password for authentication.</p>]])
+		else
+			response([[<p error>]]..error_msg..[[</p>]])
+		end
+		return
+	end
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>Mercurial - Get Password</title>
+	</head>
+	<body>
+<%		header() %>
+		<div content>
+			<h1>Get Password</h1>
+
+			<p>This site uses authentication for access.  For authentication, your email address is your user name, and your password will be emailed to you.</p>
+
+			<p>This page is used to register, to get your password if you forgot it, or to change your password.</p>
+
+			<hr>
+			<form>
+				<p>
+					<label prompt>Your email address</label>
+					<input type=email name=email required>
+					<label prompt clickable><input type=checkbox name=change> change password</label>
+				</p>
+				<p>
+					<input type=submit value="Get Password">
+				</p>
+			</form>
+		</div>
+	</body>
+</html>
+<%
+end