2015-11-02 21:02:50 +00:00
|
|
|
-- Token authentication
|
2022-09-13 12:55:00 +00:00
|
|
|
-- Copyright (C) 2021-present 8x8, Inc.
|
2015-11-02 21:02:50 +00:00
|
|
|
|
2017-05-03 21:48:49 +00:00
|
|
|
local basexx = require "basexx";
|
|
|
|
local have_async, async = pcall(require, "util.async");
|
|
|
|
local hex = require "util.hex";
|
2022-05-09 14:15:12 +00:00
|
|
|
local jwt = module:require "luajwtjitsi";
|
2017-05-04 21:12:45 +00:00
|
|
|
local jid = require "util.jid";
|
2020-05-10 23:53:14 +00:00
|
|
|
local json_safe = require "cjson.safe";
|
2017-05-03 21:48:49 +00:00
|
|
|
local path = require "util.paths";
|
|
|
|
local sha256 = require "util.hashes".sha256;
|
2020-11-05 20:30:06 +00:00
|
|
|
local main_util = module:require "util";
|
2022-07-12 06:51:13 +00:00
|
|
|
local ends_with = main_util.ends_with;
|
2020-11-05 20:30:06 +00:00
|
|
|
local http_get_with_retry = main_util.http_get_with_retry;
|
|
|
|
local extract_subdomain = main_util.extract_subdomain;
|
2015-11-02 21:02:50 +00:00
|
|
|
|
2020-08-14 12:06:14 +00:00
|
|
|
local nr_retries = 3;
|
2015-11-02 21:02:50 +00:00
|
|
|
|
2017-05-03 21:48:49 +00:00
|
|
|
-- TODO: Figure out a less arbitrary default cache size.
|
|
|
|
local cacheSize = module:get_option_number("jwt_pubkey_cache_size", 128);
|
2015-11-02 21:02:50 +00:00
|
|
|
|
2017-05-03 21:48:49 +00:00
|
|
|
local Util = {}
|
|
|
|
Util.__index = Util
|
2015-11-02 21:02:50 +00:00
|
|
|
|
2017-05-03 21:48:49 +00:00
|
|
|
--- Constructs util class for token verifications.
|
|
|
|
-- Constructor that uses the passed module to extract all the
|
|
|
|
-- needed configurations.
|
2022-07-14 07:10:08 +00:00
|
|
|
-- If configuration is missing returns nil
|
2017-05-03 21:48:49 +00:00
|
|
|
-- @param module the module in which options to check for configs.
|
|
|
|
-- @return the new instance or nil
|
|
|
|
function Util.new(module)
|
|
|
|
local self = setmetatable({}, Util)
|
2016-07-18 18:27:14 +00:00
|
|
|
|
2017-05-03 21:48:49 +00:00
|
|
|
self.appId = module:get_option_string("app_id");
|
|
|
|
self.appSecret = module:get_option_string("app_secret");
|
|
|
|
self.asapKeyServer = module:get_option_string("asap_key_server");
|
2022-04-19 11:06:20 +00:00
|
|
|
self.signatureAlgorithm = module:get_option_string("signature_algorithm");
|
2017-05-03 21:48:49 +00:00
|
|
|
self.allowEmptyToken = module:get_option_boolean("allow_empty_token");
|
2015-11-02 21:02:50 +00:00
|
|
|
|
2020-08-07 16:51:44 +00:00
|
|
|
self.cache = require"util.cache".new(cacheSize);
|
|
|
|
|
2017-05-04 21:12:45 +00:00
|
|
|
--[[
|
|
|
|
Multidomain can be supported in some deployments. In these deployments
|
|
|
|
there is a virtual conference muc, which address contains the subdomain
|
|
|
|
to use. Those deployments are accessible
|
|
|
|
by URL https://domain/subdomain.
|
|
|
|
Then the address of the room will be:
|
|
|
|
roomName@conference.subdomain.domain. This is like a virtual address
|
|
|
|
where there is only one muc configured by default with address:
|
|
|
|
conference.domain and the actual presentation of the room in that muc
|
|
|
|
component is [subdomain]roomName@conference.domain.
|
|
|
|
These setups relay on configuration 'muc_domain_base' which holds
|
2022-08-30 14:21:58 +00:00
|
|
|
the main domain and we use it to subtract subdomains from the
|
2017-05-04 21:12:45 +00:00
|
|
|
virtual addresses.
|
|
|
|
The following confgurations are for multidomain setups and domain name
|
|
|
|
verification:
|
|
|
|
--]]
|
|
|
|
|
|
|
|
-- optional parameter for custom muc component prefix,
|
|
|
|
-- defaults to "conference"
|
|
|
|
self.muc_domain_prefix = module:get_option_string(
|
|
|
|
"muc_mapper_domain_prefix", "conference");
|
|
|
|
-- domain base, which is the main domain used in the deployment,
|
|
|
|
-- the main VirtualHost for the deployment
|
|
|
|
self.muc_domain_base = module:get_option_string("muc_mapper_domain_base");
|
|
|
|
-- The "real" MUC domain that we are proxying to
|
|
|
|
if self.muc_domain_base then
|
|
|
|
self.muc_domain = module:get_option_string(
|
|
|
|
"muc_mapper_domain",
|
|
|
|
self.muc_domain_prefix.."."..self.muc_domain_base);
|
|
|
|
end
|
2022-07-12 06:51:13 +00:00
|
|
|
-- whether domain name verification is enabled, by default it is enabled
|
|
|
|
-- when disabled checking domain name and tenant if available will be skipped, we will check only room name.
|
|
|
|
self.enableDomainVerification = module:get_option_boolean('enable_domain_verification', true);
|
2017-05-04 21:12:45 +00:00
|
|
|
|
2017-05-03 21:48:49 +00:00
|
|
|
if self.allowEmptyToken == true then
|
|
|
|
module:log("warn", "WARNING - empty tokens allowed");
|
|
|
|
end
|
2015-12-22 18:51:43 +00:00
|
|
|
|
2017-05-03 21:48:49 +00:00
|
|
|
if self.appId == nil then
|
|
|
|
module:log("error", "'app_id' must not be empty");
|
|
|
|
return nil;
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.appSecret == nil and self.asapKeyServer == nil then
|
|
|
|
module:log("error", "'app_secret' or 'asap_key_server' must be specified");
|
|
|
|
return nil;
|
|
|
|
end
|
|
|
|
|
2022-04-19 11:06:20 +00:00
|
|
|
-- Set defaults for signature algorithm
|
|
|
|
if self.signatureAlgorithm == nil then
|
|
|
|
if self.asapKeyServer ~= nil then
|
|
|
|
self.signatureAlgorithm = "RS256"
|
|
|
|
elseif self.appSecret ~= nil then
|
|
|
|
self.signatureAlgorithm = "HS256"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-13 18:30:17 +00:00
|
|
|
--array of accepted issuers: by default only includes our appId
|
|
|
|
self.acceptedIssuers = module:get_option_array('asap_accepted_issuers',{self.appId})
|
|
|
|
|
|
|
|
--array of accepted audiences: by default only includes our appId
|
|
|
|
self.acceptedAudiences = module:get_option_array('asap_accepted_audiences',{'*'})
|
|
|
|
|
2020-08-12 19:43:34 +00:00
|
|
|
self.requireRoomClaim = module:get_option_boolean('asap_require_room_claim', true);
|
|
|
|
|
2017-05-03 21:48:49 +00:00
|
|
|
if self.asapKeyServer and not have_async then
|
|
|
|
module:log("error", "requires a version of Prosody with util.async");
|
|
|
|
return nil;
|
|
|
|
end
|
|
|
|
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2019-06-06 20:22:38 +00:00
|
|
|
function Util:set_asap_key_server(asapKeyServer)
|
2020-08-10 20:21:31 +00:00
|
|
|
self.asapKeyServer = asapKeyServer;
|
|
|
|
end
|
|
|
|
|
|
|
|
function Util:set_asap_accepted_issuers(acceptedIssuers)
|
|
|
|
self.acceptedIssuers = acceptedIssuers;
|
|
|
|
end
|
|
|
|
|
|
|
|
function Util:set_asap_accepted_audiences(acceptedAudiences)
|
|
|
|
self.acceptedAudiences = acceptedAudiences;
|
2019-06-06 20:22:38 +00:00
|
|
|
end
|
|
|
|
|
2020-08-12 19:43:34 +00:00
|
|
|
function Util:set_asap_require_room_claim(checkRoom)
|
|
|
|
self.requireRoomClaim = checkRoom;
|
|
|
|
end
|
|
|
|
|
2020-08-14 20:23:54 +00:00
|
|
|
function Util:clear_asap_cache()
|
|
|
|
self.cache = require"util.cache".new(cacheSize);
|
|
|
|
end
|
|
|
|
|
2017-05-03 21:48:49 +00:00
|
|
|
--- Returns the public key by keyID
|
|
|
|
-- @param keyId the key ID to request
|
|
|
|
-- @return the public key (the content of requested resource) or nil
|
2019-06-06 20:22:38 +00:00
|
|
|
function Util:get_public_key(keyId)
|
2020-08-07 16:51:44 +00:00
|
|
|
local content = self.cache:get(keyId);
|
2017-05-03 21:48:49 +00:00
|
|
|
if content == nil then
|
|
|
|
-- If the key is not found in the cache.
|
2021-09-27 19:48:13 +00:00
|
|
|
module:log("debug", "Cache miss for key: %s", keyId);
|
2020-08-14 20:23:54 +00:00
|
|
|
local keyurl = path.join(self.asapKeyServer, hex.to(sha256(keyId))..'.pem');
|
2021-09-27 19:48:13 +00:00
|
|
|
module:log("debug", "Fetching public key from: %s", keyurl);
|
2020-08-14 12:06:14 +00:00
|
|
|
content = http_get_with_retry(keyurl, nr_retries);
|
|
|
|
if content ~= nil then
|
|
|
|
self.cache:set(keyId, content);
|
2017-05-03 21:48:49 +00:00
|
|
|
end
|
2020-08-14 12:06:14 +00:00
|
|
|
return content;
|
2017-05-03 21:48:49 +00:00
|
|
|
else
|
|
|
|
-- If the key is in the cache, use it.
|
2021-09-27 19:48:13 +00:00
|
|
|
module:log("debug", "Cache hit for key: %s", keyId);
|
2017-05-03 21:48:49 +00:00
|
|
|
return content;
|
|
|
|
end
|
2015-11-02 21:02:50 +00:00
|
|
|
end
|
|
|
|
|
2017-05-03 21:48:49 +00:00
|
|
|
--- Verifies token and process needed values to be stored in the session.
|
2017-05-04 21:55:07 +00:00
|
|
|
-- Token is obtained from session.auth_token.
|
2017-05-03 21:48:49 +00:00
|
|
|
-- Stores in session the following values:
|
|
|
|
-- session.jitsi_meet_room - the room name value from the token
|
2017-05-04 21:12:45 +00:00
|
|
|
-- session.jitsi_meet_domain - the domain name value from the token
|
2017-07-13 18:30:17 +00:00
|
|
|
-- session.jitsi_meet_context_user - the user details from the token
|
|
|
|
-- session.jitsi_meet_context_group - the group value from the token
|
2018-06-15 18:10:22 +00:00
|
|
|
-- session.jitsi_meet_context_features - the features value from the token
|
2017-05-03 21:48:49 +00:00
|
|
|
-- @param session the current session
|
2020-06-15 16:55:21 +00:00
|
|
|
-- @param acceptedIssuers optional list of accepted issuers to check
|
2017-05-03 21:48:49 +00:00
|
|
|
-- @return false and error
|
2020-06-15 16:55:21 +00:00
|
|
|
function Util:process_and_verify_token(session, acceptedIssuers)
|
|
|
|
if not acceptedIssuers then
|
|
|
|
acceptedIssuers = self.acceptedIssuers;
|
|
|
|
end
|
|
|
|
|
2017-05-04 21:55:07 +00:00
|
|
|
if session.auth_token == nil then
|
2017-05-03 21:48:49 +00:00
|
|
|
if self.allowEmptyToken then
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false, "not-allowed", "token required";
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-19 11:06:20 +00:00
|
|
|
local key;
|
2020-08-11 14:29:34 +00:00
|
|
|
if session.public_key then
|
2022-04-19 11:06:20 +00:00
|
|
|
-- We're using an public key stored in the session
|
2020-08-11 14:29:34 +00:00
|
|
|
module:log("debug","Public key was found on the session");
|
2022-04-19 11:06:20 +00:00
|
|
|
key = session.public_key;
|
2020-08-11 14:29:34 +00:00
|
|
|
elseif self.asapKeyServer and session.auth_token ~= nil then
|
2022-04-19 11:06:20 +00:00
|
|
|
-- We're fetching an public key from an ASAP server
|
2017-05-03 21:48:49 +00:00
|
|
|
local dotFirst = session.auth_token:find("%.");
|
2022-02-17 18:05:39 +00:00
|
|
|
if not dotFirst then return false, "not-allowed", "Invalid token" end
|
2020-05-10 23:53:14 +00:00
|
|
|
local header, err = json_safe.decode(basexx.from_url64(session.auth_token:sub(1,dotFirst-1)));
|
|
|
|
if err then
|
|
|
|
return false, "not-allowed", "bad token format";
|
|
|
|
end
|
2017-05-03 21:48:49 +00:00
|
|
|
local kid = header["kid"];
|
|
|
|
if kid == nil then
|
|
|
|
return false, "not-allowed", "'kid' claim is missing";
|
|
|
|
end
|
2021-06-02 16:12:22 +00:00
|
|
|
local alg = header["alg"];
|
|
|
|
if alg == nil then
|
|
|
|
return false, "not-allowed", "'alg' claim is missing";
|
|
|
|
end
|
2022-04-19 11:06:20 +00:00
|
|
|
if alg.sub(alg,1,2) ~= "RS" then
|
2021-06-02 16:12:22 +00:00
|
|
|
return false, "not-allowed", "'kid' claim only support with RS family";
|
|
|
|
end
|
2022-04-19 11:06:20 +00:00
|
|
|
key = self:get_public_key(kid);
|
|
|
|
if key == nil then
|
2017-05-03 21:48:49 +00:00
|
|
|
return false, "not-allowed", "could not obtain public key";
|
|
|
|
end
|
2022-04-19 11:06:20 +00:00
|
|
|
elseif self.appSecret ~= nil then
|
|
|
|
-- We're using a symmetric secret
|
|
|
|
key = self.appSecret
|
2017-05-03 21:48:49 +00:00
|
|
|
end
|
|
|
|
|
2022-04-19 11:06:20 +00:00
|
|
|
if key == nil then
|
|
|
|
return false, "not-allowed", "signature verification key is missing";
|
2017-05-03 21:48:49 +00:00
|
|
|
end
|
2022-04-19 11:06:20 +00:00
|
|
|
|
|
|
|
-- now verify the whole token
|
|
|
|
local claims, msg = jwt.verify(
|
|
|
|
session.auth_token,
|
|
|
|
self.signatureAlgorithm,
|
|
|
|
key,
|
|
|
|
acceptedIssuers,
|
|
|
|
self.acceptedAudiences
|
|
|
|
)
|
2017-05-03 21:48:49 +00:00
|
|
|
if claims ~= nil then
|
2022-04-19 11:06:20 +00:00
|
|
|
if self.requireRoomClaim then
|
|
|
|
local roomClaim = claims["room"];
|
|
|
|
if roomClaim == nil then
|
|
|
|
return false, "'room' claim is missing";
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-03 21:48:49 +00:00
|
|
|
-- Binds room name to the session which is later checked on MUC join
|
|
|
|
session.jitsi_meet_room = claims["room"];
|
2017-05-04 21:12:45 +00:00
|
|
|
-- Binds domain name to the session
|
2017-07-12 03:06:47 +00:00
|
|
|
session.jitsi_meet_domain = claims["sub"];
|
|
|
|
|
|
|
|
-- Binds the user details to the session if available
|
|
|
|
if claims["context"] ~= nil then
|
|
|
|
if claims["context"]["user"] ~= nil then
|
|
|
|
session.jitsi_meet_context_user = claims["context"]["user"];
|
|
|
|
end
|
|
|
|
|
|
|
|
if claims["context"]["group"] ~= nil then
|
|
|
|
-- Binds any group details to the session
|
|
|
|
session.jitsi_meet_context_group = claims["context"]["group"];
|
|
|
|
end
|
2018-06-15 18:10:22 +00:00
|
|
|
|
|
|
|
if claims["context"]["features"] ~= nil then
|
|
|
|
-- Binds any features details to the session
|
|
|
|
session.jitsi_meet_context_features = claims["context"]["features"];
|
|
|
|
end
|
2021-05-27 15:58:06 +00:00
|
|
|
if claims["context"]["room"] ~= nil then
|
|
|
|
session.jitsi_meet_context_room = claims["context"]["room"]
|
|
|
|
end
|
2017-07-12 03:06:47 +00:00
|
|
|
end
|
2017-05-03 21:48:49 +00:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false, "not-allowed", msg;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-14 07:10:08 +00:00
|
|
|
--- Verifies room name and domain if necessary.
|
2017-05-03 21:48:49 +00:00
|
|
|
-- Checks configs and if necessary checks the room name extracted from
|
2017-05-04 21:12:45 +00:00
|
|
|
-- room_address against the one saved in the session when token was verified.
|
|
|
|
-- Also verifies domain name from token against the domain in the room_address,
|
|
|
|
-- if enableDomainVerification is enabled.
|
2017-05-03 21:48:49 +00:00
|
|
|
-- @param session the current session
|
|
|
|
-- @param room_address the whole room address as received
|
|
|
|
-- @return returns true in case room was verified or there is no need to verify
|
|
|
|
-- it and returns false in case verification was processed
|
|
|
|
-- and was not successful
|
|
|
|
function Util:verify_room(session, room_address)
|
|
|
|
if self.allowEmptyToken and session.auth_token == nil then
|
|
|
|
module:log(
|
|
|
|
"debug",
|
|
|
|
"Skipped room token verification - empty tokens are allowed");
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
2017-05-04 21:12:45 +00:00
|
|
|
-- extract room name using all chars, except the not allowed ones
|
|
|
|
local room,_,_ = jid.split(room_address);
|
2017-05-03 21:48:49 +00:00
|
|
|
if room == nil then
|
|
|
|
log("error",
|
|
|
|
"Unable to get name of the MUC room ? to: %s", room_address);
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
2021-06-02 15:30:09 +00:00
|
|
|
local auth_room = session.jitsi_meet_room;
|
|
|
|
if auth_room then
|
|
|
|
auth_room = string.lower(auth_room);
|
|
|
|
end
|
2017-05-04 21:12:45 +00:00
|
|
|
if not self.enableDomainVerification then
|
2017-05-15 22:16:23 +00:00
|
|
|
-- if auth_room is missing, this means user is anonymous (no token for
|
|
|
|
-- its domain) we let it through, jicofo is verifying creation domain
|
2022-07-12 06:51:13 +00:00
|
|
|
if auth_room and (room ~= auth_room and not ends_with(room, ']'..auth_room)) and auth_room ~= '*' then
|
2017-05-04 21:12:45 +00:00
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
return true;
|
2017-05-03 21:48:49 +00:00
|
|
|
end
|
|
|
|
|
2017-05-04 21:12:45 +00:00
|
|
|
local room_address_to_verify = jid.bare(room_address);
|
2017-06-20 18:48:47 +00:00
|
|
|
local room_node = jid.node(room_address);
|
2017-05-04 21:12:45 +00:00
|
|
|
-- parses bare room address, for multidomain expected format is:
|
|
|
|
-- [subdomain]roomName@conference.domain
|
2020-11-05 20:30:06 +00:00
|
|
|
local target_subdomain, target_room = extract_subdomain(room_node);
|
2017-06-20 18:48:47 +00:00
|
|
|
|
|
|
|
-- if we have '*' as room name in token, this means all rooms are allowed
|
|
|
|
-- so we will use the actual name of the room when constructing strings
|
|
|
|
-- to verify subdomains and domains to simplify checks
|
|
|
|
local room_to_check;
|
|
|
|
if auth_room == '*' then
|
|
|
|
-- authorized for accessing any room assign to room_to_check the actual
|
|
|
|
-- room name
|
|
|
|
if target_room ~= nil then
|
|
|
|
-- we are in multidomain mode and we were able to extract room name
|
|
|
|
room_to_check = target_room;
|
|
|
|
else
|
|
|
|
-- no target_room, room_address_to_verify does not contain subdomain
|
|
|
|
-- so we get just the node which is the room name
|
|
|
|
room_to_check = room_node;
|
|
|
|
end
|
|
|
|
else
|
2021-05-27 15:58:06 +00:00
|
|
|
-- no wildcard, so check room against authorized room from the token
|
|
|
|
if session.jitsi_meet_context_room and (session.jitsi_meet_context_room["regex"] == true or session.jitsi_meet_context_room["regex"] == "true") then
|
|
|
|
if target_room ~= nil then
|
|
|
|
-- room with subdomain
|
|
|
|
room_to_check = target_room:match(auth_room);
|
|
|
|
else
|
|
|
|
room_to_check = room_node:match(auth_room);
|
|
|
|
end
|
2021-04-19 12:41:39 +00:00
|
|
|
else
|
2021-05-27 15:58:06 +00:00
|
|
|
-- not a regex
|
|
|
|
room_to_check = auth_room;
|
2021-04-19 12:41:39 +00:00
|
|
|
end
|
|
|
|
module:log("debug", "room to check: %s", room_to_check)
|
|
|
|
if not room_to_check then
|
|
|
|
return false
|
|
|
|
end
|
2017-06-20 18:48:47 +00:00
|
|
|
end
|
2017-05-04 21:12:45 +00:00
|
|
|
|
2021-05-19 20:45:58 +00:00
|
|
|
local auth_domain = string.lower(session.jitsi_meet_domain);
|
2019-01-28 22:19:43 +00:00
|
|
|
local subdomain_to_check;
|
2017-05-04 21:12:45 +00:00
|
|
|
if target_subdomain then
|
2019-01-28 22:19:43 +00:00
|
|
|
if auth_domain == '*' then
|
|
|
|
-- check for wildcard in JWT claim, allow access if found
|
|
|
|
subdomain_to_check = target_subdomain;
|
|
|
|
else
|
|
|
|
-- no wildcard in JWT claim, so check subdomain against sub in token
|
|
|
|
subdomain_to_check = auth_domain;
|
|
|
|
end
|
2017-05-04 21:12:45 +00:00
|
|
|
-- from this point we depend on muc_domain_base,
|
|
|
|
-- deny access if option is missing
|
|
|
|
if not self.muc_domain_base then
|
|
|
|
module:log("warn", "No 'muc_domain_base' option set, denying access!");
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
return room_address_to_verify == jid.join(
|
2021-05-19 21:02:59 +00:00
|
|
|
"["..subdomain_to_check.."]"..room_to_check, self.muc_domain);
|
2017-05-04 21:12:45 +00:00
|
|
|
else
|
2019-01-28 22:19:43 +00:00
|
|
|
if auth_domain == '*' then
|
|
|
|
-- check for wildcard in JWT claim, allow access if found
|
|
|
|
subdomain_to_check = self.muc_domain;
|
|
|
|
else
|
|
|
|
-- no wildcard in JWT claim, so check subdomain against sub in token
|
|
|
|
subdomain_to_check = self.muc_domain_prefix.."."..auth_domain;
|
|
|
|
end
|
2017-05-04 21:12:45 +00:00
|
|
|
-- we do not have a domain part (multidomain is not enabled)
|
|
|
|
-- verify with info from the token
|
2021-05-19 20:45:58 +00:00
|
|
|
return room_address_to_verify == jid.join(room_to_check, subdomain_to_check);
|
2017-05-04 21:12:45 +00:00
|
|
|
end
|
2015-11-02 21:02:50 +00:00
|
|
|
end
|
|
|
|
|
2020-06-15 16:55:21 +00:00
|
|
|
return Util;
|