Software /
code /
prosody-modules
Annotate
mod_register_json/mod_register_json.lua @ 356:5dacfbc9cd86
mod_register_json: Fixed missing close quote.
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Tue, 12 Apr 2011 15:36:30 +0000 |
parent | 355:a5da789b2e7d |
child | 357:59345fd38ad9 |
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 |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
7 local usermanager = require "core.usermanager"; |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
8 local b64_decode = require "util.encodings".base64.decode; |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
9 local json_decode = require "util.json".decode; |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
10 |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
11 module.host = "*" -- HTTP/BOSH Servlets need to be loaded globally. |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
12 |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
13 local set_realm_name = module:get_option("reg_servlet_realm") or "Restricted"; |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
14 |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
15 local function http_response(code, message, extra_headers) |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
16 local response = { |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
17 status = code .. " " .. message; |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
18 body = message .. "\n"; } |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
19 if extra_headers then response.headers = extra_headers; end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
20 return response |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
21 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
22 |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
23 local function handle_req(method, body, request) |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
24 if request.method ~= "POST" then |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
25 return http_response(405, "Bad method...", {["Allow"] = "POST"}); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
26 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
27 if not request.headers["authorization"] then |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
28 return http_response(401, "No... No...", |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
29 {["WWW-Authenticate"]='Basic realm="'.. set_realm_name ..'"'}) |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
30 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
31 |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
32 local user, password = b64_decode(request.headers.authorization |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
33 :match("[^ ]*$") or ""):match("([^:]*):(.*)"); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
34 user = jid_prep(user); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
35 if not user or not password then return http_response(400, "What's this..?"); end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
36 local user_node, user_host = jid_split(user) |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
37 if not hosts[user_host] then return http_response(401, "Negative."); end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
38 |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
39 module:log("debug", "%s is authing to submit a new user registration data", user) |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
40 if not usermanager.test_password(user_node, user_host, password) then |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
41 module:log("debug", "%s failed authentication", user) |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
42 return http_response(401, "Who the hell are you?! Guards!"); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
43 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
44 |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
45 local req_body; pcall(function() req_body = json.decode(body) end); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
46 -- Check if user is an admin of said host |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
47 if not usermanager.is_admin(user, req_body["host"]) then |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
48 module:log("debug", "%s tried to submit registration data for %s but he's not an admin", user, req_body["host"]) |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
49 return http_response(401, "I obey only to my masters... Have a nice day."); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
50 else |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
51 -- Various sanity checks. |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
52 if req_body == nil then module:log("debug", "JSON data submitted for user registration by %s failed to Decode.", user); return http_response(400, "JSON Decoding failed."); end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
53 if req_body["password"]:match("%s") then module:log("debug", "Password submitted for user registration by %s contained spaces.", user); return http_response(400, "Supplied user passwords can't contain spaces."); end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
54 -- We first check if the supplied username for registration is already there. |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
55 if not usermanager.user_exists(req_body["username"], req_body["host"]) then |
356
5dacfbc9cd86
mod_register_json: Fixed missing close quote.
Marco Cirillo <maranda@lightwitch.org>
parents:
355
diff
changeset
|
56 usermanager.create_user(req_body["username"], req_body["password"], req_body["host"]); |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
57 module:log("debug", "%s registration data submission for %s is successful", user, req_body["user"]); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
58 return http_response(200, "Done."); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
59 else |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
60 module:log("debug", "%s registration data submission for %s failed (user already exists)", user, req_body["user"]); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
61 return http_response(409, "User already exists."); |
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 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
64 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
65 |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
66 local function setup() |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
67 local ports = module:get_option("reg_servlet_port") or { 5280 }; |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
68 local base_name = module:get_option("reg_servlet_base") or "register_account"; |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
69 require "net.httpserver".new_from_config(ports, handle_req, { base = base_name }); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
70 end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
71 if prosody.start_time then -- already started |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
72 setup(); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
73 else |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
74 prosody.events.add_handler("server-started", setup); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
75 end |