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

mod_auth_token.lua 5.7KB

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