author | Marco Cirillo <maranda@lightwitch.org> |
Tue, 12 Apr 2011 17:47:47 +0000 | |
changeset 358 | 4483bb889d12 |
parent 357 | 59345fd38ad9 |
child 359 | 5d22ebcb9ec5 |
permissions | -rw-r--r-- |
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 |
|
357
59345fd38ad9
mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents:
356
diff
changeset
|
11 |
module.host = "*" -- HTTP/BOSH Servlets need to be global. |
355
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 |
-- 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
|
54 |
if not usermanager.user_exists(req_body["username"], req_body["host"]) then |
357
59345fd38ad9
mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents:
356
diff
changeset
|
55 |
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
|
56 |
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
|
57 |
return http_response(200, "Done."); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
58 |
else |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
59 |
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
|
60 |
return http_response(409, "User already exists."); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
61 |
end |
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 |
|
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
65 |
local function setup() |
358
4483bb889d12
mod_register_json: Minor refactor, default to port 9280 if option is unspecified or default to port 9443 if SSL is used. (Good, bad?)
Marco Cirillo <maranda@lightwitch.org>
parents:
357
diff
changeset
|
66 |
local ports = module:get_option("reg_servlet_port") or { 9280 }; |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
67 |
local base_name = module:get_option("reg_servlet_base") or "register_account"; |
357
59345fd38ad9
mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents:
356
diff
changeset
|
68 |
local ssl_cert = module:get_option("reg_servlet_sslcert") or false; |
59345fd38ad9
mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents:
356
diff
changeset
|
69 |
local ssl_key = module:get_option("reg_servlet_sslkey") or false; |
59345fd38ad9
mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents:
356
diff
changeset
|
70 |
if not ssl_cert or not ssl_key then |
59345fd38ad9
mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents:
356
diff
changeset
|
71 |
require "net.httpserver".new_from_config(ports, handle_req, { base = base_name }); |
59345fd38ad9
mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents:
356
diff
changeset
|
72 |
else |
358
4483bb889d12
mod_register_json: Minor refactor, default to port 9280 if option is unspecified or default to port 9443 if SSL is used. (Good, bad?)
Marco Cirillo <maranda@lightwitch.org>
parents:
357
diff
changeset
|
73 |
if module:get_option("reg_servlet_port") == nil then ports = { 9443 }; end |
357
59345fd38ad9
mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents:
356
diff
changeset
|
74 |
require "net.httpserver".new_from_config(ports, handle_req, { ssl = { key = ssl_key, certificate = ssl_cert }, base = base_name }); |
59345fd38ad9
mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents:
356
diff
changeset
|
75 |
end |
355
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
76 |
end |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
77 |
if prosody.start_time then -- already started |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
78 |
setup(); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
79 |
else |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
80 |
prosody.events.add_handler("server-started", setup); |
a5da789b2e7d
mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
81 |
end |