Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

9.8 KB
Raw Blame
-- USAGE / DEPLOYMENT
--   This is a server-to-server endpoint. It is meant to be called only by
--   trusted backend services over a controlled network path (e.g. Prosody's
--   HTTP port reachable only from internal hosts). It is NOT safe to make it
--   publicly accessible: do not proxy it from the public web server (nginx)
--   and do not expose Prosody's HTTP ports (5280/5281) to the internet.
--
--   The Bearer token is a system token signed with a dedicated key pair whose
--   public keys are served from prosody_password_public_key_repo_url. That
--   option must be set; the private key must only be available to the calling
--   services. A valid system token authorizes the caller for every room on
--   the deployment. It is intentionally not bound to a room: there is no
--   room claim / room ownership check, the calling service is responsible
--   for deciding which room it acts on.
--
-- HTTP module that exposes a POST /invite-jigasi endpoint for inviting a Jigasi
-- SIP gateway instance to dial out to a phone number and connect it to a
-- conference. Intended for internal system use (e.g. by a backend service),
-- not for end-user clients.
--
-- Authentication uses a SEPARATE ASAP key pair from the one used for login
-- tokens (mod_auth_token). The key server URL is read from
-- prosody_password_public_key_repo_url (not asap_key_server), so login tokens
-- are not accepted.
--
-- Request format:
--   POST /invite-jigasi
--   Content-Type: application/json
--   Authorization: Bearer <system-token>
--   Body: { "conference": "<room-jid>", "phoneNo": "<dial-target>" }
--
-- The module selects a Jigasi instance from the brewery room
-- (muc_jigasi_brewery_jid, default: [email protected].<base>):
--   1. Iterates brewery occupants, skips focus.
--   2. Filters for occupants with supports_sip=true in their colibri stats.
--   3. Among those, picks the one with the lowest stress_level.
--   4. Sends a Rayo <dial> IQ to that occupant (impersonating focus) with
--      phoneNo as the dial target and conference as JvbRoomName.
--
-- Responses:
--   200  Jigasi invited successfully.
--   400  Missing or invalid parameters / wrong Content-Type.
--   401  Missing, malformed, or unverifiable token.
--   404  Brewery room not found, or no SIP-capable Jigasi available.
--
-- Copyright (C) 2023-present 8x8, Inc.

local hashes = require "util.hashes";
local random = require "util.random";
local st = require("util.stanza");
local json = require 'cjson.safe';
local util = module:require "util";
local async_handler_wrapper = util.async_handler_wrapper;
local is_focus = util.is_focus;
local process_host_module = util.process_host_module;

local muc_domain_base = module:get_option_string("muc_mapper_domain_base");

-- This module chooses jigasi from the brewery room, so it needs information for the configured brewery
local muc_domain = module:get_option_string("muc_internal_domain_base", 'internal.auth.' .. muc_domain_base);

local jigasi_brewery_room_jid = module:get_option_string("muc_jigasi_brewery_jid", 'jigasibrewery@' .. muc_domain);

local focus_jid = module:get_option_string("muc_jicofo_brewery_jid", jigasi_brewery_room_jid .. "/focus");

local main_muc_service;
local JSON_CONTENT_TYPE = "application/json";

local event_count = module:measure("muc_invite_jigasi_rate", "rate")
local event_count_success = module:measure("muc_invite_jigasi_success", "rate")
local ASAP_KEY_SERVER = module:get_option_string("prosody_password_public_key_repo_url", "");
local token_util = module:require "token/util".new(module);
if ASAP_KEY_SERVER == "" then
    module:log("warn", "No 'prosody_password_public_key_repo_url' option set, disabling invite-jigasi endpoint.");
    return;
end
-- init token util with our asap keyserver
token_util:set_asap_key_server(ASAP_KEY_SERVER)

local function invite_jigasi(conference, phone_no)
    local jigasi_brewery_room = main_muc_service.get_room_from_jid(jigasi_brewery_room_jid);
    if not jigasi_brewery_room then
        module:log("error", "Jigasi brewery room not found")
        return 404, 'Brewery room was not found'
    end
    module:log("info", "Invite jigasi from %s to join conference %s and outbound phone_no %s", jigasi_brewery_room.jid, conference, phone_no)

    --select least stressed Jigasi
    local least_stressed_value = math.huge;
    local least_stressed_jigasi_occupant;
    for occupant_jid, occupant in jigasi_brewery_room:each_occupant() do
        if not is_focus(occupant_jid) then
            local occ = occupant:get_presence();
            local stats_child = occ:get_child("stats", "http://jitsi.org/protocol/colibri")

            if not stats_child then
                module:log("warn", "Jigasi occupant %s has no stats element, skipping", occupant_jid);
            else
                local is_sip_jigasi = true;
                for stats_tag in stats_child:children() do
                    if stats_tag.attr.name == 'supports_sip' and stats_tag.attr.value == 'false' then
                        is_sip_jigasi = false;
                    end
                end

                if is_sip_jigasi then
                    for stats_tag in stats_child:children() do
                        if stats_tag.attr.name == 'stress_level' then
                            local stress_level = tonumber(stats_tag.attr.value);
                            module:log("debug", "Stressed level %s %s ", stress_level, occupant_jid)
                            if stress_level < least_stressed_value then
                                least_stressed_jigasi_occupant = occupant;
                                least_stressed_value = stress_level
                            end
                        end
                    end
                end
            end
        end
    end
    if not least_stressed_jigasi_occupant then
        module:log("error", "Cannot invite jigasi from room %s", jigasi_brewery_room.jid)
        return 404, 'Jigasi not found'
    end
    module:log("debug", "Least stressed jigasi selected jid %s value %s", least_stressed_jigasi_occupant.jid, least_stressed_value)

    -- invite Jigasi to join the conference
    local stanza_id = hashes.sha256(random.bytes(8), true);
    local invite_jigasi_stanza = st.iq({ xmlns = "jabber:client", type = "set", to = least_stressed_jigasi_occupant.jid, from = focus_jid, id = stanza_id })
                                   :tag("dial", { xmlns = "urn:xmpp:rayo:1", from = "fromnumber", to = phone_no })
                                   :tag("header", { xmlns = "urn:xmpp:rayo:1", name = "JvbRoomName", value = conference })

    module:log("debug", "Invite jigasi stanza %s", invite_jigasi_stanza)
    jigasi_brewery_room:route_stanza(invite_jigasi_stanza);
    return 200
end

local function is_token_valid(token)
    if token == nil then
        module:log("warn", "no token provided");
        return false;
    end

    local session = {};
    session.auth_token = token;
    local verified, reason, msg = token_util:process_and_verify_token(session);
    if not verified then
        module:log("warn", "not a valid token %s %s", tostring(reason), tostring(msg));
        return false;
    end
    return true;
end

local function handle_jigasi_invite(event)
    module:log("debug", "Request for invite jigasi received: reqId %s", event.request.headers["request_id"])
    event_count()
    local request = event.request;
    -- verify access
    local token = event.request.headers["authorization"]
    if not token then
        module:log("error", "Authorization header was not provided for conference %s", conference)
        return { status_code = 401 };
    end
    if util.starts_with(token, 'Bearer ') then
        token = token:sub(8, #token)
    else
        module:log("error", "Authorization header is invalid")
        return { status_code = 401 };
    end
    if not is_token_valid(token) then
        return { status_code = 401 };
    end

    -- verify payload
    if request.headers.content_type ~= JSON_CONTENT_TYPE
            or (not request.body or #request.body == 0) then
        module:log("warn", "Wrong content type: %s or missing payload", request.headers.content_type);
        return { status_code = 400; }
    end
    local payload, error = json.decode(request.body);

    if not payload then
        module:log('error', 'Cannot decode json error:%s', error);
        return { status_code = 400; }
    end

    local conference = payload["conference"];
    local phone_no = payload["phoneNo"];
    if not conference then
        module:log("warn", "Missing conference param")
        return { status_code = 400; }
    end
    if not phone_no then
        module:log("warn", "Missing phone no param")
        return { status_code = 400; }
    end

    --invite jigasi
    local status_code, error_msg = invite_jigasi(conference, phone_no)

    if not error_msg then
        event_count_success()
        return { status_code = 200 }
    else
        return { status_code = status_code, body = json.encode({ error = error_msg }) }
    end
end

module:log("info", "Adding http handler for /invite-jigasi on %s", module.host);
module:depends("http");
module:provides("http", {
    default_path = "/";
    route = {
        ["POST invite-jigasi"] = function(event)
            return async_handler_wrapper(event, handle_jigasi_invite)
        end;
    };
});

process_host_module(muc_domain, function(_, host)
    local muc_module = prosody.hosts[host].modules.muc;
    if muc_module then
        main_muc_service = muc_module;
        module:log('info', 'Found main_muc_service: %s', main_muc_service);
    else
        module:log('info', 'Will wait for muc to be available');
        prosody.hosts[host].events.add_handler('module-loaded', function(event)
            if (event.module == 'muc') then
                main_muc_service = prosody.hosts[host].modules.muc;
                module:log('info', 'Found(on loaded) main_muc_service: %s', main_muc_service);
            end
        end);
    end
end);