Annotate

plugins/mod_external_services.lua @ 11710:26a8cc9d9eb7

net.resolvers.service: Only do DANE with secure SRV records If this seems backwards, that' because it is but the API isn't really designed to easily pass along details from each resolution step onto the next.
author Kim Alvefur <zash@zash.se>
date Sun, 18 Jul 2021 23:25:45 +0200
parent 11628:0807e835d3b5
child 11753:c4599a7c534c
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";
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 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
10 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
11 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
12 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
13
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 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
15
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 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
17
11038
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
18 -- 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
19 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
20 local ttl = default_ttl;
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
21 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
22 ttl = item.ttl;
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
23 end
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
24 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
25 local username;
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
26 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
27 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
28 else
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
29 username = string.format("%d", expires);
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
30 end
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
31 srv.username = username;
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
32 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
33 end
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
34
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
35 local algorithms = {
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
36 turn = behave_turn_rest_credentials;
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
37 }
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
38
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 -- filter config into well-defined service records
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local function prepare(item)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 if type(item) ~= "table" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 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
43 return nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 local srv = {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 type = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 transport = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 host = default_host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 port = default_port;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 username = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 password = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 restricted = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 expires = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 };
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 if type(item.type) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 srv.type = item.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 else
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 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
61 return nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 if type(item.transport) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 srv.transport = item.transport;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 if type(item.host) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 srv.host = item.host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 if type(item.port) == "number" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 srv.port = item.port;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 if type(item.username) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 srv.username = item.username;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 if type(item.password) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 srv.password = item.password;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 srv.restricted = true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 if item.restricted == true then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 srv.restricted = true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 if type(item.expires) == "number" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 srv.expires = item.expires;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 elseif type(item.ttl) == "number" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 srv.expires = os.time() + item.ttl;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 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
88 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
89 local secret = item.secret;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 if secret == true then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 secret = default_secret;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 end
11038
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
93 if secret_cb then
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
94 secret_cb(srv, item, secret);
efefdf71373b mod_external_services: Prepare to allow more credential algorithms
Kim Alvefur <zash@zash.se>
parents: 11037
diff changeset
95 srv.restricted = true;
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 end
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 return srv;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 function module.load()
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 -- 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
103 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
104 local services = ( configured_services + extras ) / prepare;
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 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
106 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
107 end
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
11627
3a5212fd7e8e mod_external_services: Validate items as they are added
Kim Alvefur <zash@zash.se>
parents: 11626
diff changeset
110 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
111 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
112 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
113 end
11627
3a5212fd7e8e mod_external_services: Validate items as they are added
Kim Alvefur <zash@zash.se>
parents: 11626
diff changeset
114 end, module.load);
3a5212fd7e8e mod_external_services: Validate items as they are added
Kim Alvefur <zash@zash.se>
parents: 11626
diff changeset
115
11040
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
116 -- 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
117 local services_mt = {
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
118 __index = getmetatable(array()).__index;
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
119 __newindex = function (self, i, v)
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
120 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
121 end;
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
122 }
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
123
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 local function handle_services(event)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 local origin, stanza = event.origin, event.stanza;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 local action = stanza.tags[1];
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 local user_bare = jid.bare(stanza.attr.from);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 local user_host = jid.host(user_bare);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 if not ((access:empty() and origin.type == "c2s") or access:contains(user_bare) or access:contains(user_host)) then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 origin.send(st.error_reply(stanza, "auth", "forbidden"));
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 return true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 local reply = st.reply(stanza):tag("services", { xmlns = action.attr.xmlns });
11037
936ee55e1ae3 mod_external_services: Support adding services via items API
Kim Alvefur <zash@zash.se>
parents: 11036
diff changeset
136 local extras = module:get_host_items("external_service");
936ee55e1ae3 mod_external_services: Support adding services via items API
Kim Alvefur <zash@zash.se>
parents: 11036
diff changeset
137 local services = ( configured_services + extras ) / prepare;
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 local requested_type = action.attr.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 if requested_type then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 services:filter(function(item)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 return item.type == requested_type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 end);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145
11040
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
146 setmetatable(services, services_mt);
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
147
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 module:fire_event("external_service/services", {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 origin = origin;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 stanza = stanza;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 reply = reply;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 requested_type = requested_type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 services = services;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 });
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 for _, srv in ipairs(services) do
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 reply:tag("service", {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 type = srv.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 transport = srv.transport;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 host = srv.host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 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
162 username = srv.username;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 password = srv.password;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 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
165 restricted = srv.restricted and "1" or nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 }):up();
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 origin.send(reply);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 return true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 local function handle_credentials(event)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 local origin, stanza = event.origin, event.stanza;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 local action = stanza.tags[1];
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 if origin.type ~= "c2s" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 origin.send(st.error_reply(stanza, "auth", "forbidden"));
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 return true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 local reply = st.reply(stanza):tag("credentials", { xmlns = action.attr.xmlns });
11037
936ee55e1ae3 mod_external_services: Support adding services via items API
Kim Alvefur <zash@zash.se>
parents: 11036
diff changeset
183 local extras = module:get_host_items("external_service");
936ee55e1ae3 mod_external_services: Support adding services via items API
Kim Alvefur <zash@zash.se>
parents: 11036
diff changeset
184 local services = ( configured_services + extras ) / prepare;
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 services:filter(function (item)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 return item.restricted;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 end)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 local requested_credentials = {};
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 for service in action:childtags("service") do
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 table.insert(requested_credentials, {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 type = service.attr.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 host = service.attr.host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 port = tonumber(service.attr.port);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 });
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197
11040
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
198 setmetatable(services, services_mt);
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
199 setmetatable(requested_credentials, services_mt);
c560531d9a6e mod_external_services: Validate services added via events
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
200
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 module:fire_event("external_service/credentials", {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 origin = origin;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 stanza = stanza;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 reply = reply;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 requested_credentials = requested_credentials;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 services = services;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 });
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 for req_srv in action:childtags("service") do
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 for _, srv in ipairs(services) do
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 if srv.type == req_srv.attr.type and srv.host == req_srv.attr.host
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 and not req_srv.attr.port or srv.port == tonumber(req_srv.attr.port) then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 reply:tag("service", {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 type = srv.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 transport = srv.transport;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 host = srv.host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 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
218 username = srv.username;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 password = srv.password;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 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
221 restricted = srv.restricted and "1" or nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 }):up();
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 origin.send(reply);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 return true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 -- XEP-0215 v0.7
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 module:add_feature("urn:xmpp:extdisco:2");
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:services", handle_services);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 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
235
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 -- COMPAT XEP-0215 v0.6
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 -- 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
238 module:add_feature("urn:xmpp:extdisco:1");
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:services", handle_services);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 module:hook("iq-get/host/urn:xmpp:extdisco:1:credentials", handle_credentials);