Mercurial > prosody-modules
annotate mod_external_services/mod_external_services.lua @ 4517:d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Requires the most recent of the recent prosody-trunks.
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Mon, 22 Mar 2021 21:32:43 +0100 |
parents | 4841cf3fded5 |
children | f0ffa8cf3ce6 |
rev | line source |
---|---|
4075
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local dt = require "util.datetime"; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local base64 = require "util.encodings".base64; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local hashes = require "util.hashes"; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local st = require "util.stanza"; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local jid = require "util.jid"; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local array = require "util.array"; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local default_host = module:get_option_string("external_service_host", module.host); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local default_port = module:get_option_number("external_service_port"); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local default_secret = module:get_option_string("external_service_secret"); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local default_ttl = module:get_option_number("external_service_ttl", 86400); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local configured_services = module:get_option_array("external_services", {}); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local access = module:get_option_set("external_service_access", {}); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 -- https://tools.ietf.org/html/draft-uberti-behave-turn-rest-00 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local function behave_turn_rest_credentials(srv, item, secret) |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 local ttl = default_ttl; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 if type(item.ttl) == "number" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 ttl = item.ttl; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 local expires = srv.expires or os.time() + ttl; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 local username; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 if type(item.username) == "string" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 username = string.format("%d:%s", expires, item.username); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 else |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 username = string.format("%d", expires); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 srv.username = username; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 srv.password = base64.encode(hashes.hmac_sha1(secret, srv.username)); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 local algorithms = { |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 turn = behave_turn_rest_credentials; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 } |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 -- filter config into well-defined service records |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 local function prepare(item) |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 if type(item) ~= "table" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 module:log("error", "Service definition is not a table: %q", item); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 return nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 local srv = { |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 type = nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 transport = nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 host = default_host; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 port = default_port; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 username = nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 password = nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 restricted = nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 expires = nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 }; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 if type(item.type) == "string" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 srv.type = item.type; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 else |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 module:log("error", "Service missing mandatory 'type' field: %q", item); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 return nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 if type(item.transport) == "string" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 srv.transport = item.transport; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 if type(item.host) == "string" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 srv.host = item.host; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 if type(item.port) == "number" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 srv.port = item.port; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 if type(item.username) == "string" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 srv.username = item.username; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 if type(item.password) == "string" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 srv.password = item.password; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 srv.restricted = true; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 if item.restricted == true then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 srv.restricted = true; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 if type(item.expires) == "number" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 srv.expires = item.expires; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 elseif type(item.ttl) == "number" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 srv.expires = os.time() + item.ttl; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 if (item.secret == true and default_secret) or type(item.secret) == "string" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 local secret_cb = item.credentials_cb or algorithms[item.algorithm] or algorithms[srv.type]; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 local secret = item.secret; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 if secret == true then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 secret = default_secret; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 if secret_cb then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 secret_cb(srv, item, secret); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 srv.restricted = true; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 return srv; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 function module.load() |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 -- Trigger errors on startup |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 local services = configured_services / prepare; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 if #services == 0 then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 module:log("warn", "No services configured or all had errors"); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 -- Ensure only valid items are added in events |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 local services_mt = { |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 __index = getmetatable(array()).__index; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 __newindex = function (self, i, v) |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 rawset(self, i, assert(prepare(v), "Invalid service entry added")); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 end; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 } |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 local function handle_services(event) |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 local origin, stanza = event.origin, event.stanza; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 local action = stanza.tags[1]; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 local user_bare = jid.bare(stanza.attr.from); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 local user_host = jid.host(user_bare); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 if not ((access:empty() and origin.type == "c2s") or access:contains(user_bare) or access:contains(user_host)) then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 origin.send(st.error_reply(stanza, "auth", "forbidden")); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 return true; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 local reply = st.reply(stanza):tag("services", { xmlns = action.attr.xmlns }); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 local extras = module:get_host_items("external_service"); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 local services = ( configured_services + extras ) / prepare; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 local requested_type = action.attr.type; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 if requested_type then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 services:filter(function(item) |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 return item.type == requested_type; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 end); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 setmetatable(services, services_mt); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 module:fire_event("external_service/services", { |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 origin = origin; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 stanza = stanza; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 reply = reply; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 requested_type = requested_type; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 services = services; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 }); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 for _, srv in ipairs(services) do |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 reply:tag("service", { |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 type = srv.type; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 transport = srv.transport; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 host = srv.host; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 port = srv.port and string.format("%d", srv.port) or nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 username = srv.username; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 password = srv.password; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 expires = srv.expires and dt.datetime(srv.expires) or nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 restricted = srv.restricted and "1" or nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 }):up(); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 origin.send(reply); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 return true; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 local function handle_credentials(event) |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 local origin, stanza = event.origin, event.stanza; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 local action = stanza.tags[1]; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 if origin.type ~= "c2s" then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 origin.send(st.error_reply(stanza, "auth", "forbidden")); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 return true; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 local reply = st.reply(stanza):tag("credentials", { xmlns = action.attr.xmlns }); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 local extras = module:get_host_items("external_service"); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 local services = ( configured_services + extras ) / prepare; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 services:filter(function (item) |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 return item.restricted; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 end) |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 local requested_credentials = {}; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 for service in action:childtags("service") do |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 table.insert(requested_credentials, { |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 type = service.attr.type; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 host = service.attr.host; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 port = tonumber(service.attr.port); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 }); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 setmetatable(services, services_mt); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 setmetatable(requested_credentials, services_mt); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 module:fire_event("external_service/credentials", { |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 origin = origin; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 stanza = stanza; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 reply = reply; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 requested_credentials = requested_credentials; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 services = services; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 }); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 for req_srv in action:childtags("service") do |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 for _, srv in ipairs(services) do |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 if srv.type == req_srv.attr.type and srv.host == req_srv.attr.host |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 and not req_srv.attr.port or srv.port == tonumber(req_srv.attr.port) then |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 reply:tag("service", { |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 type = srv.type; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 transport = srv.transport; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 host = srv.host; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 port = srv.port and string.format("%d", srv.port) or nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 username = srv.username; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 password = srv.password; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 expires = srv.expires and dt.datetime(srv.expires) or nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 restricted = srv.restricted and "1" or nil; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 }):up(); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
216 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
220 origin.send(reply); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 return true; |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 end |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 -- XEP-0215 v0.7 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
225 module:add_feature("urn:xmpp:extdisco:2"); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 module:hook("iq-get/host/urn:xmpp:extdisco:2:services", handle_services); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
227 module:hook("iq-get/host/urn:xmpp:extdisco:2:credentials", handle_credentials); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 -- COMPAT XEP-0215 v0.6 |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 -- Those still on the old version gets to deal with undefined attributes until they upgrade. |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
231 module:add_feature("urn:xmpp:extdisco:1"); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 module:hook("iq-get/host/urn:xmpp:extdisco:1:services", handle_services); |
4841cf3fded5
mod_external_service: Include for users of older versions of Prosody
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 module:hook("iq-get/host/urn:xmpp:extdisco:1:credentials", handle_credentials); |