Annotate

mod_password_reset/mod_password_reset.lua @ 3344:0ce475235ae1

mod_password_reset: New module for self-service password resets via a web page
author Matthew Wild <mwild1@gmail.com>
date Tue, 02 Oct 2018 16:09:17 +0100
child 3352:f7668aee968a
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;
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 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
5 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
6 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
7 local dataforms_new = require "util.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
8 local tohtml = require "util.stanza".xml_escape
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 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
10
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 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
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 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
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 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
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 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
18 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
19
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local function apply_template(template, args)
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 return
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 template:gsub("{{([^}]*)}}", function (k)
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 if args[k] then
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return tohtml(args[k])
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 else
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 return k
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end)
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
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 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
32 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
33
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 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
35 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
36
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 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
38
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 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
40 module:log("warn", "Expired token: %s", token or "<none>");
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local template = assert(module:load_resource("password_reset/password_result.html")):read("*a");
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 return apply_template(template, { classes = "alert-danger", message = "This link has expired." })
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
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 template = assert(module:load_resource("password_reset/password_reset.html")):read("*a");
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return apply_template(template, { jid = reset_info.user.."@"..module.host, token = token });
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
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 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
52 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
53 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
54 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
55
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 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
57
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local template = assert(module:load_resource("password_reset/password_result.html")):read("*a");
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 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
61
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 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
63 return apply_template(template, { classes = "alert-danger", message = "This link has expired." })
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 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
67
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 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
69 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
70
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 return apply_template(template, { classes = "alert-success",
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 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
73 else
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:log("debug", "Resetting password failed: " .. tostring(err));
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 return apply_template(template, { classes = "alert-danger", message = "An unknown error has occurred." })
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
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 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
81 route = {
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 ["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
83 ["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
84 ["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
85 };
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
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 -- 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
89 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
90 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
91 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
92
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 { 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
94 { 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
95 };
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 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
98 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
99 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
100 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
101 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
102 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 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
104 end
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 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
107 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
108
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 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
110 return {
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 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
112 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
113 };
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 end
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 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
117 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
118 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
119 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
120 });
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 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
123 end);
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 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
126 "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
127 "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
128 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
129 "admin"
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 );
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131
0ce475235ae1 mod_password_reset: New module for self-service password resets via a web page
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 module:add_item("adhoc", adhoc_reset);