Annotate

mod_register_json/register_json/mod_register_json.lua @ 989:7c04c5856daa

mod_register_json: major code overhaul into a token based registration & verification system.
author Marco Cirillo <maranda@lightwitch.org>
date Mon, 29 Apr 2013 22:53:39 +0200
child 991:929dcf3c4bcb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
989
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
1 -- Expose a simple token based servlet to handle user registrations from web pages
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
2 -- through Base64 encoded JSON.
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
3
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
4 -- Copyright (C) 2010 - 2013, Marco Cirillo (LW.Org)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
5
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
6 local datamanager = datamanager
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
7 local b64_decode = require "util.encodings".base64.decode
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
8 local b64_encode = require "util.encodings".base64.encode
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
9 local http_event = require "net.http.server".fire_event
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
10 local jid_prep = require "util.jid".prep
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
11 local jid_split = require "util.jid".split
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
12 local json_decode = require "util.json".decode
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
13 local nodeprep = require "util.encodings".stringprep.nodeprep
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
14 local open, os_time, setmt, type = io.open, os.time, setmetatable, type
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
15 local sha1 = require "util.hashes".sha1
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
16 local urldecode = http.urldecode
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
17 local usermanager = usermanager
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
18 local uuid_gen = require "util.uuid".generate
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
19 local timer = require "util.timer"
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
20
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
21 module:depends("http")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
22
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
23 -- Pick up configuration and setup stores/variables.
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
24
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
25 local auth_token = module:get_option_string("reg_servlet_auth_token")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
26 local secure = module:get_option_boolean("reg_servlet_secure", true)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
27 local base_path = module:get_option_string("reg_servlet_base", "/register_account/")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
28 local throttle_time = module:get_option_number("reg_servlet_ttime", nil)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
29 local whitelist = module:get_option_set("reg_servlet_wl", {})
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
30 local blacklist = module:get_option_set("reg_servlet_bl", {})
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
31 local fm_patterns = module:get_option("reg_servlet_filtered_mails", {})
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
32 if type(fm_patterns) ~= "table" then fm_patterns = {} end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
33
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
34 local files_base = module.path:gsub("/[^/]+$","") .. "/template/"
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
35
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
36 local recent_ips = {}
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
37 local pending = {}
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
38 local pending_node = {}
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
39
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
40 -- Setup hashes data structure
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
41
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
42 hashes = { _index = {} }
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
43 local hashes_mt = {} ; hashes_mt.__index = hashes_mt
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
44 function hashes_mt:add(node, mail)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
45 local _hash = b64_encode(sha1(mail))
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
46 if not self:exists(_hash) then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
47 self[_hash] = node ; self._index[node] = _hash ; self:save()
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
48 return true
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
49 else
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
50 return false
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
51 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
52 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
53 function hashes_mt:exists(hash)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
54 if hashes[hash] then return true else return false end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
55 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
56 function hashes_mt:remove(node)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
57 local _hash = self._index[node]
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
58 if _hash then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
59 self[_hash] = nil ; self._index[node] = nil ; self:save()
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
60 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
61 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
62 function hashes_mt:save()
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
63 if not datamanager.store("register_json", module.host, "hashes", hashes) then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
64 module:log("error", "Failed to save the mail addresses' hashes store.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
65 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
66 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
67
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
68 local function check_mail(address)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
69 for _, pattern in ipairs(fm_patterns) do
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
70 if address:match(pattern) then return false end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
71 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
72 return true
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
73 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
74
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
75 -- Begin
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
76
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
77 local function handle(code, message) return http_event("http-error", { code = code, message = message }) end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
78 local function http_response(event, code, message, headers)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
79 local response = event.response
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
80
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
81 if headers then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
82 for header, data in pairs(headers) do response.headers[header] = data end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
83 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
84
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
85 response.status_code = code
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
86 response:send(handle(code, message))
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
87 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
88
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
89 local function handle_req(event)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
90 local response = event.response
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
91 local request = event.request
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
92 local body = request.body
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
93 if secure and not request.secure then return nil end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
94
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
95 if request.method ~= "POST" then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
96 return http_response(event, 405, "Bad method.", {["Allow"] = "POST"})
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
97 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
98
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
99 local req_body
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
100 -- We check that what we have is valid JSON wise else we throw an error...
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
101 if not pcall(function() req_body = json_decode(b64_decode(body)) end) then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
102 module:log("debug", "Data submitted for user registration by %s failed to Decode.", user)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
103 return http_response(event, 400, "Decoding failed.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
104 else
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
105 -- Decode JSON data and check that all bits are there else throw an error
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
106 if req_body["username"] == nil or req_body["password"] == nil or req_body["ip"] == nil or req_body["mail"] == nil or
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
107 req_body["auth_token"] == nil then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
108 module:log("debug", "%s supplied an insufficent number of elements or wrong elements for the JSON registration", user)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
109 return http_response(event, 400, "Invalid syntax.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
110 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
111 -- Set up variables
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
112 local username, password, ip, mail, token = req_body.username, req_body.password, req_body.ip, req_body.mail, req_body.auth_token
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
113
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
114 -- Check if user is an admin of said host
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
115 if token ~= auth_token then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
116 module:log("warn", "%s tried to retrieve a registration token for %s@%s", request.ip, username, module.host)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
117 return http_response(event, 401, "Auth token is invalid! The attempt has been logged.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
118 else
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
119 -- Blacklist can be checked here.
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
120 if blacklist:contains(ip) then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
121 module:log("warn", "Attempt of reg. submission to the JSON servlet from blacklisted address: %s", ip)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
122 return http_response(403, "The specified address is blacklisted, sorry.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
123 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
124
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
125 if not check_mail(mail) then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
126 module:log("warn", "%s attempted to use a mail address (%s) matching one of the forbidden patterns.", ip, mail)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
127 return http_response(403, "Requesting to register using this E-Mail address is forbidden, sorry.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
128 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
129
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
130 -- We first check if the supplied username for registration is already there.
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
131 -- And nodeprep the username
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
132 username = nodeprep(username)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
133 if not username then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
134 module:log("debug", "An username containing invalid characters was supplied: %s", req_body["username"])
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
135 return http_response(event, 406, "Supplied username contains invalid characters, see RFC 6122.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
136 else
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
137 if pending_node[username] then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
138 module:log("warn", "%s attempted to submit a registration request but another request for that user is pending")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
139 return http_response(event, 401, "Another user registration by that username is pending.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
140 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
141
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
142 if not usermanager.user_exists(username, module.host) then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
143 -- if username fails to register successive requests shouldn't be throttled until one is successful.
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
144 if throttle_time and not whitelist:contains(ip) then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
145 if not recent_ips[ip] then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
146 recent_ips[ip] = os_time()
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
147 else
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
148 if os_time() - recent_ips[ip] < throttle_time then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
149 recent_ips[ip] = os_time()
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
150 module:log("warn", "JSON Registration request from %s has been throttled.", req_body["ip"])
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
151 return http_response(event, 503, "Request throttled, wait a bit and try again.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
152 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
153 recent_ips[ip] = os_time()
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
154 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
155 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
156
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
157 local uuid = uuid_gen()
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
158 if not hashes:add(username, mail) then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
159 module:log("warn", "%s (%s) attempted to register to the server with an E-Mail address we already possess the hash of.", username, ip)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
160 return http_response(event, 409, "The E-Mail Address provided matches the hash associated to an existing account.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
161 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
162 pending[uuid] = { node = username, password = password, ip = ip }
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
163 pending_node[username] = uuid
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
164
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
165 timer.add_task(300, function()
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
166 if pending[uuid] then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
167 pending[uuid] = nil
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
168 pending_node[username] = nil
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
169 hashes:remove(username)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
170 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
171 end)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
172 module:log("info", "%s (%s) submitted a registration request and is awaiting final verification", username, uuid)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
173 return uuid
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
174 else
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
175 module:log("debug", "%s registration data submission failed (user already exists)", username)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
176 return http_response(event, 409, "User already exists.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
177 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
178 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
179 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
180 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
181 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
182
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
183 local function open_file(file)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
184 local f, err = open(file, "rb");
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
185 if not f then return nil end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
186
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
187 local data = f:read("*a") ; f:close()
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
188 return data
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
189 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
190
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
191 local function r_template(event, type)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
192 local response = event.response
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
193
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
194 local data = open_file(files_base..type.."_t.html")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
195 if data then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
196 data = data:gsub("%%REG%-URL", base_path.."verify/")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
197 return data
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
198 else return http_response(event, 500, "Failed to obtain template.") end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
199 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
200
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
201 local function handle_verify(event, path)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
202 local response = event.response
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
203 local request = event.request
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
204 local body = request.body
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
205 if secure and not request.secure then return nil end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
206
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
207 local valid_files = {
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
208 ["css/style.css"] = files_base.."css/style.css",
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
209 ["images/tile.png"] = files_base.."images/tile.png",
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
210 ["images/header.png"] = files_base.."images/header.png"
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
211 }
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
212
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
213 if request.method == "GET" then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
214 if path == "" then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
215 return r_template(event, "form")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
216 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
217
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
218 if valid_files[path] then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
219 local data = open_file(valid_files[path])
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
220 if data then return data
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
221 else return http_response(event, 404, "Not found.") end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
222 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
223 elseif request.method == "POST" then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
224 if path == "" then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
225 if not request.body then return http_response(event, 400, "Bad Request.") end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
226 local uuid = urldecode(request.body):match("^uuid=(.*)$")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
227
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
228 if not pending[uuid] then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
229 return r_template(event, "fail")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
230 else
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
231 local username, password, ip =
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
232 pending[uuid].node, pending[uuid].password, pending[uuid].ip
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
233
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
234 local ok, error = usermanager.create_user(username, password, module.host)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
235 if ok then
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
236 module:fire_event(
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
237 "user-registered",
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
238 { username = username, host = module.host, source = "mod_register_json", session = { ip = ip } }
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
239 )
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
240 module:log("info", "Account %s@%s is successfully verified and activated", username, module.host)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
241 -- we shall not clean the user from the pending lists as long as registration doesn't succeed.
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
242 pending[uuid] = nil ; pending_node[username] = nil
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
243 return r_template(event, "success")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
244 else
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
245 module:log("error", "User creation failed: "..error)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
246 return http_response(event, 500, "Encountered server error while creating the user: "..error)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
247 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
248 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
249 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
250 else
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
251 return http_response(event, 405, "Invalid method.")
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
252 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
253 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
254
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
255 local function handle_user_deletion(event)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
256 local user, hostname = event.username, event.host
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
257 if hostname == module.host then hashes:remove(user) end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
258 end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
259
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
260 -- Set it up!
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
261
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
262 hashes = datamanager.load("register_json", module.host, "hashes") or hashes ; setmt(hashes, hashes_mt)
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
263
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
264 module:provides("http", {
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
265 default_path = base_path,
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
266 route = {
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
267 ["GET /"] = handle_req,
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
268 ["POST /"] = handle_req,
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
269 ["GET /verify/*"] = handle_verify,
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
270 ["POST /verify/*"] = handle_verify
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
271 }
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
272 })
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
273
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
274 module:hook_global("user-deleted", handle_user_deletion, 10);
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
275
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
276 -- Reloadability
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
277
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
278 module.save = function() return { hashes = hashes } end
7c04c5856daa mod_register_json: major code overhaul into a token based registration & verification system.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
279 module.restore = function(data) hashes = data.hashes or { _index = {} } ; setmt(hashes, hashes_mt) end