2015-11-02 21:02:50 +00:00
|
|
|
-- Token authentication
|
|
|
|
-- Copyright (C) 2015 Atlassian
|
|
|
|
|
|
|
|
local log = module._log;
|
|
|
|
local host = module.host;
|
|
|
|
local st = require "util.stanza";
|
|
|
|
local is_admin = require "core.usermanager".is_admin;
|
|
|
|
|
|
|
|
|
|
|
|
local parentHostName = string.gmatch(tostring(host), "%w+.(%w.+)")();
|
|
|
|
if parentHostName == nil then
|
|
|
|
log("error", "Failed to start - unable to get parent hostname");
|
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
|
|
|
local parentCtx = module:context(parentHostName);
|
|
|
|
if parentCtx == nil then
|
2015-12-22 18:51:43 +00:00
|
|
|
log("error",
|
|
|
|
"Failed to start - unable to get parent context for host: %s",
|
|
|
|
tostring(parentHostName));
|
2015-11-02 21:02:50 +00:00
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
|
|
|
local appId = parentCtx:get_option_string("app_id");
|
|
|
|
local appSecret = parentCtx:get_option_string("app_secret");
|
2015-12-22 18:51:43 +00:00
|
|
|
local allowEmptyToken = parentCtx:get_option_boolean("allow_empty_token");
|
2016-06-13 21:11:44 +00:00
|
|
|
local disableRoomNameConstraints = parentCtx:get_option_boolean("disable_room_name_constraints")
|
2015-11-02 21:02:50 +00:00
|
|
|
|
2015-12-22 18:51:43 +00:00
|
|
|
log("debug",
|
|
|
|
"%s - starting MUC token verifier app_id: %s app_secret: %s allow empty: %s",
|
|
|
|
tostring(host), tostring(appId), tostring(appSecret),
|
|
|
|
tostring(allowEmptyToken));
|
2015-11-02 21:02:50 +00:00
|
|
|
|
2015-12-22 18:51:43 +00:00
|
|
|
local function verify_user(session, stanza)
|
|
|
|
log("debug", "Session token: %s, session room: %s",
|
|
|
|
tostring(session.auth_token),
|
|
|
|
tostring(session.jitsi_meet_room));
|
2015-11-18 18:49:36 +00:00
|
|
|
|
2015-11-02 21:02:50 +00:00
|
|
|
-- token not required for admin users
|
2015-12-22 18:51:43 +00:00
|
|
|
local user_jid = stanza.attr.from;
|
2015-11-02 21:02:50 +00:00
|
|
|
if is_admin(user_jid) then
|
|
|
|
log("debug", "Token not required from admin user: %s", user_jid);
|
|
|
|
return nil;
|
|
|
|
end
|
2015-11-18 18:49:36 +00:00
|
|
|
|
2016-06-13 21:11:44 +00:00
|
|
|
if allowEmptyToken and session.auth_token == nil then
|
|
|
|
module:log(
|
|
|
|
"debug",
|
|
|
|
"Skipped room token verification - empty tokens are allowed");
|
|
|
|
return nil;
|
|
|
|
end
|
|
|
|
|
2015-11-18 18:49:36 +00:00
|
|
|
local room = string.match(stanza.attr.to, "^(%w+)@");
|
|
|
|
log("debug", "Will verify token for user: %s, room: %s ", user_jid, room);
|
|
|
|
if room == nil then
|
2015-12-22 18:51:43 +00:00
|
|
|
log("error",
|
|
|
|
"Unable to get name of the MUC room ? to: %s", stanza.attr.to);
|
2015-11-18 18:49:36 +00:00
|
|
|
return nil;
|
|
|
|
end
|
|
|
|
|
2015-12-22 18:51:43 +00:00
|
|
|
local token = session.auth_token;
|
|
|
|
local auth_room = session.jitsi_meet_room;
|
2016-06-13 21:11:44 +00:00
|
|
|
if room ~= auth_room and disableRoomNameConstraints ~= true then
|
2015-12-22 18:51:43 +00:00
|
|
|
log("error", "Token %s not allowed to join: %s",
|
|
|
|
tostring(token), tostring(auth_room));
|
|
|
|
session.send(
|
|
|
|
st.error_reply(
|
|
|
|
stanza, "cancel", "not-allowed", "Room and token mismatched"));
|
2015-11-02 21:02:50 +00:00
|
|
|
return true;
|
|
|
|
end
|
2015-12-22 18:51:43 +00:00
|
|
|
log("debug", "allowed: %s to enter/create room: %s", user_jid, room);
|
2015-11-02 21:02:50 +00:00
|
|
|
end
|
|
|
|
|
2015-12-22 18:51:43 +00:00
|
|
|
module:hook("muc-room-pre-create", function(event)
|
|
|
|
local origin, stanza = event.origin, event.stanza;
|
|
|
|
log("debug", "pre create: %s %s", tostring(origin), tostring(stanza));
|
|
|
|
return verify_user(origin, stanza);
|
|
|
|
end);
|
|
|
|
|
|
|
|
module:hook("muc-occupant-pre-join", function(event)
|
|
|
|
local origin, room, stanza = event.origin, event.room, event.stanza;
|
|
|
|
log("debug", "pre join: %s %s", tostring(room), tostring(stanza));
|
|
|
|
return verify_user(origin, stanza);
|
|
|
|
end);
|