annotate mod_password_reset/mod_password_reset.lua @ 3353:e8d6f602f382

mod_password_reset: Improve error reporting when setting a password fails
author Matthew Wild <mwild1@gmail.com>
date Tue, 09 Oct 2018 14:10:42 +0100
parents f7668aee968a
children 3bc68057f232
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3344
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local adhoc_new = module:require "adhoc".new;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local adhoc_simple_form = require "util.adhoc".new_simple_form;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local new_token = require "util.id".long;
3353
e8d6f602f382 mod_password_reset: Improve error reporting when setting a password fails
Matthew Wild <mwild1@gmail.com>
parents: 3352
diff changeset
4 local new_error_id = require "util.id".short;
3344
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local jid_prepped_split = require "util.jid".prepped_split;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local http_formdecode = require "net.http".formdecode;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local usermanager = require "core.usermanager";
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local dataforms_new = require "util.dataforms".new;
3352
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
9 local st = require "util.stanza";
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
10 local apply_template = require"util.interpolation".new("%b{}", st.xml_escape);
3344
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local tostring = tostring;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local reset_tokens = module:open_store();
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local max_token_age = module:get_option_number("password_reset_validity", 86400);
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local serve = module:depends"http_files".serve;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 module:depends"adhoc";
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 module:depends"http";
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
3352
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
22 local form_template = assert(module:load_resource("password_reset/password_reset.html")):read("*a");
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
23 local result_template = assert(module:load_resource("password_reset/password_result.html")):read("*a");
3344
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 function generate_page(event)
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local request, response = event.request, event.response;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local token = request.url.query;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local reset_info = token and reset_tokens:get(token);
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 response.headers.content_type = "text/html; charset=utf-8";
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if not reset_info or os.difftime(os.time(), reset_info.generated_at) > max_token_age then
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 module:log("warn", "Expired token: %s", token or "<none>");
3352
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
35 return apply_template(result_template, { classes = "alert-danger", message = "This link has expired." })
3344
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
3352
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
38 return apply_template(form_template, {
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
39 jid = reset_info.user.."@"..module.host;
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
40 token = token;
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
41 });
3344
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 function handle_form(event)
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local request, response = event.request, event.response;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local form_data = http_formdecode(request.body);
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local password, token = form_data["password"], form_data["token"];
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local reset_info = reset_tokens:get(token);
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 response.headers.content_type = "text/html; charset=utf-8";
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if not reset_info or os.difftime(os.time(), reset_info.generated_at) > max_token_age then
3352
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
54 return apply_template(result_template, { classes = "alert-danger", message = "This link has expired." })
3344
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 local ok, err = usermanager.set_password(reset_info.user, password, module.host);
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 if ok then
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 reset_tokens:set(token, nil);
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
3352
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
62 return apply_template(result_template, { classes = "alert-success",
3344
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 message = "Your password has been updated! Happy chatting :)" })
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 else
3353
e8d6f602f382 mod_password_reset: Improve error reporting when setting a password fails
Matthew Wild <mwild1@gmail.com>
parents: 3352
diff changeset
65 local error_id = new_error_id();
e8d6f602f382 mod_password_reset: Improve error reporting when setting a password fails
Matthew Wild <mwild1@gmail.com>
parents: 3352
diff changeset
66 module:log("warn", "Resetting password for %s failed: %s [%s]", reset_info.user, err, error_id);
3352
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
67 return apply_template(result_template, {
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
68 classes = "alert-danger";
3353
e8d6f602f382 mod_password_reset: Improve error reporting when setting a password fails
Matthew Wild <mwild1@gmail.com>
parents: 3352
diff changeset
69 message = "An unknown error has occurred. Please contact your administrator and quote error id '"..error_id.."'";
3352
f7668aee968a mod_password_reset: Switch to util.interpolation (our standard template library)
Matthew Wild <mwild1@gmail.com>
parents: 3344
diff changeset
70 })
3344
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 module:provides("http", {
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 route = {
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 ["GET /bootstrap.min.css"] = serve(module:get_directory() .. "/password_reset/bootstrap.min.css");
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 ["GET /reset"] = generate_page;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 ["POST /reset"] = handle_form;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 };
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 });
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 -- Changing a user's password
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local reset_password_layout = dataforms_new{
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 title = "Generate password reset link";
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 instructions = "Please enter the details of the user who needs a reset link.";
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/adhoc/mod_password_reset" };
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 { name = "accountjid", type = "jid-single", required = true, label = "JID" };
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 };
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 local reset_command_handler = adhoc_simple_form(reset_password_layout, function (data, errors)
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 if errors then
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 local errmsg = {};
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 for name, text in pairs(errors) do
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 errmsg[#errmsg + 1] = name .. ": " .. text;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 return { status = "completed", error = { message = table.concat(errmsg, "\n") } };
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 local jid = data.accountjid;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 local user, host = jid_prepped_split(jid);
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 if host ~= module.host then
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 return {
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 status = "completed";
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 error = { message = "You may only generate password reset links for users on "..module.host.."." };
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 };
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 local token = new_token();
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 reset_tokens:set(token, {
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 generated_at = os.time();
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 user = user;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 });
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 return { info = module:http_url() .. "/reset?" .. token, status = "completed" };
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 end);
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 local adhoc_reset = adhoc_new(
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 "Generate password reset link",
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 "password_reset",
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 reset_command_handler,
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 "admin"
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 );
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 module:add_item("adhoc", adhoc_reset);