You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mod_token_verification.lua 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. -- Token authentication
  2. -- Copyright (C) 2015 Atlassian
  3. local log = module._log;
  4. local host = module.host;
  5. local st = require "util.stanza";
  6. local is_admin = require "core.usermanager".is_admin;
  7. local parentHostName = string.gmatch(tostring(host), "%w+.(%w.+)")();
  8. if parentHostName == nil then
  9. log("error", "Failed to start - unable to get parent hostname");
  10. return;
  11. end
  12. local parentCtx = module:context(parentHostName);
  13. if parentCtx == nil then
  14. log("error",
  15. "Failed to start - unable to get parent context for host: %s",
  16. tostring(parentHostName));
  17. return;
  18. end
  19. local token_util = module:require "token/util".new(parentCtx);
  20. -- no token configuration
  21. if token_util == nil then
  22. return;
  23. end
  24. log("debug",
  25. "%s - starting MUC token verifier app_id: %s app_secret: %s allow empty: %s",
  26. tostring(host), tostring(token_util.appId), tostring(token_util.appSecret),
  27. tostring(token_util.allowEmptyToken));
  28. -- option to disable room modification (sending muc config form) for guest that do not provide token
  29. local require_token_for_moderation;
  30. local function load_config()
  31. require_token_for_moderation = module:get_option_boolean("token_verification_require_token_for_moderation");
  32. end
  33. load_config();
  34. -- verify user and whether he is allowed to join a room based on the token information
  35. local function verify_user(session, stanza)
  36. log("debug", "Session token: %s, session room: %s",
  37. tostring(session.auth_token),
  38. tostring(session.jitsi_meet_room));
  39. -- token not required for admin users
  40. local user_jid = stanza.attr.from;
  41. if is_admin(user_jid) then
  42. log("debug", "Token not required from admin user: %s", user_jid);
  43. return true;
  44. end
  45. log("debug",
  46. "Will verify token for user: %s, room: %s ", user_jid, stanza.attr.to);
  47. if not token_util:verify_room(session, stanza.attr.to) then
  48. log("error", "Token %s not allowed to join: %s",
  49. tostring(session.auth_token), tostring(stanza.attr.to));
  50. session.send(
  51. st.error_reply(
  52. stanza, "cancel", "not-allowed", "Room and token mismatched"));
  53. return false; -- we need to just return non nil
  54. end
  55. log("debug",
  56. "allowed: %s to enter/create room: %s", user_jid, stanza.attr.to);
  57. return true;
  58. end
  59. module:hook("muc-room-pre-create", function(event)
  60. local origin, stanza = event.origin, event.stanza;
  61. log("debug", "pre create: %s %s", tostring(origin), tostring(stanza));
  62. if not verify_user(origin, stanza) then
  63. return true; -- Returning any value other than nil will halt processing of the event
  64. end
  65. end);
  66. module:hook("muc-occupant-pre-join", function(event)
  67. local origin, room, stanza = event.origin, event.room, event.stanza;
  68. log("debug", "pre join: %s %s", tostring(room), tostring(stanza));
  69. if not verify_user(origin, stanza) then
  70. return true; -- Returning any value other than nil will halt processing of the event
  71. end
  72. end);
  73. for event_name, method in pairs {
  74. -- Normal room interactions
  75. ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
  76. -- Host room
  77. ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
  78. } do
  79. module:hook(event_name, function (event)
  80. local session, stanza = event.origin, event.stanza;
  81. -- if we do not require token we pass it through(default behaviour)
  82. -- or the request is coming from admin (focus)
  83. if not require_token_for_moderation or is_admin(stanza.attr.from) then
  84. return;
  85. end
  86. -- jitsi_meet_room is set after the token had been verified
  87. if not session.auth_token or not session.jitsi_meet_room then
  88. session.send(
  89. st.error_reply(
  90. stanza, "cancel", "not-allowed", "Room modification disabled for guests"));
  91. return true;
  92. end
  93. end, -1); -- the default prosody hook is on -2
  94. end
  95. module:hook_global('config-reloaded', load_config);