2015-11-02 21:02:50 +00:00
|
|
|
-- Token authentication
|
2021-05-13 03:02:51 +00:00
|
|
|
-- Copyright (C) 2021-present 8x8, Inc.
|
2015-11-02 21:02:50 +00:00
|
|
|
|
2016-08-23 19:42:49 +00:00
|
|
|
local formdecode = require "util.http".formdecode;
|
2015-12-22 18:51:43 +00:00
|
|
|
local generate_uuid = require "util.uuid".generate;
|
2015-11-02 21:02:50 +00:00
|
|
|
local new_sasl = require "util.sasl".new;
|
2015-12-22 18:51:43 +00:00
|
|
|
local sasl = require "util.sasl";
|
2017-05-03 21:48:49 +00:00
|
|
|
local token_util = module:require "token/util".new(module);
|
2020-03-09 18:50:12 +00:00
|
|
|
local sessions = prosody.full_sessions;
|
2017-05-03 21:48:49 +00:00
|
|
|
|
|
|
|
-- no token configuration
|
|
|
|
if token_util == nil then
|
|
|
|
return;
|
|
|
|
end
|
2015-11-02 21:02:50 +00:00
|
|
|
|
2021-05-13 03:02:51 +00:00
|
|
|
module:depends("jitsi_session");
|
|
|
|
|
2015-11-02 21:02:50 +00:00
|
|
|
-- define auth provider
|
|
|
|
local provider = {};
|
|
|
|
|
2015-12-22 18:51:43 +00:00
|
|
|
local host = module.host;
|
2015-11-02 21:02:50 +00:00
|
|
|
|
2020-01-24 14:59:29 +00:00
|
|
|
-- Extract 'token' param from URL when session is created
|
|
|
|
function init_session(event)
|
2015-12-22 18:51:43 +00:00
|
|
|
local session, request = event.session, event.request;
|
|
|
|
local query = request.url.query;
|
2017-05-03 21:48:49 +00:00
|
|
|
|
2015-12-22 18:51:43 +00:00
|
|
|
if query ~= nil then
|
2017-07-15 03:08:41 +00:00
|
|
|
local params = formdecode(query);
|
2020-06-30 13:15:08 +00:00
|
|
|
|
|
|
|
-- The following fields are filled in the session, by extracting them
|
2022-07-14 07:10:08 +00:00
|
|
|
-- from the query and no validation is being done.
|
2020-06-30 13:15:08 +00:00
|
|
|
-- After validating auth_token will be cleaned in case of error and few
|
|
|
|
-- other fields will be extracted from the token and set in the session
|
|
|
|
|
2017-07-15 03:08:41 +00:00
|
|
|
session.auth_token = query and params.token or nil;
|
|
|
|
end
|
2020-01-24 14:59:29 +00:00
|
|
|
end
|
|
|
|
|
2020-04-08 18:06:49 +00:00
|
|
|
module:hook_global("bosh-session", init_session);
|
|
|
|
module:hook_global("websocket-session", init_session);
|
2015-12-22 18:51:43 +00:00
|
|
|
|
|
|
|
function provider.test_password(username, password)
|
|
|
|
return nil, "Password based auth not supported";
|
2015-11-02 21:02:50 +00:00
|
|
|
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
|
|
|
|
|
2015-12-22 18:51:43 +00:00
|
|
|
function provider.get_sasl_handler(session)
|
|
|
|
|
|
|
|
local function get_username_from_token(self, message)
|
2017-08-08 13:48:18 +00:00
|
|
|
|
2020-08-11 14:29:34 +00:00
|
|
|
-- retrieve custom public key from server and save it on the session
|
2020-08-14 12:06:14 +00:00
|
|
|
local pre_event_result = prosody.events.fire_event("pre-jitsi-authentication-fetch-key", session);
|
|
|
|
if pre_event_result ~= nil and pre_event_result.res == false then
|
|
|
|
log("warn",
|
|
|
|
"Error verifying token on pre authentication stage:%s, reason:%s", pre_event_result.error, pre_event_result.reason);
|
|
|
|
session.auth_token = nil;
|
|
|
|
return pre_event_result.res, pre_event_result.error, pre_event_result.reason;
|
2017-08-08 13:48:18 +00:00
|
|
|
end
|
2017-07-15 03:12:56 +00:00
|
|
|
|
2020-08-11 14:29:34 +00:00
|
|
|
local res, error, reason = token_util:process_and_verify_token(session);
|
2020-08-14 12:06:14 +00:00
|
|
|
if res == false then
|
|
|
|
log("warn",
|
|
|
|
"Error verifying token err:%s, reason:%s", error, reason);
|
|
|
|
session.auth_token = nil;
|
|
|
|
return res, error, reason;
|
|
|
|
end
|
2020-08-11 14:29:34 +00:00
|
|
|
|
2021-10-04 10:19:41 +00:00
|
|
|
local shouldAllow = prosody.events.fire_event("jitsi-access-ban-check", session);
|
|
|
|
if shouldAllow == false then
|
|
|
|
log("warn", "user is banned")
|
|
|
|
return false, "not-allowed", "user is banned";
|
|
|
|
end
|
|
|
|
|
2017-07-15 03:12:56 +00:00
|
|
|
local customUsername
|
|
|
|
= prosody.events.fire_event("pre-jitsi-authentication", session);
|
|
|
|
|
|
|
|
if (customUsername) then
|
|
|
|
self.username = customUsername;
|
2020-03-09 18:50:12 +00:00
|
|
|
elseif (session.previd ~= nil) then
|
|
|
|
for _, session1 in pairs(sessions) do
|
|
|
|
if (session1.resumption_token == session.previd) then
|
|
|
|
self.username = session1.username;
|
|
|
|
break;
|
|
|
|
end
|
2023-01-24 13:58:43 +00:00
|
|
|
end
|
2017-07-15 03:12:56 +00:00
|
|
|
else
|
|
|
|
self.username = message;
|
|
|
|
end
|
|
|
|
|
2020-08-14 12:06:14 +00:00
|
|
|
local post_event_result = prosody.events.fire_event("post-jitsi-authentication", session);
|
|
|
|
if post_event_result ~= nil and post_event_result.res == false then
|
|
|
|
log("warn",
|
|
|
|
"Error verifying token on post authentication stage :%s, reason:%s", post_event_result.error, post_event_result.reason);
|
|
|
|
session.auth_token = nil;
|
|
|
|
return post_event_result.res, post_event_result.error, post_event_result.reason;
|
2020-08-11 14:29:34 +00:00
|
|
|
end
|
|
|
|
|
2017-07-15 03:12:56 +00:00
|
|
|
return res;
|
2015-12-22 18:51:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return new_sasl(host, { anonymous = get_username_from_token });
|
2015-11-02 21:02:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module:provides("auth", provider);
|
2015-12-22 18:51:43 +00:00
|
|
|
|
|
|
|
local function anonymous(self, message)
|
|
|
|
|
|
|
|
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
|
2020-03-09 18:50:12 +00:00
|
|
|
if (self.username == nil) then
|
|
|
|
self.username = username;
|
|
|
|
end
|
2016-08-29 14:39:47 +00:00
|
|
|
return "success";
|
2015-12-22 18:51:43 +00:00
|
|
|
else
|
2016-08-29 14:39:47 +00:00
|
|
|
return "failure", err, msg;
|
2015-12-22 18:51:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sasl.registerMechanism("ANONYMOUS", {"anonymous"}, anonymous);
|