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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. local function verify_user(session, stanza)
  35. log("debug", "Session token: %s, session room: %s",
  36. tostring(session.auth_token),
  37. tostring(session.jitsi_meet_room));
  38. -- token not required for admin users
  39. local user_jid = stanza.attr.from;
  40. if is_admin(user_jid) then
  41. log("debug", "Token not required from admin user: %s", user_jid);
  42. return nil;
  43. end
  44. log("debug",
  45. "Will verify token for user: %s, room: %s ", user_jid, stanza.attr.to);
  46. if not token_util:verify_room(session, stanza.attr.to) then
  47. log("error", "Token %s not allowed to join: %s",
  48. tostring(session.auth_token), tostring(stanza.attr.to));
  49. session.send(
  50. st.error_reply(
  51. stanza, "cancel", "not-allowed", "Room and token mismatched"));
  52. return false; -- we need to just return non nil
  53. end
  54. log("debug",
  55. "allowed: %s to enter/create room: %s", user_jid, stanza.attr.to);
  56. end
  57. module:hook("muc-room-pre-create", function(event)
  58. local origin, stanza = event.origin, event.stanza;
  59. log("debug", "pre create: %s %s", tostring(origin), tostring(stanza));
  60. return verify_user(origin, stanza);
  61. end);
  62. module:hook("muc-occupant-pre-join", function(event)
  63. local origin, room, stanza = event.origin, event.room, event.stanza;
  64. log("debug", "pre join: %s %s", tostring(room), tostring(stanza));
  65. return verify_user(origin, stanza);
  66. end);
  67. for event_name, method in pairs {
  68. -- Normal room interactions
  69. ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
  70. -- Host room
  71. ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
  72. } do
  73. module:hook(event_name, function (event)
  74. local session, stanza = event.origin, event.stanza;
  75. -- if we do not require token we pass it through(default behaviour)
  76. -- or the request is coming from admin (focus)
  77. if not require_token_for_moderation or is_admin(stanza.attr.from) then
  78. return;
  79. end
  80. -- jitsi_meet_room is set after the token had been verified
  81. if not session.auth_token or not session.jitsi_meet_room then
  82. session.send(
  83. st.error_reply(
  84. stanza, "cancel", "not-allowed", "Room modification disabled for guests"));
  85. return true;
  86. end
  87. end, -1); -- the default prosody hook is on -2
  88. end
  89. module:hook_global('config-reloaded', load_config);