Annotate

mod_invites/mod_invites.lua @ 4342:84e60c3d6e61

mod_invites: Fix luacheck warning
author Matthew Wild <mwild1@gmail.com>
date Sun, 17 Jan 2021 12:47:56 +0000
parent 4341:a104440c20a4
child 4344:844cfc8c4039
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
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 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
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
4077
f85ea76447dd mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 3776
diff changeset
22 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
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();
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 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
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;
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
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 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
45
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 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
47 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
48 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
49 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
50 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
51 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 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
55 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
56 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
57 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
58 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
59 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
60 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 return invite;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 -- Create invitation to 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
67 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
68 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
69 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
70 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
4078
2f0c8670d2fa mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4077
diff changeset
72 -- 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
73 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
74 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
75 end
2f0c8670d2fa mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4077
diff changeset
76
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 -- 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
78 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
79 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
80 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
4341
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
82 -- 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
83 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
84 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
85 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
86 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
87 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
88 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
89 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
90 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
91
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
92 function get_account_invite_info(token)
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
93 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
94 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
95 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
96
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
97 -- 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
98 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
99 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
100 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
101 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
102 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
103 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
104
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
105 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
106 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
107
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
108 function delete_account_invite(token)
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
109 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
110 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
111 end
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
112
a104440c20a4 mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents: 4096
diff changeset
113 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
114 end
4080
14a3f5223074 mod_invites: Whitespace (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4079
diff changeset
115
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 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
117 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
118
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 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
120 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
121 -- 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
122 -- 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
123 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
124 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 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
126
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 return true;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 -- 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
131 -- 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
132 -- 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
133 -- 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
134 -- 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
135 -- 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
136 -- 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
137 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
138 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
139 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
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 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
143
4079
b68b917e568f mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents: 4078
diff changeset
144 -- 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
145 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
146
3776
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 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
148 -- 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
149 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
150 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
151 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
152 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
153 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
154 inviter = username;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 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
159 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
160 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
161 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
162 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
163 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
164 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 return setmetatable({
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 token = token;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 username = username;
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 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
170 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
171 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
172 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
173 }, valid_invite_mt);
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 end
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175
80830d97da81 mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 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
177 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
178 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
179 end
4096
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
180
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
181 --- shell command
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
182 do
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
183 -- 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
184 -- 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
185
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
186 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
187
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
188 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
189
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
190 -- 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
191 console_env.invite = {};
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
192 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
193 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
194 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
195 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
196 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
197 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
198 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
199 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
200
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
201 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
202 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
203 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
204 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
205 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
206 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
207 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
208 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
209 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
210
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
211 --- prosodyctl command
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
212 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
213 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
214 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
215 return;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
216 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
217 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
218
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
219 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
220 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
221
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
222 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
223 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
224 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
225 table.remove(arg, 1); -- pop host
4342
84e60c3d6e61 mod_invites: Fix luacheck warning
Matthew Wild <mwild1@gmail.com>
parents: 4341
diff changeset
226 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
227 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
228
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
229 -- Load mod_invites
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
230 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
231 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
232 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
233 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
234 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
235
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
236
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
237 local invite, roles;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
238 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
239 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
240 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
241 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
242 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
243 return;
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
244 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
245 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
246 else
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
247 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
248 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
249 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
250 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
251 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
252 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
253 end
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
254
24f4eb35ab60 mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents: 4081
diff changeset
255 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
256 end