Skip to content

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

2.8 KB
Raw Blame
-- Anonymous authentication with extras:
-- * session resumption
-- Copyright (C) 2021-present 8x8, Inc.

local generate_uuid = require "util.uuid".generate;
local new_sasl = require "util.sasl".new;
local sasl = require "util.sasl";
local sessions = prosody.full_sessions;

module:depends("jitsi_session");

-- define auth provider
local provider = {};

function provider.test_password(username, password)
    return nil, "Password based auth not supported";
end

function provider.get_password(username)
    return nil;
end

function provider.set_password(username, password)
    return nil, "Set password not supported";
end

function provider.user_exists(username)
    return nil;
end

function provider.create_user(username, password)
    return nil;
end

function provider.delete_user(username)
    return nil;
end

function provider.get_sasl_handler(session)
    -- Custom session matching so we can resume session even with randomly
    -- generated user IDs.
    local function get_username(self, message)

        local resuming = false;
        if (session.previd ~= nil) then
            for _, session1 in pairs(sessions) do
                if (session1.resumption_token == session.previd) then
                    self.username = session1.username;
                    resuming = true;
                    break;
                end
            end
        else
            self.username = message;
        end

        if not resuming then
            session.auth_token = nil;
        end

        return true;
    end

    return new_sasl(module.host, { anonymous = get_username });
end

module:provides("auth", provider);

local function anonymous(self, message)
    -- Same as the vanilla anonymous auth plugin
    local username = generate_uuid();

    -- This calls the handler created in 'provider.get_sasl_handler(session)'
    local result, err, msg = self.profile.anonymous(self, username, self.realm);

    if result == true then
        if (self.username == nil) then
            -- Session was not resumed
            self.username = username;
        end
        return "success";
    else
        return "failure", err, msg;
    end
end

sasl.registerMechanism("ANONYMOUS", {"anonymous"}, anonymous);

-- The token kept above for a matching previd is meant for mod_auth_token, which verifies it
-- when the stream is resumed (c2s-session-updated). A resumed stream never binds a resource,
-- so a session reaching bind is a new one and its token was never verified: drop it.
module:hook("resource-bind", function (event)
    event.session.auth_token = nil;
end, 100);

module:hook("pre-resource-unbind", function (e)
    local error, session = e.error, e.session;

    prosody.events.fire_event('jitsi-pre-session-unbind', {
        jid = session.full_jid,
        session = session,
        error = error
    });
end, 11);