Mercurial > prosody-modules
annotate mod_invites/mod_invites.lua @ 3933:93147b89ea67
mod_rest: Avoid interfering with mod_http_oauth2 errors (fixes #1500)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 08 Mar 2020 12:52:18 +0100 |
parents | 80830d97da81 |
children | f85ea76447dd |
rev | line source |
---|---|
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local id = require "util.id"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local url = require "socket.url"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local jid_node = require "util.jid".node; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local invite_ttl = module:get_option_number("invite_expiry", 86400 * 7); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local token_storage = module:open_store("invite_token", "map"); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local function get_uri(action, jid, token, params) --> string |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 return url.build({ |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 scheme = "xmpp", |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 path = jid, |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 query = action..";preauth="..token..(params and (";"..params) or ""), |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 }); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local function create_invite(invite_action, invite_jid, allow_registration) |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local token = id.medium(); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 local created_at = os.time(); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local expires = created_at + invite_ttl; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local invite_params = (invite_action == "roster" and allow_registration) and "ibr=y" or nil; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 local invite = { |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 type = invite_action; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 jid = invite_jid; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 token = token; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 allow_registration = allow_registration; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 uri = get_uri(invite_action, invite_jid, token, invite_params); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 created_at = created_at; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 expires = expires; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 }; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 module:fire_event("invite-created", invite); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 if allow_registration then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 local ok, err = token_storage:set(nil, token, invite); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 if not ok then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 module:log("warn", "Failed to store account invite: %s", err); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 return nil, "internal-server-error"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 if invite_action == "roster" then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 local username = jid_node(invite_jid); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 local ok, err = token_storage:set(username, token, expires); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 if not ok then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 module:log("warn", "Failed to store subscription invite: %s", err); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 return nil, "internal-server-error"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 return invite; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 -- Create invitation to register an account (optionally restricted to the specified username) |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 function create_account(account_username) --luacheck: ignore 131/create_account |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 local jid = account_username and (account_username.."@"..module.host) or module.host; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 return create_invite("register", jid, true); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 -- Create invitation to become a contact of a local user |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 function create_contact(username, allow_registration) --luacheck: ignore 131/create_contact |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 return create_invite("roster", username.."@"..module.host, allow_registration); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 local valid_invite_methods = {}; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 local valid_invite_mt = { __index = valid_invite_methods }; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 function valid_invite_methods:use() |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 if self.username then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 -- Also remove the contact invite if present, on the |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 -- assumption that they now have a mutual subscription |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 token_storage:set(self.username, self.token, nil); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 token_storage:set(nil, self.token, nil); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 return true; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 -- Get a validated invite (or nil, err). Must call :use() on the |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 -- returned invite after it is actually successfully used |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 -- For "roster" invites, the username of the local user (who issued |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 -- the invite) must be passed. |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 -- If no username is passed, but the registration is a roster invite |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 -- from a local user, the "inviter" field of the returned invite will |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 -- be set to their username. |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 function get(token, username) |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 if not token then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 return nil, "no-token"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 local valid_until, inviter; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 if username then -- token being used for subscription |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 -- Fetch from user store (subscription invite) |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 valid_until = token_storage:get(username, token); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 else -- token being used for account creation |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 -- Fetch from host store (account invite) |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 local token_info = token_storage:get(nil, token); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 valid_until = token_info and token_info.expires; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 if token_info.type == "roster" then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 username = jid_node(token_info.jid); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 inviter = username; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 if not valid_until then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 module:log("debug", "Got unknown token: %s", token); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 return nil, "token-invalid"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 elseif os.time() > valid_until then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 module:log("debug", "Got expired token: %s", token); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 return nil, "token-expired"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 return setmetatable({ |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 token = token; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 username = username; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 inviter = inviter; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 }, valid_invite_mt); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 function use(token) --luacheck: ignore 131/use |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 local invite = get(token); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 return invite and invite:use(); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 end |