mod_external_services API
mod_external_services allows other modules to add items via the items API, as well as events.
Items API
Services added via the items API are subject to the same processing as those added via config file. Fields with the wrong type are ignored and time based credentials can be generated from a supplied secret.
Credentials, if required, can be supplied in several ways:
- Static
password
- Generated from a shared
secret
- Generated by a
credentials_cb()
function - Generated in
external_service/credentials
event (can be async)
:add_item("external_service", {
moduletype = "ftp",
= "tcp",
transport = "ftp.example.net",
host = 21,
port
= 300,
ttl
-- static credentials:
= "jdoe",
username = "changeme",
password
-- shared secret
= "secr3t",
secret = "turn",
algorithm
-- callback
= function (srv)
credentials_cb .username = random_username(),
srv.password = random_password(),
srvend
})
Obs! Only use one of the above credentials methods.
Other processing
For time-limited credentials, a ttl
field may be set to
the number of seconds that the credentials remain valid. It later gets
translated into an expires
timestamp.
Event API
Two kinds of events are fired, corresponding to the two commands described in XEP-0215.
external_service/services
- Request for list of services, possibly filtered by type.
external_service/credentials
- Request for credentials for a specific service.
Service record method
This method includes access to the origin session like other events, and thus arbitrary access control can be performed, or credentials could be based on the senders JID etc.
:hook_event("external_service/services", function(event)
moduleif event.requested_type == "ftp" then
.services:push({
eventtype = "ftp",
= "tcp",
transport = "ftp.example.net",
host = 21,
port = event.origin.username,
username = generate_temporary_password(event.origin.username),
password = true,
restricted = 300
ttl });
end
end);
Stanza format
Finally, the raw reply stanza can be manipulated in arbitrary ways.
local dt = require "util.datetime";
:hook_event("external_service/services", function(event)
moduleif event.requested_type == "ftp" then
.reply:tag("service", {
eventtype = "ftp",
= "tcp",
transport = "ftp.example.com,
host port = "21", -- Obs! string
username = event.origin.username,
password = generate_temporary_password(),
expires = dt.datetime(os.time() + 300),
restricted = "1",
}):up();
end
end);