changeset 3352:f7668aee968a

mod_password_reset: Switch to util.interpolation (our standard template library)
author Matthew Wild <mwild1@gmail.com>
date Tue, 09 Oct 2018 14:09:54 +0100
parents 662f2722f745
children e8d6f602f382
files mod_password_reset/mod_password_reset.lua mod_password_reset/password_reset/password_reset.html mod_password_reset/password_reset/password_result.html
diffstat 3 files changed, 21 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/mod_password_reset/mod_password_reset.lua	Tue Oct 09 14:03:00 2018 +0100
+++ b/mod_password_reset/mod_password_reset.lua	Tue Oct 09 14:09:54 2018 +0100
@@ -5,7 +5,8 @@
 local http_formdecode = require "net.http".formdecode;
 local usermanager = require "core.usermanager";
 local dataforms_new = require "util.dataforms".new;
-local tohtml = require "util.stanza".xml_escape
+local st = require "util.stanza";
+local apply_template = require"util.interpolation".new("%b{}", st.xml_escape);
 local tostring = tostring;
 
 local reset_tokens = module:open_store();
@@ -17,16 +18,8 @@
 module:depends"adhoc";
 module:depends"http";
 
-local function apply_template(template, args)
-	return
-		template:gsub("{{([^}]*)}}", function (k)
-			if args[k] then
-				return tohtml(args[k])
-			else
-				return k
-			end
-		end)
-end
+local form_template = assert(module:load_resource("password_reset/password_reset.html")):read("*a");
+local result_template = assert(module:load_resource("password_reset/password_result.html")):read("*a");
 
 function generate_page(event)
 	local request, response = event.request, event.response;
@@ -38,14 +31,13 @@
 
 	if not reset_info or os.difftime(os.time(), reset_info.generated_at) > max_token_age then
 		module:log("warn", "Expired token: %s", token or "<none>");
-		local template = assert(module:load_resource("password_reset/password_result.html")):read("*a");
-
-		return apply_template(template, { classes = "alert-danger", message = "This link has expired." })
+		return apply_template(result_template, { classes = "alert-danger", message = "This link has expired." })
 	end
 
-	local template = assert(module:load_resource("password_reset/password_reset.html")):read("*a");
-
-	return apply_template(template, { jid = reset_info.user.."@"..module.host, token = token });
+	return apply_template(form_template, {
+		jid = reset_info.user.."@"..module.host;
+		token = token;
+	});
 end
 
 function handle_form(event)
@@ -55,12 +47,10 @@
 
 	local reset_info = reset_tokens:get(token);
 
-	local template = assert(module:load_resource("password_reset/password_result.html")):read("*a");
-
 	response.headers.content_type = "text/html; charset=utf-8";
 
 	if not reset_info or os.difftime(os.time(), reset_info.generated_at) > max_token_age then
-		return apply_template(template, { classes = "alert-danger", message = "This link has expired." })
+		return apply_template(result_template, { classes = "alert-danger", message = "This link has expired." })
 	end
 
 	local ok, err = usermanager.set_password(reset_info.user, password, module.host);
@@ -68,12 +58,14 @@
 	if ok then
 		reset_tokens:set(token, nil);
 
-		return apply_template(template, { classes = "alert-success",
+		return apply_template(result_template, { classes = "alert-success",
 			message = "Your password has been updated! Happy chatting :)" })
 	else
 		module:log("debug", "Resetting password failed: " .. tostring(err));
-
-		return apply_template(template, { classes = "alert-danger", message = "An unknown error has occurred." })
+		return apply_template(result_template, {
+			classes = "alert-danger";
+			message = "An unknown error has occurred.";
+		})
 	end
 end
 
--- a/mod_password_reset/password_reset/password_reset.html	Tue Oct 09 14:03:00 2018 +0100
+++ b/mod_password_reset/password_reset/password_reset.html	Tue Oct 09 14:09:54 2018 +0100
@@ -7,6 +7,9 @@
 </head>
 <body>
 	<div class="container col-md-4 col-md-offset-4" style="margin-top: 100px">
+		{message&
+		<div class="alert {classes}">{message}</div>
+		}
 		<div class="panel panel-default">
 			<div class="panel-heading">
 				<div class="panel-title">Reset password</div>
@@ -20,7 +23,7 @@
 					<div class="form-group" style="margin-right: 0px;">
 						<label for="user" class="col-sm-2 control-label">Account:</label>
 						<div class="input-group col-sm-10">
-							<input type="text" name="user" class="form-control" disabled placeholder="{{jid}}">
+							<input type="text" name="user" class="form-control" disabled placeholder="{jid}">
 						</div>
 					</div>
 					<div class="form-group" style="margin-right: 0px;">
@@ -29,7 +32,7 @@
 							<input type="password" name="password" class="form-control" placeholder="password">
 						</div>
 					</div>
-					<input type="hidden" name="token" value="{{token}}">
+					<input type="hidden" name="token" value="{token}">
 					<button type="submit" class="btn btn-primary btn-lg">Reset</button>
 				</form>
 			</div>
--- a/mod_password_reset/password_reset/password_result.html	Tue Oct 09 14:03:00 2018 +0100
+++ b/mod_password_reset/password_reset/password_result.html	Tue Oct 09 14:09:54 2018 +0100
@@ -9,7 +9,7 @@
 	<div class="container">
 		<h1>Password reset</h1>
 		
-		<div class="alert {{classes}}">{{message}}</div>
+		<div class="alert {classes}">{message}</div>
 	</div>
 </body>
 </html>