# HG changeset patch
# User Mike Yuan <me@yhndnzj.com>
# Date 1794538783 -3600
#      Fri Nov 13 03:59:43 2026 +0100
# Node ID 9388ffb0333c5fe5eed46ced503361cbd9cb0b91
# Parent  68c7b416d9b053157e0b812599415dcd5c96e331
mod_external_services: introduce ephemeral_credentials_cb

I've been working on a module to support Cloudflare's TURN
servers. It involves an API call to request ephermeral TURN
credentials, meaning the allocation/expiry is bound to
external resources. For the sake of efficiency a local cache
that maps jid -> credential is maintained inside the module,
which essentially entails two things:

- The callback must only be fired upon genuine extdisco events,
  not for basic sanity checks where no jid is associated with
  the request.
- Callback needs to be able to signal the caller that the request
  failed, and the service shall be tentatively pulled from the list.

Additionally, the API secrets are stored in the module itself,
so the callback should be invoked without dependency on a per-item
'secret' field.

Hence I opted for a new 'ephemeral_credentials_cb', which is
hooked into prepare() via two optional 'do_ephemeral' and 'full_jid'
params. The validity checks call into prepare() with do_ephemeral=false,
while get_services() enables support for ephemeral creds. For inspection
from shell commands, get_services() -> prepare() is invoked with
do_ephemeral=true but full_jid=nil, i.e. show the bare services
if no ephemeral credentials can be allocated.

diff --git a/plugins/mod_external_services.lua b/plugins/mod_external_services.lua
--- a/plugins/mod_external_services.lua
+++ b/plugins/mod_external_services.lua
@@ -39,7 +39,7 @@
 }
 
 -- filter config into well-defined service records
-local function prepare(item)
+local function prepare(item, do_ephemeral, full_jid)
 	if type(item) ~= "table" then
 		module:log("error", "Service definition is not a table: %q", item);
 		return nil;
@@ -90,6 +90,21 @@
 	elseif type(item.ttl) == "number" then
 		srv.expires = os.time() + item.ttl;
 	end
+	if item.ephemeral_credentials_cb then
+		if (item.password ~= nil or item.secret ~= nil or item.expires ~= nil) then
+			module:log("error", "Service definition contains both static credentials and 'ephemeral_credentials_cb': %q", item);
+			return nil;
+		end
+		if do_ephemeral then
+			local ok = item.ephemeral_credentials_cb(srv, item, full_jid);
+			if not ok and full_jid then
+				return nil;
+			end
+			-- if do_ephemeral is enabled without full_jid, assume inspection from shell
+			-- and serve records without credentials
+		end
+		srv.restricted = true;
+	end
 	if (item.secret == true and default_secret) or type(item.secret) == "string" then
 		local secret_cb = item.credentials_cb or algorithms[item.algorithm] or algorithms[srv.type];
 		local secret = item.secret;
@@ -130,9 +145,11 @@
 	end;
 }
 
-function get_services()
+function get_services(full_jid)
 	local extras = module:get_host_items("external_service");
-	local services = ( configured_services + extras ) / prepare;
+	local services = ( configured_services + extras ) / function(item)
+		return prepare(item, true, full_jid);
+	end;
 
 	setmetatable(services, services_mt);
 
@@ -173,7 +190,7 @@
 		return true;
 	end
 
-	local services = get_services();
+	local services = get_services(stanza.attr.from);
 
 	local requested_type = action.attr.type;
 	if requested_type then
@@ -204,7 +221,7 @@
 		return true;
 	end
 
-	local services = get_services();
+	local services = get_services(stanza.attr.from);
 	services:filter(function (item)
 		return item.restricted;
 	end)
