Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

mod_auth_token.lua 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. -- Token authentication
  2. -- Copyright (C) 2015 Atlassian
  3. local basexx = require "basexx";
  4. local have_async, async = pcall(require, "util.async");
  5. local formdecode = require "util.http".formdecode;
  6. local generate_uuid = require "util.uuid".generate;
  7. local http = require "net.http";
  8. local json = require "cjson";
  9. local new_sasl = require "util.sasl".new;
  10. local sasl = require "util.sasl";
  11. local timer = require "util.timer";
  12. local token_util = module:require "token/util";
  13. -- define auth provider
  14. local provider = {};
  15. local host = module.host;
  16. local appId = module:get_option_string("app_id");
  17. local appSecret = module:get_option_string("app_secret");
  18. local asapKeyServer = module:get_option_string("asap_key_server");
  19. local allowEmptyToken = module:get_option_boolean("allow_empty_token");
  20. local disableRoomNameConstraints = module:get_option_boolean("disable_room_name_constraints");
  21. -- TODO: Figure out a less arbitrary default cache size.
  22. local cacheSize = module:get_option_number("jwt_pubkey_cache_size", 128);
  23. local cache = require"util.cache".new(cacheSize);
  24. if allowEmptyToken == true then
  25. module:log("warn", "WARNING - empty tokens allowed");
  26. end
  27. if appId == nil then
  28. module:log("error", "'app_id' must not be empty");
  29. return;
  30. end
  31. if appSecret == nil and asapKeyServer == nil then
  32. module:log("error", "'app_secret' or 'asap_key_server' must be specified");
  33. return;
  34. end
  35. if asapKeyServer and not have_async then
  36. module:log("error", "requires a version of Prosody with util.async");
  37. return;
  38. end
  39. -- Extract 'token' param from BOSH URL when session is created
  40. module:hook("bosh-session", function(event)
  41. local session, request = event.session, event.request;
  42. local query = request.url.query;
  43. if query ~= nil then
  44. session.auth_token = query and formdecode(query).token or nil;
  45. end
  46. end);
  47. function provider.test_password(username, password)
  48. return nil, "Password based auth not supported";
  49. end
  50. function provider.get_password(username)
  51. return nil;
  52. end
  53. function provider.set_password(username, password)
  54. return nil, "Set password not supported";
  55. end
  56. function provider.user_exists(username)
  57. return nil;
  58. end
  59. function provider.create_user(username, password)
  60. return nil;
  61. end
  62. function provider.delete_user(username)
  63. return nil;
  64. end
  65. local http_timeout = 30;
  66. local http_headers = {
  67. ["User-Agent"] = "Prosody ("..prosody.version.."; "..prosody.platform..")"
  68. };
  69. function get_public_key(keyId)
  70. local content = cache:get(keyId);
  71. if content == nil then
  72. -- If the key is not found in the cache.
  73. module:log("debug", "Cache miss for key: "..keyId);
  74. local code;
  75. local wait, done = async.waiter();
  76. local function cb(content_, code_, response_, request_)
  77. content, code = content_, code_;
  78. done();
  79. end
  80. module:log("debug", "Fetching public key from: "..asapKeyServer..keyId);
  81. local request = http.request(asapKeyServer..keyId, {
  82. headers = http_headers or {},
  83. method = "GET"
  84. }, cb);
  85. -- TODO: Is the done() call racey? Can we cancel this if the request
  86. -- succeedes?
  87. local function cancel()
  88. -- TODO: This check is racey. Not likely to be a problem, but we should
  89. -- still stick a mutex on content / code at some point.
  90. if code == nil then
  91. http.destroy_request(request);
  92. done();
  93. end
  94. end
  95. timer.add_task(http_timeout, cancel);
  96. wait();
  97. if code == 200 or code == 204 then
  98. return content;
  99. end
  100. else
  101. -- If the key is in the cache, use it.
  102. module:log("debug", "Cache hit for key: "..keyId);
  103. return content;
  104. end
  105. return nil;
  106. end
  107. function provider.get_sasl_handler(session)
  108. -- JWT token extracted from BOSH URL
  109. local token = session.auth_token;
  110. local function get_username_from_token(self, message)
  111. if token == nil then
  112. if allowEmptyToken then
  113. return true;
  114. else
  115. return false, "not-allowed", "token required";
  116. end
  117. end
  118. local pubKey;
  119. if asapKeyServer and session.auth_token ~= nil then
  120. local dotFirst = session.auth_token:find("%.");
  121. if not dotFirst then return nil, "Invalid token" end
  122. local header = json.decode(basexx.from_url64(session.auth_token:sub(1,dotFirst-1)));
  123. local kid = header["kid"];
  124. if kid == nil then
  125. return false, "not-allowed", "'kid' claim is missing";
  126. end
  127. pubKey = get_public_key(kid);
  128. if pubKey == nil then
  129. return false, "not-allowed", "could not obtain public key";
  130. end
  131. end
  132. -- now verify the whole token
  133. local claims, msg;
  134. if asapKeyServer then
  135. claims, msg = token_util.verify_token(token, appId, pubKey, disableRoomNameConstraints);
  136. else
  137. claims, msg = token_util.verify_token(token, appId, appSecret, disableRoomNameConstraints);
  138. end
  139. if claims ~= nil then
  140. -- Binds room name to the session which is later checked on MUC join
  141. session.jitsi_meet_room = claims["room"];
  142. return true;
  143. else
  144. return false, "not-allowed", msg;
  145. end
  146. end
  147. return new_sasl(host, { anonymous = get_username_from_token });
  148. end
  149. module:provides("auth", provider);
  150. local function anonymous(self, message)
  151. local username = generate_uuid();
  152. -- This calls the handler created in 'provider.get_sasl_handler(session)'
  153. local result, err, msg = self.profile.anonymous(self, username, self.realm);
  154. self.username = username;
  155. if result == true then
  156. return "success";
  157. else
  158. return "failure", err, msg;
  159. end
  160. end
  161. sasl.registerMechanism("ANONYMOUS", {"anonymous"}, anonymous);