annotate mod_invites/mod_invites.lua @ 4102:4e7ff27c212c

mod_lastlog2: Fix 'registered' event (Thanks Ge0rG) Looks like this event was not updated for the map change, it's still how it was in mod_lastlog(1)
author Kim Alvefur <zash@zash.se>
date Fri, 11 Sep 2020 16:45:47 +0200
parents 24f4eb35ab60
children a104440c20a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
4 local jid_split = require "util.jid".split;
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 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
7
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
8 local token_storage;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
9 if prosody.process_type == "prosody" or prosody.shutdown then
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
10 token_storage = module:open_store("invite_token", "map");
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
11 end
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 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
14 return url.build({
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 scheme = "xmpp",
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 path = jid,
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 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
18 });
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
4077
f85ea76447dd mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 3776
diff changeset
21 local function create_invite(invite_action, invite_jid, allow_registration, additional_data)
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 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
23
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 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
25 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
26
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 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
28
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local invite = {
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 type = invite_action;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 jid = invite_jid;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 token = token;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 allow_registration = allow_registration;
4077
f85ea76447dd mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 3776
diff changeset
35 additional_data = additional_data;
3776
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 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
38
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 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
40 expires = expires;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 };
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 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
44
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 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
46 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
47 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
48 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
49 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
50 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 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
54 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
55 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
56 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
57 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
58 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
59 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 return invite;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 -- Create invitation to register an account (optionally restricted to the specified username)
4077
f85ea76447dd mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 3776
diff changeset
66 function create_account(account_username, additional_data) --luacheck: ignore 131/create_account
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 local jid = account_username and (account_username.."@"..module.host) or module.host;
4077
f85ea76447dd mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 3776
diff changeset
68 return create_invite("register", jid, true, additional_data);
3776
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
4078
2f0c8670d2fa mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4077
diff changeset
71 -- Create invitation to reset the password for an account
2f0c8670d2fa mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4077
diff changeset
72 function create_account_reset(account_username) --luacheck: ignore 131/create_account_reset
2f0c8670d2fa mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4077
diff changeset
73 return create_account(account_username, { allow_reset = account_username });
2f0c8670d2fa mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4077
diff changeset
74 end
2f0c8670d2fa mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4077
diff changeset
75
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 -- Create invitation to become a contact of a local user
4077
f85ea76447dd mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 3776
diff changeset
77 function create_contact(username, allow_registration, additional_data) --luacheck: ignore 131/create_contact
f85ea76447dd mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 3776
diff changeset
78 return create_invite("roster", username.."@"..module.host, allow_registration, additional_data);
3776
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
4080
14a3f5223074 mod_invites: Whitespace (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4079
diff changeset
81
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 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
83 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
84
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 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
86 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
87 -- 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
88 -- 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
89 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
90 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 token_storage:set(nil, self.token, nil);
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
92
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 return true;
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 -- 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
97 -- 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
98 -- 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
99 -- 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
100 -- 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
101 -- 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
102 -- 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
103 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
104 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
105 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
106 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 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
109
4079
b68b917e568f mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4078
diff changeset
110 -- Fetch from host store (account invite)
b68b917e568f mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4078
diff changeset
111 local token_info = token_storage:get(nil, token);
b68b917e568f mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4078
diff changeset
112
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 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
114 -- 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
115 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
116 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
117 valid_until = token_info and token_info.expires;
4081
3c18d8deeb38 mod_invites: Fix potential traceback when invalid token used (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4080
diff changeset
118 if token_info and token_info.type == "roster" then
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 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
120 inviter = username;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 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
125 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
126 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
127 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
128 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
129 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
130 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 return setmetatable({
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 token = token;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 username = username;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 inviter = inviter;
4079
b68b917e568f mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4078
diff changeset
136 type = token_info and token_info.type or "roster";
b68b917e568f mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4078
diff changeset
137 uri = token_info and token_info.uri or get_uri("roster", username.."@"..module.host, token);
4077
f85ea76447dd mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 3776
diff changeset
138 additional_data = token_info and token_info.additional_data or nil;
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 }, valid_invite_mt);
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 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
143 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
144 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
145 end
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
146
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
147 --- shell command
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
148 do
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
149 -- Since the console is global this overwrites the command for
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
150 -- each host it's loaded on, but this should be fine.
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
151
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
152 local get_module = require "core.modulemanager".get_module;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
153
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
154 local console_env = module:shared("/*/admin_shell/env");
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
155
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
156 -- luacheck: ignore 212/self
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
157 console_env.invite = {};
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
158 function console_env.invite:create_account(user_jid)
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
159 local username, host = jid_split(user_jid);
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
160 local mod_invites, err = get_module(host, "invites");
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
161 if not mod_invites then return nil, err or "mod_invites not loaded on this host"; end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
162 local invite, err = mod_invites.create_account(username);
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
163 if not invite then return nil, err; end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
164 return true, invite.uri;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
165 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
166
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
167 function console_env.invite:create_contact(user_jid, allow_registration)
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
168 local username, host = jid_split(user_jid);
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
169 local mod_invites, err = get_module(host, "invites");
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
170 if not mod_invites then return nil, err or "mod_invites not loaded on this host"; end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
171 local invite, err = mod_invites.create_contact(username, allow_registration);
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
172 if not invite then return nil, err; end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
173 return true, invite.uri;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
174 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
175 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
176
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
177 --- prosodyctl command
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
178 function module.command(arg)
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
179 if #arg < 2 or arg[1] ~= "generate" then
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
180 print("usage: prosodyctl mod_"..module.name.." generate example.com");
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
181 return;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
182 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
183 table.remove(arg, 1); -- pop command
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
184
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
185 local sm = require "core.storagemanager";
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
186 local mm = require "core.modulemanager";
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
187
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
188 local host = arg[1];
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
189 assert(hosts[host], "Host "..tostring(host).." does not exist");
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
190 sm.initialize_host(host);
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
191 table.remove(arg, 1); -- pop host
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
192 module.host = host;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
193 token_storage = module:open_store("invite_token", "map");
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
194
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
195 -- Load mod_invites
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
196 local invites = module:depends("invites");
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
197 local invites_page_module = module:get_option_string("invites_page_module", "invites_page");
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
198 if mm.get_modules_for_host(host):contains(invites_page_module) then
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
199 module:depends(invites_page_module);
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
200 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
201
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
202
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
203 local invite, roles;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
204 if arg[1] == "--reset" then
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
205 local nodeprep = require "util.encodings".stringprep.nodeprep;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
206 local username = nodeprep(arg[2]);
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
207 if not username then
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
208 print("Please supply a valid username to generate a reset link for");
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
209 return;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
210 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
211 invite = assert(invites.create_account_reset(username));
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
212 else
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
213 if arg[1] == "--admin" then
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
214 roles = { ["prosody:admin"] = true };
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
215 elseif arg[1] == "--role" then
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
216 roles = { [arg[2]] = true };
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
217 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
218 invite = assert(invites.create_account(nil, { roles = roles }));
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
219 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
220
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
221 print(invite.landing_page or invite.uri);
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
222 end