2015-11-02 21:02:50 +00:00
|
|
|
-- Token authentication
|
|
|
|
-- Copyright (C) 2015 Atlassian
|
|
|
|
|
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
|
|
|
|
|
|
|
-- 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);
|
|
|
|
session.auth_token = query and params.token or nil;
|
2020-03-09 18:50:12 +00:00
|
|
|
-- previd is used together with https://modules.prosody.im/mod_smacks.html
|
|
|
|
-- the param is used to find resumed session and re-use anonymous(random) user id
|
|
|
|
-- (see get_username_from_token)
|
|
|
|
session.previd = query and params.previd or nil;
|
2017-07-15 03:08:41 +00:00
|
|
|
|
2019-05-16 19:23:36 +00:00
|
|
|
-- The room name and optional prefix from the bosh query
|
2017-07-15 03:08:41 +00:00
|
|
|
session.jitsi_bosh_query_room = params.room;
|
2019-05-16 19:23:36 +00:00
|
|
|
session.jitsi_bosh_query_prefix = params.prefix or "";
|
2017-07-15 03:08:41 +00:00
|
|
|
end
|
2020-01-24 14:59:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module:hook("bosh-session", init_session);
|
|
|
|
module:hook("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
|
|
|
local res, error, reason = token_util:process_and_verify_token(session);
|
|
|
|
|
|
|
|
if (res == false) then
|
|
|
|
log("warn",
|
|
|
|
"Error verifying token err:%s, reason:%s", error, reason);
|
2017-10-26 19:01:21 +00:00
|
|
|
return res, error, reason;
|
2017-08-08 13:48:18 +00:00
|
|
|
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
|
|
|
|
end
|
2017-07-15 03:12:56 +00:00
|
|
|
else
|
|
|
|
self.username = message;
|
|
|
|
end
|
|
|
|
|
|
|
|
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);
|