Mercurial > prosody-modules
annotate mod_invites_register_api/mod_invites_register_api.lua @ 4393:ae1d1e352504
mod_http_admin_api: remove deprecated API endpoint
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Tue, 26 Jan 2021 16:01:45 +0100 |
parents | a1256e376dca |
children | dbfa830e4504 |
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 |
4381
a1256e376dca
mod_invites_register_api: reroute register call
Jonas Schäfer <jonas@wielicki.name>
parents:
4380
diff
changeset
|
35 function register_with_invite(event) |
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
|
36 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
|
37 |
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 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 |
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 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 |
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 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
|
51 |
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 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
|
53 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
|
54 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
|
55 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
|
56 |
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 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
|
58 |
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 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
|
60 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
|
61 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
|
62 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
|
63 |
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 -- 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
|
65 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
|
66 |
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 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
|
68 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
|
69 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
|
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 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
|
72 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
|
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 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 }; |
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 |
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 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
|
84 |
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 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
|
86 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
|
87 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
|
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 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
|
90 |
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 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 |
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: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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 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
|
103 }); |
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 |
4381
a1256e376dca
mod_invites_register_api: reroute register call
Jonas Schäfer <jonas@wielicki.name>
parents:
4380
diff
changeset
|
105 return json.encode({ |
a1256e376dca
mod_invites_register_api: reroute register call
Jonas Schäfer <jonas@wielicki.name>
parents:
4380
diff
changeset
|
106 jid = prepped_username .. "@" .. module.host; |
a1256e376dca
mod_invites_register_api: reroute register call
Jonas Schäfer <jonas@wielicki.name>
parents:
4380
diff
changeset
|
107 }); |
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
|
108 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
|
109 |
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 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
|
111 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
|
112 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
|
113 ["GET /invite/*"] = get_invite_info; |
4381
a1256e376dca
mod_invites_register_api: reroute register call
Jonas Schäfer <jonas@wielicki.name>
parents:
4380
diff
changeset
|
114 ["POST /register"] = register_with_invite; |
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
|
115 }; |
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 }); |