Comparison

mod_register_web/mod_register_web.lua @ 653:c08b0e4b7b38

mod_register_web: Extremely rough web registration page, with captcha
author Matthew Wild <mwild1@gmail.com>
date Sun, 29 Apr 2012 17:36:31 +0100
child 746:03595194075a
comparison
equal deleted inserted replaced
652:3e6f43ab7e22 653:c08b0e4b7b38
1 local captcha_options = module:get_option("captcha_options", {});
2
3 function generate_captcha(display_options)
4 return (([[
5 <script type="text/javascript"
6 src="http://www.google.com/recaptcha/api/challenge?k=$$recaptcha_public_key$$">
7 </script>
8 <noscript>
9 <iframe src="http://www.google.com/recaptcha/api/noscript?k=$$recaptcha_public_key$$$$recaptcha_display_error$$"
10 height="300" width="500" frameborder="0"></iframe><br>
11 <textarea name="recaptcha_challenge_field" rows="3" cols="40">
12 </textarea>
13 <input type="hidden" name="recaptcha_response_field"
14 value="manual_challenge">
15 </noscript>
16 ]]):gsub("$$([^$]+)$%$", setmetatable({
17 recaptcha_display_error = display_options and display_options.recaptcha_error
18 and ("&error="..display_options.recaptcha_error) or "";
19 }, {
20 __index = function (t, k)
21 if captcha_options[k] then return captcha_options[k]; end
22 module:log("error", "Missing parameter from captcha_options: %s", k);
23 end })
24 ));
25 end
26
27 function generate_page(event, display_options)
28 local request = event.request;
29 return [[<!DOCTYPE html>
30 <html><body>
31 <h1>XMPP Account Registration</h1>
32 <form action="]]..request.path..[[" method="POST">]]
33 ..("<p>%s</p>\n"):format((display_options or {}).register_error or "")..
34 [[ <table>
35 <tr>
36 <td>Username:</td>
37 <td><input type="text" name="username">@server</td>
38 </tr>
39 <tr>
40 <td>Password:</td>
41 <td><input type="password" name="password"></td>
42 </tr>
43 <tr>
44 <td colspan='2'>]]..generate_captcha(display_options)..[[</td>
45 </tr>
46 </table>
47 <input type="submit" value="Register!">
48 </form>
49 </body></html>]];
50 end
51
52 function register_user(form)
53 if usermanager.user_exists(form.username, module.host) then
54 return nil, "user-exists";
55 end
56 return usermanager.create_user(form.username, form.password, module.host);
57 end
58
59 function generate_success(event, form)
60 return [[<!DOCTYPE html>
61 <html><body><p>Registration succeeded! Your account is <pre>]]
62 ..form.username.."@"..module.host..
63 [[</pre> - happy chatting!</p></body></html>]];
64 end
65
66 function generate_register_response(event, form, ok, err)
67 local message;
68 if ok then
69 return generate_success(event, form);
70 else
71 return generate_page(event, { register_error = err });
72 end
73 end
74
75 function handle_form(event)
76 local request, response = event.request, event.response;
77 local form = http.formdecode(request.body);
78 http.request("http://www.google.com/recaptcha/api/verify", {
79 body = http.formencode {
80 privatekey = captcha_options.recaptcha_private_key;
81 remoteip = request.conn:ip();
82 challenge = form.recaptcha_challenge_field;
83 response = form.recaptcha_response_field;
84 };
85 }, function (verify_result, code)
86 local verify_ok, verify_err = verify_result:match("^([^\n]+)\n([^\n]+)");
87 if verify_ok == "true" then
88 local register_ok, register_err = register_user(form);
89 response:send(generate_register_response(event, form, register_ok, register_err));
90 else
91 response:send(generate_page(event, { register_error = verify_err }));
92 end
93 end);
94 return true; -- Leave connection open until we respond above
95 end
96
97 module:provides("http", {
98 route = {
99 GET = generate_page;
100 POST = handle_form;
101 };
102 });