Software /
code /
prosody-modules
Comparison
mod_register_json/mod_register_json.lua @ 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?
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Tue, 12 Apr 2011 17:13:38 +0000 |
parent | 356:5dacfbc9cd86 |
child | 358:4483bb889d12 |
comparison
equal
deleted
inserted
replaced
356:5dacfbc9cd86 | 357:59345fd38ad9 |
---|---|
6 | 6 |
7 local usermanager = require "core.usermanager"; | 7 local usermanager = require "core.usermanager"; |
8 local b64_decode = require "util.encodings".base64.decode; | 8 local b64_decode = require "util.encodings".base64.decode; |
9 local json_decode = require "util.json".decode; | 9 local json_decode = require "util.json".decode; |
10 | 10 |
11 module.host = "*" -- HTTP/BOSH Servlets need to be loaded globally. | 11 module.host = "*" -- HTTP/BOSH Servlets need to be global. |
12 | 12 |
13 local set_realm_name = module:get_option("reg_servlet_realm") or "Restricted"; | 13 local set_realm_name = module:get_option("reg_servlet_realm") or "Restricted"; |
14 | 14 |
15 local function http_response(code, message, extra_headers) | 15 local function http_response(code, message, extra_headers) |
16 local response = { | 16 local response = { |
48 module:log("debug", "%s tried to submit registration data for %s but he's not an admin", user, req_body["host"]) | 48 module:log("debug", "%s tried to submit registration data for %s but he's not an admin", user, req_body["host"]) |
49 return http_response(401, "I obey only to my masters... Have a nice day."); | 49 return http_response(401, "I obey only to my masters... Have a nice day."); |
50 else | 50 else |
51 -- Various sanity checks. | 51 -- Various sanity checks. |
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 | 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 |
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 | |
54 -- We first check if the supplied username for registration is already there. | 53 -- We first check if the supplied username for registration is already there. |
55 if not usermanager.user_exists(req_body["username"], req_body["host"]) then | 54 if not usermanager.user_exists(req_body["username"], req_body["host"]) then |
56 usermanager.create_user(req_body["username"], req_body["password"], req_body["host"]); | 55 usermanager.create_user(req_body["username"], req_body["password"], req_body["host]); |
57 module:log("debug", "%s registration data submission for %s is successful", user, req_body["user"]); | 56 module:log("debug", "%s registration data submission for %s is successful", user, req_body["user"]); |
58 return http_response(200, "Done."); | 57 return http_response(200, "Done."); |
59 else | 58 else |
60 module:log("debug", "%s registration data submission for %s failed (user already exists)", user, req_body["user"]); | 59 module:log("debug", "%s registration data submission for %s failed (user already exists)", user, req_body["user"]); |
61 return http_response(409, "User already exists."); | 60 return http_response(409, "User already exists."); |
62 end | 61 end |
63 end | 62 end |
64 end | 63 end |
65 | 64 |
66 local function setup() | 65 local function setup() |
67 local ports = module:get_option("reg_servlet_port") or { 5280 }; | 66 local ports = module:get_option("reg_servlet_port") or { 9443 }; |
68 local base_name = module:get_option("reg_servlet_base") or "register_account"; | 67 local base_name = module:get_option("reg_servlet_base") or "register_account"; |
69 require "net.httpserver".new_from_config(ports, handle_req, { base = base_name }); | 68 local ssl_cert = module:get_option("reg_servlet_sslcert") or false; |
69 local ssl_key = module:get_option("reg_servlet_sslkey") or false; | |
70 if not ssl_cert or not ssl_key then | |
71 require "net.httpserver".new_from_config(ports, handle_req, { base = base_name }); | |
72 else | |
73 require "net.httpserver".new_from_config(ports, handle_req, { ssl = { key = ssl_key, certificate = ssl_cert }, base = base_name }); | |
74 end | |
70 end | 75 end |
71 if prosody.start_time then -- already started | 76 if prosody.start_time then -- already started |
72 setup(); | 77 setup(); |
73 else | 78 else |
74 prosody.events.add_handler("server-started", setup); | 79 prosody.events.add_handler("server-started", setup); |