Annotate

mod_invites/mod_invites.lua @ 5491:7842502c1157

mod_http_debug: Log some extended info about requests If you point something external at this module, you don't get the response body back, hence it can be useful to see some details in the log as well.
author Kim Alvefur <zash@zash.se>
date Fri, 26 May 2023 15:37:15 +0200
parent 4422:2047dd56cc40
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";
4341
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
2 local it = require "util.iterators";
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 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
4 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
5 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
6
4346
671bc55f0fc9 mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
7 local default_ttl = module:get_option_number("invite_expiry", 86400 * 7);
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
9 local token_storage;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
10 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
11 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
12 end
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 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
15 return url.build({
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 scheme = "xmpp",
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 path = jid,
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 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
19 });
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
4346
671bc55f0fc9 mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
22 local function create_invite(invite_action, invite_jid, allow_registration, additional_data, ttl, reusable)
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 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
24
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local created_at = os.time();
4346
671bc55f0fc9 mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
26 local expires = created_at + (ttl or default_ttl);
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 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
29
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local invite = {
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 type = invite_action;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 jid = invite_jid;
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 token = token;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 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
36 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
37
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 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
39
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 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
41 expires = expires;
4346
671bc55f0fc9 mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
42
671bc55f0fc9 mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
43 reusable = reusable;
3776
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
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 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
47
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 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
49 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
50 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
51 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
52 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
53 end
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
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 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
57 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
58 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
59 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
60 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
61 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
62 end
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 return invite;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 -- Create invitation to register an account (optionally restricted to the specified username)
4377
a0f1fb5e7829 mod_invites: Add ttl to all public creation APIs
Matthew Wild <mwild1@gmail.com>
parents: 4376
diff changeset
69 function create_account(account_username, additional_data, ttl) --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
70 local jid = account_username and (account_username.."@"..module.host) or module.host;
4377
a0f1fb5e7829 mod_invites: Add ttl to all public creation APIs
Matthew Wild <mwild1@gmail.com>
parents: 4376
diff changeset
71 return create_invite("register", jid, true, additional_data, ttl);
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
4078
2f0c8670d2fa mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4077
diff changeset
74 -- Create invitation to reset the password for an account
4377
a0f1fb5e7829 mod_invites: Add ttl to all public creation APIs
Matthew Wild <mwild1@gmail.com>
parents: 4376
diff changeset
75 function create_account_reset(account_username, ttl) --luacheck: ignore 131/create_account_reset
a0f1fb5e7829 mod_invites: Add ttl to all public creation APIs
Matthew Wild <mwild1@gmail.com>
parents: 4376
diff changeset
76 return create_account(account_username, { allow_reset = account_username }, ttl or 86400);
4078
2f0c8670d2fa mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4077
diff changeset
77 end
2f0c8670d2fa mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4077
diff changeset
78
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 -- Create invitation to become a contact of a local user
4377
a0f1fb5e7829 mod_invites: Add ttl to all public creation APIs
Matthew Wild <mwild1@gmail.com>
parents: 4376
diff changeset
80 function create_contact(username, allow_registration, additional_data, ttl) --luacheck: ignore 131/create_contact
a0f1fb5e7829 mod_invites: Add ttl to all public creation APIs
Matthew Wild <mwild1@gmail.com>
parents: 4376
diff changeset
81 return create_invite("roster", username.."@"..module.host, allow_registration, additional_data, ttl);
3776
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
4347
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
84 -- Create invitation to register an account and join a user group
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
85 -- If explicit ttl is passed, invite is valid for multiple signups
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
86 -- during that time period
4377
a0f1fb5e7829 mod_invites: Add ttl to all public creation APIs
Matthew Wild <mwild1@gmail.com>
parents: 4376
diff changeset
87 function create_group(group_ids, additional_data, ttl) --luacheck: ignore 131/create_group
4347
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
88 local merged_additional_data = {
4357
a49ca492e621 mod_invites, mod_http_admin_api: Allow specifying multiple groups when creating an invite
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
89 groups = group_ids;
4347
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
90 };
4376
4b617a246d81 mod_invites: Fix typo in variable name
Matthew Wild <mwild1@gmail.com>
parents: 4357
diff changeset
91 if additional_data then
4347
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
92 for k, v in pairs(additional_data) do
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
93 merged_additional_data[k] = v;
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
94 end
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
95 end
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
96 return create_invite("register", module.host, true, merged_additional_data, ttl, not not ttl);
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
97 end
0ec482e617bb mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents: 4346
diff changeset
98
4341
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
99 -- Iterates pending (non-expired, unused) invites that allow registration
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
100 function pending_account_invites() --luacheck: ignore 131/pending_account_invites
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
101 local store = module:open_store("invite_token");
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
102 local now = os.time();
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
103 local function is_valid_invite(_, invite)
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
104 return invite.expires > now;
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
105 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
106 return it.filter(is_valid_invite, pairs(store:get(nil) or {}));
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
107 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
108
4344
844cfc8c4039 mod_invites: Fix some more luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents: 4342
diff changeset
109 function get_account_invite_info(token) --luacheck: ignore 131/get_account_invite_info
4341
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
110 if not token then
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
111 return nil, "no-token";
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
112 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
113
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
114 -- Fetch from host store (account invite)
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
115 local token_info = token_storage:get(nil, token);
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
116 if not token_info then
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
117 return nil, "token-invalid";
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
118 elseif os.time() > token_info.expires then
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
119 return nil, "token-expired";
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
120 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
121
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
122 return token_info;
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
123 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
124
4344
844cfc8c4039 mod_invites: Fix some more luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents: 4342
diff changeset
125 function delete_account_invite(token) --luacheck: ignore 131/delete_account_invite
4341
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
126 if not token then
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
127 return nil, "no-token";
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
128 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
129
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
130 return token_storage:set(nil, token, nil);
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
131 end
4080
14a3f5223074 mod_invites: Whitespace (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4079
diff changeset
132
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 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
134 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
135
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 function valid_invite_methods:use()
4346
671bc55f0fc9 mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
137 if self.reusable then
671bc55f0fc9 mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
138 return true;
671bc55f0fc9 mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
139 end
671bc55f0fc9 mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
140
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 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
142 -- 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
143 -- 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
144 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
145 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 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
147
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 return true;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 -- 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
152 -- 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
153 -- 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
154 -- 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
155 -- 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
156 -- 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
157 -- 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
158 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
159 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
160 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
161 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 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
164
4079
b68b917e568f mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4078
diff changeset
165 -- 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
166 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
167
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 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
169 -- 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
170 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
171 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
172 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
173 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
174 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
175 inviter = username;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 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
180 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
181 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
182 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
183 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
184 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
185 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 return setmetatable({
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 token = token;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 username = username;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 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
191 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
192 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
193 additional_data = token_info and token_info.additional_data or nil;
4346
671bc55f0fc9 mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents: 4344
diff changeset
194 reusable = token_info.reusable;
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 }, valid_invite_mt);
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 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
199 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
200 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
201 end
4096
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 --- shell command
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
204 do
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
205 -- 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
206 -- 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
207
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
208 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
209
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
210 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
211
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
212 -- 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
213 console_env.invite = {};
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
214 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
215 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
216 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
217 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
218 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
219 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
220 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
221 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
222
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
223 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
224 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
225 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
226 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
227 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
228 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
229 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
230 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
231 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
232
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
233 --- prosodyctl command
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
234 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
235 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
236 print("usage: prosodyctl mod_"..module.name.." generate example.com");
4421
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
237 return 2;
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
238 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
239 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
240
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
241 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
242 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
243
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
244 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
245 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
246 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
247 table.remove(arg, 1); -- pop host
4342
84e60c3d6e61 mod_invites: Fix luacheck warning
Matthew Wild <mwild1@gmail.com>
parents: 4341
diff changeset
248 module.host = host; --luacheck: ignore 122/module
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
249 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
250
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
251 -- Load mod_invites
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
252 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
253 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
254 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
255 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
256 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
257
4421
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
258 local allow_reset;
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
259 local roles;
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
260 local groups = {};
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
261
4421
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
262 while #arg > 0 do
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
263 local value = arg[1];
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
264 table.remove(arg, 1);
4422
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
265 if value == "--help" then
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
266 print("usage: prosodyctl mod_"..module.name.." generate DOMAIN --reset USERNAME")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
267 print("usage: prosodyctl mod_"..module.name.." generate DOMAIN [--admin] [--role ROLE] [--group GROUPID]...")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
268 print()
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
269 print("This command has two modes: password reset and new account.")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
270 print("If --reset is given, the command operates in password reset mode and in new account mode otherwise.")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
271 print()
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
272 print("required arguments in password reset mode:")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
273 print()
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
274 print(" --reset USERNAME Generate a password reset link for the given USERNAME.")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
275 print()
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
276 print("optional arguments in new account mode:")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
277 print()
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
278 print(" --admin Make the new user privileged")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
279 print(" Equivalent to --role prosody:admin")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
280 print(" --role ROLE Grant the given ROLE to the new user")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
281 print(" --group GROUPID Add the user to the group with the given ID")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
282 print(" Can be specified multiple times")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
283 print()
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
284 print("--role and --admin override each other; the last one wins")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
285 print("--group can be specified multiple times; the user will be added to all groups.")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
286 print()
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
287 print("--reset and the other options cannot be mixed.")
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
288 return 2
2047dd56cc40 mod_invites: add extensive help message
Jonas Schäfer <jonas@wielicki.name>
parents: 4421
diff changeset
289 elseif value == "--reset" then
4421
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
290 local nodeprep = require "util.encodings".stringprep.nodeprep;
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
291 local username = nodeprep(arg[1])
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
292 table.remove(arg, 1);
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
293 if not username then
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
294 print("Please supply a valid username to generate a reset link for");
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
295 return 2;
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
296 end
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
297 allow_reset = username;
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
298 elseif value == "--admin" then
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
299 roles = { ["prosody:admin"] = true };
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
300 elseif value == "--role" then
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
301 local rolename = arg[1];
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
302 if not rolename then
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
303 print("Please supply a role name");
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
304 return 2;
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
305 end
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
306 roles = { [rolename] = true };
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
307 table.remove(arg, 1);
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
308 elseif value == "--group" or value == "-g" then
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
309 local groupid = arg[1];
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
310 if not groupid then
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
311 print("Please supply a group ID")
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
312 return 2;
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
313 end
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
314 table.insert(groups, groupid);
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
315 table.remove(arg, 1);
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
316 else
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
317 print("unexpected argument: "..value)
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
318 end
4421
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
319 end
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
320
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
321 local invite;
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
322 if allow_reset then
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
323 if roles then
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
324 print("--role/--admin and --reset are mutually exclusive")
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
325 return 2;
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
326 end
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
327 if #groups > 0 then
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
328 print("--group and --reset are mutually exclusive")
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
329 end
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
330 invite = assert(invites.create_account_reset(allow_reset));
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
331 else
4421
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
332 invite = assert(invites.create_account(nil, {
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
333 roles = roles,
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
334 groups = groups
94805a7e7b30 mod_invites: rework CLI parsing to support groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4377
diff changeset
335 }));
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
336 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
337
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
338 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
339 end