您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

mod_auth_token.lua 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. -- Token authentication
  2. -- Copyright (C) 2015 Atlassian
  3. local generate_uuid = require "util.uuid".generate;
  4. local new_sasl = require "util.sasl".new;
  5. local sasl = require "util.sasl";
  6. local formdecode = require "util.http".formdecode;
  7. local token_util = module:require "token/util";
  8. -- define auth provider
  9. local provider = {};
  10. local host = module.host;
  11. local appId = module:get_option_string("app_id");
  12. local appSecret = module:get_option_string("app_secret");
  13. local allowEmptyToken = module:get_option_boolean("allow_empty_token");
  14. local disableRoomNameConstraints = module:get_option_boolean("disable_room_name_constraints");
  15. if allowEmptyToken == true then
  16. module:log("warn", "WARNING - empty tokens allowed");
  17. end
  18. if appId == nil then
  19. module:log("error", "'app_id' must not be empty");
  20. return;
  21. end
  22. if appSecret == nil then
  23. module:log("error", "'app_secret' must not be empty");
  24. return;
  25. end
  26. -- Extract 'token' param from BOSH URL when session is created
  27. module:hook("bosh-session", function(event)
  28. local session, request = event.session, event.request;
  29. local query = request.url.query;
  30. if query ~= nil then
  31. session.auth_token = query and formdecode(query).token or nil;
  32. end
  33. end)
  34. function provider.test_password(username, password)
  35. return nil, "Password based auth not supported";
  36. end
  37. function provider.get_password(username)
  38. return nil;
  39. end
  40. function provider.set_password(username, password)
  41. return nil, "Set password not supported";
  42. end
  43. function provider.user_exists(username)
  44. return nil;
  45. end
  46. function provider.create_user(username, password)
  47. return nil;
  48. end
  49. function provider.delete_user(username)
  50. return nil;
  51. end
  52. function provider.get_sasl_handler(session)
  53. -- JWT token extracted from BOSH URL
  54. local token = session.auth_token;
  55. local function get_username_from_token(self, message)
  56. if token == nil then
  57. if allowEmptyToken == true then
  58. return true;
  59. else
  60. return false, "not-allowed", "token required";
  61. end
  62. end
  63. -- here we check if 'room' claim exists
  64. local room, roomErr = token_util.get_room_name(token, appSecret);
  65. if room == nil and disableRoomNameConstraints ~= true then
  66. if roomErr == nil then
  67. roomErr = "'room' claim is missing";
  68. end
  69. return false, "not-allowed", roomErr;
  70. end
  71. -- now verify the whole token
  72. local result, msg
  73. = token_util.verify_token(token, appId, appSecret, room, disableRoomNameConstraints);
  74. if result == true then
  75. -- Binds room name to the session which is later checked on MUC join
  76. session.jitsi_meet_room = room;
  77. return true
  78. else
  79. return false, "not-allowed", msg
  80. end
  81. end
  82. return new_sasl(host, { anonymous = get_username_from_token });
  83. end
  84. module:provides("auth", provider);
  85. local function anonymous(self, message)
  86. local username = generate_uuid();
  87. -- This calls the handler created in 'provider.get_sasl_handler(session)'
  88. local result, err, msg = self.profile.anonymous(self, username, self.realm);
  89. self.username = username;
  90. if result == true then
  91. return "success"
  92. else
  93. return "failure", err, msg
  94. end
  95. end
  96. sasl.registerMechanism("ANONYMOUS", {"anonymous"}, anonymous);