Mercurial > prosody-modules
annotate mod_register_json/mod_register_json.lua @ 758:295ae44b8a01
mod_remote_roster: Replace use of core_post_stanza() with module:send()
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 01 Aug 2012 02:22:24 +0200 |
parents | c26652d055b5 |
children | 836e4e110c71 |
rev | line source |
---|---|
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
1 -- Expose a simple servlet to handle user registrations from web pages |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
2 -- via JSON. |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
3 -- |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
4 -- A Good chunk of the code is from mod_data_access.lua by Kim Alvefur |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
5 -- aka Zash. |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
6 |
529
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
7 local jid_prep = require "util.jid".prep |
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
8 local jid_split = require "util.jid".split |
723
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
9 local usermanager = usermanager |
529
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
10 local b64_decode = require "util.encodings".base64.decode |
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
11 local json_decode = require "util.json".decode |
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
12 local os_time = os.time |
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
13 local nodeprep = require "util.encodings".stringprep.nodeprep |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
14 |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
15 module:depends("http") |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
16 |
359
5d22ebcb9ec5
mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents:
358
diff
changeset
|
17 -- Pick up configuration. |
5d22ebcb9ec5
mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents:
358
diff
changeset
|
18 |
651
78a23a7dc613
mod_register_json: fixed typo, added https/http switch and default value to it.
Marco Cirillo <maranda@lightwitch.org>
parents:
648
diff
changeset
|
19 local secure = module:get_option_boolean("reg_servlet_secure", true) |
560
b62f5e38f865
mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents:
553
diff
changeset
|
20 local set_realm_name = module:get_option_string("reg_servlet_realm", "Restricted") |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
21 local base_path = module:get_option_string("reg_servlet_base", "/register_account/") |
560
b62f5e38f865
mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents:
553
diff
changeset
|
22 local throttle_time = module:get_option_number("reg_servlet_ttime", nil) |
550
d8143f627f9f
mod_register_json: modified code to employ get_option_set for true sets, and contains meta method
Marco Cirillo <maranda@lightwitch.org>
parents:
548
diff
changeset
|
23 local whitelist = module:get_option_set("reg_servlet_wl", {}) |
d8143f627f9f
mod_register_json: modified code to employ get_option_set for true sets, and contains meta method
Marco Cirillo <maranda@lightwitch.org>
parents:
548
diff
changeset
|
24 local blacklist = module:get_option_set("reg_servlet_bl", {}) |
529
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
25 local recent_ips = {} |
359
5d22ebcb9ec5
mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents:
358
diff
changeset
|
26 |
5d22ebcb9ec5
mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents:
358
diff
changeset
|
27 -- Begin |
5d22ebcb9ec5
mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents:
358
diff
changeset
|
28 |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
29 local function http_response(event, code, message, headers) |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
30 local response = event.response |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
31 |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
32 if headers then |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
33 for header, data in pairs(headers) do response.headers[header] = data end |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
34 end |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
35 |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
36 response.headers.content_type = "application/json" |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
37 response.status_code = code |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
38 response:send(message) |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
39 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
40 |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
41 local function handle_req(event) |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
42 local request = event.request |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
43 local body = request.body |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
44 |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
45 if request.method ~= "POST" then |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
46 return http_response(event, 405, "Bad method...", {["Allow"] = "POST"}) |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
47 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
48 if not request.headers["authorization"] then |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
49 return http_response(event, 401, "No... No...", {["WWW-Authenticate"]='Basic realm="'.. set_realm_name ..'"'}) |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
50 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
51 |
529
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
52 local user, password = b64_decode(request.headers.authorization:match("[^ ]*$") or ""):match("([^:]*):(.*)") |
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
53 user = jid_prep(user) |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
54 if not user or not password then return http_response(event, 400, "What's this..?") end |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
55 local user_node, user_host = jid_split(user) |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
56 if not hosts[user_host] then return http_response(event, 401, "Negative.") end |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
57 |
359
5d22ebcb9ec5
mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents:
358
diff
changeset
|
58 module:log("warn", "%s is authing to submit a new user registration data", user) |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
59 if not usermanager.test_password(user_node, user_host, password) then |
359
5d22ebcb9ec5
mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents:
358
diff
changeset
|
60 module:log("warn", "%s failed authentication", user) |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
61 return http_response(event, 401, "Who the hell are you?! Guards!") |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
62 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
63 |
529
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
64 local req_body |
361
146496a3be78
mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents:
360
diff
changeset
|
65 -- We check that what we have is valid JSON wise else we throw an error... |
146496a3be78
mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents:
360
diff
changeset
|
66 if not pcall(function() req_body = json_decode(body) end) then |
529
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
67 module:log("debug", "JSON data submitted for user registration by %s failed to Decode.", user) |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
68 return http_response(event, 400, "JSON Decoding failed.") |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
69 else |
363
72a5778579c5
mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents:
362
diff
changeset
|
70 -- Decode JSON data and check that all bits are there else throw an error |
529
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
71 req_body = json_decode(body) |
363
72a5778579c5
mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents:
362
diff
changeset
|
72 if req_body["username"] == nil or req_body["password"] == nil or req_body["host"] == nil or req_body["ip"] == nil then |
529
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
73 module:log("debug", "%s supplied an insufficent number of elements or wrong elements for the JSON registration", user) |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
74 return http_response(event, 400, "Invalid syntax.") |
363
72a5778579c5
mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents:
362
diff
changeset
|
75 end |
361
146496a3be78
mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents:
360
diff
changeset
|
76 -- Check if user is an admin of said host |
146496a3be78
mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents:
360
diff
changeset
|
77 if not usermanager.is_admin(user, req_body["host"]) then |
529
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
78 module:log("warn", "%s tried to submit registration data for %s but he's not an admin", user, req_body["host"]) |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
79 return http_response(event, 401, "I obey only to my masters... Have a nice day.") |
361
146496a3be78
mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents:
360
diff
changeset
|
80 else |
723
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
81 -- Blacklist can be checked here. |
550
d8143f627f9f
mod_register_json: modified code to employ get_option_set for true sets, and contains meta method
Marco Cirillo <maranda@lightwitch.org>
parents:
548
diff
changeset
|
82 if blacklist:contains(req_body["ip"]) then module:log("warn", "Attempt of reg. submission to the JSON servlet from blacklisted address: %s", req_body["ip"]) ; return http_response(403, "The specified address is blacklisted, sorry sorry.") end |
361
146496a3be78
mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents:
360
diff
changeset
|
83 |
146496a3be78
mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents:
360
diff
changeset
|
84 -- We first check if the supplied username for registration is already there. |
430
f0fafd19fd72
mod_register_json: changed pestered code to something less pestered. (added nodeprep)
Marco Cirillo <maranda@lightwitch.org>
parents:
429
diff
changeset
|
85 -- And nodeprep the username |
529
84e992f70ba3
mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents:
430
diff
changeset
|
86 local username = nodeprep(req_body["username"]) |
721
9080b0898b6f
mod_register_json: modify logic to prevent an unnecessary call to usermanager.
Marco Cirillo <maranda@lightwitch.org>
parents:
710
diff
changeset
|
87 if not username then |
9080b0898b6f
mod_register_json: modify logic to prevent an unnecessary call to usermanager.
Marco Cirillo <maranda@lightwitch.org>
parents:
710
diff
changeset
|
88 module:log("debug", "%s supplied an username containing invalid characters: %s", user, username) |
9080b0898b6f
mod_register_json: modify logic to prevent an unnecessary call to usermanager.
Marco Cirillo <maranda@lightwitch.org>
parents:
710
diff
changeset
|
89 return http_response(event, 406, "Supplied username contains invalid characters, see RFC 6122.") |
9080b0898b6f
mod_register_json: modify logic to prevent an unnecessary call to usermanager.
Marco Cirillo <maranda@lightwitch.org>
parents:
710
diff
changeset
|
90 else |
9080b0898b6f
mod_register_json: modify logic to prevent an unnecessary call to usermanager.
Marco Cirillo <maranda@lightwitch.org>
parents:
710
diff
changeset
|
91 if not usermanager.user_exists(username, req_body["host"]) then |
723
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
92 -- if username fails to register successive requests shouldn't be throttled until one is successful. |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
93 if throttle_time and not whitelist:contains(req_body["ip"]) then |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
94 if not recent_ips[req_body["ip"]] then |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
95 recent_ips[req_body["ip"]] = os_time() |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
96 else |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
97 if os_time() - recent_ips[req_body["ip"]] < throttle_time then |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
98 recent_ips[req_body["ip"]] = os_time() |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
99 module:log("warn", "JSON Registration request from %s has been throttled.", req_body["ip"]) |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
100 return http_response(event, 503, "Woah... How many users you want to register..? Request throttled, wait a bit and try again.") |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
101 end |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
102 recent_ips[req_body["ip"]] = os_time() |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
103 end |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
104 end |
c26652d055b5
mod_register_json: moved throttling logic so that if there's a failure during nick registration the user can retry and referenced usermanager from prosody's _G instead of req. it.
Marco Cirillo <maranda@lightwitch.org>
parents:
721
diff
changeset
|
105 |
553
7310ceb7564f
mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents:
551
diff
changeset
|
106 local ok, error = usermanager.create_user(username, req_body["password"], req_body["host"]) |
7310ceb7564f
mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents:
551
diff
changeset
|
107 if ok then |
7310ceb7564f
mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents:
551
diff
changeset
|
108 hosts[req_body["host"]].events.fire_event("user-registered", { username = username, host = req_body["host"], source = "mod_register_json", session = { ip = req_body["ip"] } }) |
7310ceb7564f
mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents:
551
diff
changeset
|
109 module:log("debug", "%s registration data submission for %s@%s is successful", user, username, req_body["host"]) |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
110 return http_response(event, 200, "Done.") |
553
7310ceb7564f
mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents:
551
diff
changeset
|
111 else |
7310ceb7564f
mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents:
551
diff
changeset
|
112 module:log("error", "user creation failed: "..error) |
651
78a23a7dc613
mod_register_json: fixed typo, added https/http switch and default value to it.
Marco Cirillo <maranda@lightwitch.org>
parents:
648
diff
changeset
|
113 return http_response(event, 500, "Encountered server error while creating the user: "..error) |
553
7310ceb7564f
mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents:
551
diff
changeset
|
114 end |
721
9080b0898b6f
mod_register_json: modify logic to prevent an unnecessary call to usermanager.
Marco Cirillo <maranda@lightwitch.org>
parents:
710
diff
changeset
|
115 else |
9080b0898b6f
mod_register_json: modify logic to prevent an unnecessary call to usermanager.
Marco Cirillo <maranda@lightwitch.org>
parents:
710
diff
changeset
|
116 module:log("debug", "%s registration data submission for %s failed (user already exists)", user, username) |
9080b0898b6f
mod_register_json: modify logic to prevent an unnecessary call to usermanager.
Marco Cirillo <maranda@lightwitch.org>
parents:
710
diff
changeset
|
117 return http_response(event, 409, "User already exists.") |
429
ea6641deec12
mod_register_json: added check for invalid characters in the username.
Marco Cirillo <maranda@lightwitch.org>
parents:
370
diff
changeset
|
118 end |
361
146496a3be78
mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents:
360
diff
changeset
|
119 end |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
120 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
121 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
122 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
123 |
359
5d22ebcb9ec5
mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents:
358
diff
changeset
|
124 -- Set it up! |
560
b62f5e38f865
mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents:
553
diff
changeset
|
125 |
655
81d269f97ea2
mod_register_json: revert change, it's not needed (providing http provides https as well)
Marco Cirillo <maranda@lightwitch.org>
parents:
651
diff
changeset
|
126 module:provides("http", { |
648
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
127 default_path = base_path, |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
128 route = { |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
129 ["GET /"] = handle_req, |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
130 ["POST /"] = handle_req |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
131 } |
6f0e0d6790a7
mod_register_json: updated to current HTTP API.
Marco Cirillo <maranda@lightwitch.org>
parents:
607
diff
changeset
|
132 }) |