Software /
code /
prosody-modules
Annotate
mod_invites_register_api/mod_invites_register_api.lua @ 4380:cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 25 Jan 2021 12:44:20 +0000 |
child | 4381:a1256e376dca |
rev | line source |
---|---|
4380
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local id = require "util.id"; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local json = require "util.json"; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local usermanager = require "core.usermanager"; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local nodeprep = require "util.encodings".stringprep.nodeprep; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local site_name = module:get_option_string("site_name", module.host); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local json_content_type = "application/json"; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 module:depends("http"); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local invites = module:depends("invites"); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 function get_invite_info(event, invite_token) |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 if not invite_token or #invite_token == 0 then |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 return 404; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local invite = invites.get(invite_token); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 if not invite then |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 return 404; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 event.response.headers["Content-Type"] = json_content_type; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 return json.encode({ |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 site_name = site_name; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 token = invite.token; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 domain = module.host; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 uri = invite.uri; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 type = invite.type; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 jid = invite.jid; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 inviter = invite.inviter; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 }); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 function register_with_invite(event, invite_token) |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 if not invite_token or #invite_token == 0 then |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 return 404; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 local request, response = event.request, event.response; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 if not request.body or #request.body == 0 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 or request.headers.content_type ~= json_content_type then |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 module:log("warn", "Invalid payload"); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 return 400; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 local register_data = json.decode(event.request.body); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 if not register_data then |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 module:log("warn", "Invalid JSON"); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 return 400; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 local user, password, token = register_data.username, register_data.password, register_data.token; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local invite = invites.get(token); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 if not invite then |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 return 404; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 response.headers["Content-Type"] = json_content_type; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 if not user or #user == 0 or not password or #password == 0 or not token then |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 module:log("warn", "Invalid data"); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 return 400; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 -- Shamelessly copied from mod_register_web. |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 local prepped_username = nodeprep(user); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 if not prepped_username or #prepped_username == 0 then |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 return 400; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 if usermanager.user_exists(prepped_username, module.host) then |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 return 409; -- Conflict |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 local registering = { |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 validated_invite = invite; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 username = prepped_username; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 host = module.host; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 ip = request.ip; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 allowed = true; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 }; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 module:fire_event("user-registering", registering); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 if not registering.allowed then |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 return 403; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 local ok, err = usermanager.create_user(prepped_username, password, module.host); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 if not ok then |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 local err_id = id.short(); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 module:log("warn", "Registration failed (%s): %s", err_id, tostring(err)); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 return 500; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 module:fire_event("user-registered", { |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 username = prepped_username; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 host = module.host; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 source = "mod_"..module.name; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 validated_invite = invite; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 ip = request.ip; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 }); |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 return 200; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 end |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 module:provides("http", { |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 default_path = "register_api"; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 route = { |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 ["GET /invite/*"] = get_invite_info; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 ["POST /register/*"] = register_with_invite; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 }; |
cba8cd564641
mod_invites_register_api: New module to allow turning invites into accounts via a HTTP API
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 }); |