Annotate

plugins/mod_external_services.lua @ 11906:ba3344926e18

MUC: Add option to include form in registration query This was originally not done based on my interpretation of XEP-0045. Today's reading, however, revealed that it actually says the result > SHOULD contain **at least** a <username/> element (emphasis mine) I take this to mean that including a form **is** allowed (and I think this is sensible). Tigase already includes the form I believe. I've gated the new behaviour behind a (default off) option, because it hasn't been tested for compatibility with clients. My primary desire for it is in Snikket, where the clients will be tested to ensure compatibility with this. I don't anticipate that (m)any clients would break, so maybe after 0.12 we can experiment with enabling it by default and eventually remove the option.
author Matthew Wild <mwild1@gmail.com>
date Mon, 15 Nov 2021 16:11:03 +0000
parent 11756:a0e17b7c8b05
child 11933:f752427a5214
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local dt = require "util.datetime";
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local base64 = require "util.encodings".base64;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local hashes = require "util.hashes";
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local st = require "util.stanza";
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local jid = require "util.jid";
11040
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
7 local array = require "util.array";
11754
21a9b3f2a728 mod_external_services: Filter services by requested credentials using a Set
Kim Alvefur <zash@zash.se>
parents: 11753
diff changeset
8 local set = require "util.set";
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local default_host = module:get_option_string("external_service_host", module.host);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local default_port = module:get_option_number("external_service_port");
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local default_secret = module:get_option_string("external_service_secret");
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local default_ttl = module:get_option_number("external_service_ttl", 86400);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local configured_services = module:get_option_array("external_services", {});
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local access = module:get_option_set("external_service_access", {});
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
11038
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
19 -- https://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
20 local function behave_turn_rest_credentials(srv, item, secret)
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
21 local ttl = default_ttl;
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
22 if type(item.ttl) == "number" then
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
23 ttl = item.ttl;
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
24 end
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
25 local expires = srv.expires or os.time() + ttl;
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
26 local username;
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
27 if type(item.username) == "string" then
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
28 username = string.format("%d:%s", expires, item.username);
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
29 else
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
30 username = string.format("%d", expires);
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
31 end
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
32 srv.username = username;
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
33 srv.password = base64.encode(hashes.hmac_sha1(secret, srv.username));
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
34 end
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
35
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
36 local algorithms = {
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
37 turn = behave_turn_rest_credentials;
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
38 }
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
39
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 -- filter config into well-defined service records
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local function prepare(item)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 if type(item) ~= "table" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 module:log("error", "Service definition is not a table: %q", item);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 local srv = {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 type = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 transport = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 host = default_host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 port = default_port;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 username = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 password = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 restricted = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 expires = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 };
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 if type(item.type) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 srv.type = item.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 else
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 module:log("error", "Service missing mandatory 'type' field: %q", item);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 return nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 if type(item.transport) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 srv.transport = item.transport;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 if type(item.host) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 srv.host = item.host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 if type(item.port) == "number" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 srv.port = item.port;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 if type(item.username) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 srv.username = item.username;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 if type(item.password) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 srv.password = item.password;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 srv.restricted = true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 if item.restricted == true then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 srv.restricted = true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 if type(item.expires) == "number" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 srv.expires = item.expires;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 elseif type(item.ttl) == "number" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 srv.expires = os.time() + item.ttl;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 if (item.secret == true and default_secret) or type(item.secret) == "string" then
11039
ec6919401790 mod_external_services: Allow specifying a credential generation callback
Kim Alvefur <zash@zash.se>
parents: 11038
diff changeset
89 local secret_cb = item.credentials_cb or algorithms[item.algorithm] or algorithms[srv.type];
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 local secret = item.secret;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 if secret == true then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 secret = default_secret;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end
11038
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
94 if secret_cb then
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
95 secret_cb(srv, item, secret);
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
96 srv.restricted = true;
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 return srv;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 function module.load()
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 -- Trigger errors on startup
11626
ef62d29c8fdc mod_external_services: Also validate services added by other modules
Kim Alvefur <zash@zash.se>
parents: 11040
diff changeset
104 local extras = module:get_host_items("external_service");
ef62d29c8fdc mod_external_services: Also validate services added by other modules
Kim Alvefur <zash@zash.se>
parents: 11040
diff changeset
105 local services = ( configured_services + extras ) / prepare;
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 if #services == 0 then
11628
0807e835d3b5 mod_external_services: Report overall status as a module status
Kim Alvefur <zash@zash.se>
parents: 11627
diff changeset
107 module:set_status("warn", "No services configured or all had errors");
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110
11627
3a5212fd7e8e mod_external_services: Validate items as they are added
Kim Alvefur <zash@zash.se>
parents: 11626
diff changeset
111 module:handle_items("external_service", function(added)
11628
0807e835d3b5 mod_external_services: Report overall status as a module status
Kim Alvefur <zash@zash.se>
parents: 11627
diff changeset
112 if prepare(added.item) then
0807e835d3b5 mod_external_services: Report overall status as a module status
Kim Alvefur <zash@zash.se>
parents: 11627
diff changeset
113 module:set_status("core", "OK");
0807e835d3b5 mod_external_services: Report overall status as a module status
Kim Alvefur <zash@zash.se>
parents: 11627
diff changeset
114 end
11627
3a5212fd7e8e mod_external_services: Validate items as they are added
Kim Alvefur <zash@zash.se>
parents: 11626
diff changeset
115 end, module.load);
3a5212fd7e8e mod_external_services: Validate items as they are added
Kim Alvefur <zash@zash.se>
parents: 11626
diff changeset
116
11040
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
117 -- Ensure only valid items are added in events
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
118 local services_mt = {
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
119 __index = getmetatable(array()).__index;
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
120 __newindex = function (self, i, v)
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
121 rawset(self, i, assert(prepare(v), "Invalid service entry added"));
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
122 end;
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
123 }
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
124
11755
ae565e49289a mod_external_services: Factor out public function returning current services
Kim Alvefur <zash@zash.se>
parents: 11754
diff changeset
125 function get_services()
ae565e49289a mod_external_services: Factor out public function returning current services
Kim Alvefur <zash@zash.se>
parents: 11754
diff changeset
126 local extras = module:get_host_items("external_service");
ae565e49289a mod_external_services: Factor out public function returning current services
Kim Alvefur <zash@zash.se>
parents: 11754
diff changeset
127 local services = ( configured_services + extras ) / prepare;
ae565e49289a mod_external_services: Factor out public function returning current services
Kim Alvefur <zash@zash.se>
parents: 11754
diff changeset
128
ae565e49289a mod_external_services: Factor out public function returning current services
Kim Alvefur <zash@zash.se>
parents: 11754
diff changeset
129 setmetatable(services, services_mt);
ae565e49289a mod_external_services: Factor out public function returning current services
Kim Alvefur <zash@zash.se>
parents: 11754
diff changeset
130
ae565e49289a mod_external_services: Factor out public function returning current services
Kim Alvefur <zash@zash.se>
parents: 11754
diff changeset
131 return services;
ae565e49289a mod_external_services: Factor out public function returning current services
Kim Alvefur <zash@zash.se>
parents: 11754
diff changeset
132 end
ae565e49289a mod_external_services: Factor out public function returning current services
Kim Alvefur <zash@zash.se>
parents: 11754
diff changeset
133
11756
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
134 function services_xml(services, name, namespace)
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
135 local reply = st.stanza(name or "services", { xmlns = namespace or "urn:xmpp:extdisco:2" });
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 for _, srv in ipairs(services) do
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 reply:tag("service", {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 type = srv.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 transport = srv.transport;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 host = srv.host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 port = srv.port and string.format("%d", srv.port) or nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 username = srv.username;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 password = srv.password;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 expires = srv.expires and dt.datetime(srv.expires) or nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 restricted = srv.restricted and "1" or nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 }):up();
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149
11756
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
150 return reply;
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
151 end
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
152
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
153 local function handle_services(event)
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
154 local origin, stanza = event.origin, event.stanza;
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
155 local action = stanza.tags[1];
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
156
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
157 local user_bare = jid.bare(stanza.attr.from);
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
158 local user_host = jid.host(user_bare);
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
159 if not ((access:empty() and origin.type == "c2s") or access:contains(user_bare) or access:contains(user_host)) then
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
160 origin.send(st.error_reply(stanza, "auth", "forbidden"));
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
161 return true;
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
162 end
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
163
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
164 local services = get_services();
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
165
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
166 local requested_type = action.attr.type;
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
167 if requested_type then
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
168 services:filter(function(item)
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
169 return item.type == requested_type;
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
170 end);
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
171 end
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
172
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
173 module:fire_event("external_service/services", {
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
174 origin = origin;
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
175 stanza = stanza;
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
176 requested_type = requested_type;
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
177 services = services;
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
178 });
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
179
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
180 local reply = st.reply(stanza):add_child(services_xml(services, action.name, action.attr.xmlns));
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
181
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 origin.send(reply);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 return true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 local function handle_credentials(event)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 local origin, stanza = event.origin, event.stanza;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 local action = stanza.tags[1];
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 if origin.type ~= "c2s" then
11753
c4599a7c534c mod_external_services: Validate required attributes on credentials requests
Kim Alvefur <zash@zash.se>
parents: 11628
diff changeset
191 origin.send(st.error_reply(stanza, "auth", "forbidden", "The 'port' and 'type' attributes are required."));
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 return true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194
11755
ae565e49289a mod_external_services: Factor out public function returning current services
Kim Alvefur <zash@zash.se>
parents: 11754
diff changeset
195 local services = get_services();
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 services:filter(function (item)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 return item.restricted;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 end)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199
11754
21a9b3f2a728 mod_external_services: Filter services by requested credentials using a Set
Kim Alvefur <zash@zash.se>
parents: 11753
diff changeset
200 local requested_credentials = set.new();
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 for service in action:childtags("service") do
11753
c4599a7c534c mod_external_services: Validate required attributes on credentials requests
Kim Alvefur <zash@zash.se>
parents: 11628
diff changeset
202 if not service.attr.type or not service.attr.host then
c4599a7c534c mod_external_services: Validate required attributes on credentials requests
Kim Alvefur <zash@zash.se>
parents: 11628
diff changeset
203 origin.send(st.error_reply(stanza, "modify", "bad-request"));
c4599a7c534c mod_external_services: Validate required attributes on credentials requests
Kim Alvefur <zash@zash.se>
parents: 11628
diff changeset
204 return true;
c4599a7c534c mod_external_services: Validate required attributes on credentials requests
Kim Alvefur <zash@zash.se>
parents: 11628
diff changeset
205 end
c4599a7c534c mod_external_services: Validate required attributes on credentials requests
Kim Alvefur <zash@zash.se>
parents: 11628
diff changeset
206
11754
21a9b3f2a728 mod_external_services: Filter services by requested credentials using a Set
Kim Alvefur <zash@zash.se>
parents: 11753
diff changeset
207 requested_credentials:add(string.format("%s:%s:%d", service.attr.type, service.attr.host,
21a9b3f2a728 mod_external_services: Filter services by requested credentials using a Set
Kim Alvefur <zash@zash.se>
parents: 11753
diff changeset
208 tonumber(service.attr.port) or 0));
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 module:fire_event("external_service/credentials", {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 origin = origin;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 stanza = stanza;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 requested_credentials = requested_credentials;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 services = services;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 });
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217
11754
21a9b3f2a728 mod_external_services: Filter services by requested credentials using a Set
Kim Alvefur <zash@zash.se>
parents: 11753
diff changeset
218 services:filter(function (srv)
21a9b3f2a728 mod_external_services: Filter services by requested credentials using a Set
Kim Alvefur <zash@zash.se>
parents: 11753
diff changeset
219 local port_key = string.format("%s:%s:%d", srv.type, srv.host, srv.port or 0);
21a9b3f2a728 mod_external_services: Filter services by requested credentials using a Set
Kim Alvefur <zash@zash.se>
parents: 11753
diff changeset
220 local portless_key = string.format("%s:%s:%d", srv.type, srv.host, 0);
21a9b3f2a728 mod_external_services: Filter services by requested credentials using a Set
Kim Alvefur <zash@zash.se>
parents: 11753
diff changeset
221 return requested_credentials:contains(port_key) or requested_credentials:contains(portless_key);
21a9b3f2a728 mod_external_services: Filter services by requested credentials using a Set
Kim Alvefur <zash@zash.se>
parents: 11753
diff changeset
222 end);
21a9b3f2a728 mod_external_services: Filter services by requested credentials using a Set
Kim Alvefur <zash@zash.se>
parents: 11753
diff changeset
223
11756
a0e17b7c8b05 mod_external_services: Factor out public function for converting to XML
Kim Alvefur <zash@zash.se>
parents: 11755
diff changeset
224 local reply = st.reply(stanza):add_child(services_xml(services, action.name, action.attr.xmlns));
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 origin.send(reply);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 return true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 -- XEP-0215 v0.7
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 module:add_feature("urn:xmpp:extdisco:2");
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 module:hook("iq-get/host/urn:xmpp:extdisco:2:services", handle_services);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 module:hook("iq-get/host/urn:xmpp:extdisco:2:credentials", handle_credentials);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 -- COMPAT XEP-0215 v0.6
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 -- Those still on the old version gets to deal with undefined attributes until they upgrade.
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 module:add_feature("urn:xmpp:extdisco:1");
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 module:hook("iq-get/host/urn:xmpp:extdisco:1:services", handle_services);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 module:hook("iq-get/host/urn:xmpp:extdisco:1:credentials", handle_credentials);