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 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 appId = parentCtx:get_option_string("app_id");
  20. local appSecret = parentCtx:get_option_string("app_secret");
  21. local allowEmptyToken = parentCtx:get_option_boolean("allow_empty_token");
  22. log("debug",
  23. "%s - starting MUC token verifier app_id: %s app_secret: %s allow empty: %s",
  24. tostring(host), tostring(appId), tostring(appSecret),
  25. tostring(allowEmptyToken));
  26. local function verify_user(session, stanza)
  27. log("debug", "Session token: %s, session room: %s",
  28. tostring(session.auth_token),
  29. tostring(session.jitsi_meet_room));
  30. if allowEmptyToken and session.auth_token == nil then
  31. module:log(
  32. "debug",
  33. "Skipped room token verification - empty tokens are allowed");
  34. return nil;
  35. end
  36. -- token not required for admin users
  37. local user_jid = stanza.attr.from;
  38. if is_admin(user_jid) then
  39. log("debug", "Token not required from admin user: %s", user_jid);
  40. return nil;
  41. end
  42. local room = string.match(stanza.attr.to, "^(%w+)@");
  43. log("debug", "Will verify token for user: %s, room: %s ", user_jid, room);
  44. if room == nil then
  45. log("error",
  46. "Unable to get name of the MUC room ? to: %s", stanza.attr.to);
  47. return nil;
  48. end
  49. local token = session.auth_token;
  50. local auth_room = session.jitsi_meet_room;
  51. if room ~= auth_room then
  52. log("error", "Token %s not allowed to join: %s",
  53. tostring(token), tostring(auth_room));
  54. session.send(
  55. st.error_reply(
  56. stanza, "cancel", "not-allowed", "Room and token mismatched"));
  57. return true;
  58. end
  59. log("debug", "allowed: %s to enter/create room: %s", user_jid, room);
  60. end
  61. module:hook("muc-room-pre-create", function(event)
  62. local origin, stanza = event.origin, event.stanza;
  63. log("debug", "pre create: %s %s", tostring(origin), tostring(stanza));
  64. return verify_user(origin, stanza);
  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. return verify_user(origin, stanza);
  70. end);