You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mod_external_services.lua 6.7KB

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