Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

util.lib.lua 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 jwt = require "luajwtjitsi";
  7. local http = require "net.http";
  8. local json = require "cjson";
  9. local path = require "util.paths";
  10. local sha256 = require "util.hashes".sha256;
  11. local timer = require "util.timer";
  12. local http_timeout = 30;
  13. local http_headers = {
  14. ["User-Agent"] = "Prosody ("..prosody.version.."; "..prosody.platform..")"
  15. };
  16. -- TODO: Figure out a less arbitrary default cache size.
  17. local cacheSize = module:get_option_number("jwt_pubkey_cache_size", 128);
  18. local cache = require"util.cache".new(cacheSize);
  19. local Util = {}
  20. Util.__index = Util
  21. --- Constructs util class for token verifications.
  22. -- Constructor that uses the passed module to extract all the
  23. -- needed configurations.
  24. -- If confuguration is missing returns nil
  25. -- @param module the module in which options to check for configs.
  26. -- @return the new instance or nil
  27. function Util.new(module)
  28. local self = setmetatable({}, Util)
  29. self.appId = module:get_option_string("app_id");
  30. self.appSecret = module:get_option_string("app_secret");
  31. self.asapKeyServer = module:get_option_string("asap_key_server");
  32. self.allowEmptyToken = module:get_option_boolean("allow_empty_token");
  33. if self.allowEmptyToken == true then
  34. module:log("warn", "WARNING - empty tokens allowed");
  35. end
  36. if self.appId == nil then
  37. module:log("error", "'app_id' must not be empty");
  38. return nil;
  39. end
  40. if self.appSecret == nil and self.asapKeyServer == nil then
  41. module:log("error", "'app_secret' or 'asap_key_server' must be specified");
  42. return nil;
  43. end
  44. if self.asapKeyServer and not have_async then
  45. module:log("error", "requires a version of Prosody with util.async");
  46. return nil;
  47. end
  48. return self
  49. end
  50. --- Returns the public key by keyID
  51. -- @param keyId the key ID to request
  52. -- @return the public key (the content of requested resource) or nil
  53. function Util:get_public_key(keyId)
  54. local content = cache:get(keyId);
  55. if content == nil then
  56. -- If the key is not found in the cache.
  57. module:log("debug", "Cache miss for key: "..keyId);
  58. local code;
  59. local wait, done = async.waiter();
  60. local function cb(content_, code_, response_, request_)
  61. content, code = content_, code_;
  62. if code == 200 or code == 204 then
  63. cache:set(keyId, content);
  64. end
  65. done();
  66. end
  67. local keyurl = path.join(self.asapKeyServer, hex.to(sha256(keyId))..'.pem');
  68. module:log("debug", "Fetching public key from: "..keyurl);
  69. -- We hash the key ID to work around some legacy behavior and make
  70. -- deployment easier. It also helps prevent directory
  71. -- traversal attacks (although path cleaning could have done this too).
  72. local request = http.request(keyurl, {
  73. headers = http_headers or {},
  74. method = "GET"
  75. }, cb);
  76. -- TODO: Is the done() call racey? Can we cancel this if the request
  77. -- succeedes?
  78. local function cancel()
  79. -- TODO: This check is racey. Not likely to be a problem, but we should
  80. -- still stick a mutex on content / code at some point.
  81. if code == nil then
  82. http.destroy_request(request);
  83. done();
  84. end
  85. end
  86. timer.add_task(http_timeout, cancel);
  87. wait();
  88. if code == 200 or code == 204 then
  89. return content;
  90. end
  91. else
  92. -- If the key is in the cache, use it.
  93. module:log("debug", "Cache hit for key: "..keyId);
  94. return content;
  95. end
  96. return nil;
  97. end
  98. --- Verifies token
  99. -- @param token the token to verify
  100. -- @return nil and error or the extracted claims from the token
  101. function Util:verify_token(token)
  102. local claims, err = jwt.decode(token, self.appSecret, true);
  103. if claims == nil then
  104. return nil, err;
  105. end
  106. local alg = claims["alg"];
  107. if alg ~= nil and (alg == "none" or alg == "") then
  108. return nil, "'alg' claim must not be empty";
  109. end
  110. local issClaim = claims["iss"];
  111. if issClaim == nil then
  112. return nil, "'iss' claim is missing";
  113. end
  114. if issClaim ~= self.appId then
  115. return nil, "Invalid application ID('iss' claim)";
  116. end
  117. local roomClaim = claims["room"];
  118. if roomClaim == nil then
  119. return nil, "'room' claim is missing";
  120. end
  121. return claims;
  122. end
  123. --- Verifies token and process needed values to be stored in the session.
  124. -- Stores in session the following values:
  125. -- session.jitsi_meet_room - the room name value from the token
  126. -- @param session the current session
  127. -- @param token the token to verify
  128. -- @return false and error
  129. function Util:process_and_verify_token(session, token)
  130. if token == nil then
  131. if self.allowEmptyToken then
  132. return true;
  133. else
  134. return false, "not-allowed", "token required";
  135. end
  136. end
  137. local pubKey;
  138. if self.asapKeyServer and session.auth_token ~= nil then
  139. local dotFirst = session.auth_token:find("%.");
  140. if not dotFirst then return nil, "Invalid token" end
  141. local header = json.decode(basexx.from_url64(session.auth_token:sub(1,dotFirst-1)));
  142. local kid = header["kid"];
  143. if kid == nil then
  144. return false, "not-allowed", "'kid' claim is missing";
  145. end
  146. pubKey = self:get_public_key(kid);
  147. if pubKey == nil then
  148. return false, "not-allowed", "could not obtain public key";
  149. end
  150. end
  151. -- now verify the whole token
  152. local claims, msg;
  153. if self.asapKeyServer then
  154. claims, msg = self:verify_token(token);
  155. else
  156. claims, msg = self:verify_token(token);
  157. end
  158. if claims ~= nil then
  159. -- Binds room name to the session which is later checked on MUC join
  160. session.jitsi_meet_room = claims["room"];
  161. return true;
  162. else
  163. return false, "not-allowed", msg;
  164. end
  165. end
  166. --- Verifies room name if necesarry.
  167. -- Checks configs and if necessary checks the room name extracted from
  168. -- room_address against the one saved in the session when token was verified
  169. -- @param session the current session
  170. -- @param room_address the whole room address as received
  171. -- @return returns true in case room was verified or there is no need to verify
  172. -- it and returns false in case verification was processed
  173. -- and was not successful
  174. function Util:verify_room(session, room_address)
  175. if self.allowEmptyToken and session.auth_token == nil then
  176. module:log(
  177. "debug",
  178. "Skipped room token verification - empty tokens are allowed");
  179. return true;
  180. end
  181. local room = string.match(room_address, "^(%w+)@");
  182. if room == nil then
  183. log("error",
  184. "Unable to get name of the MUC room ? to: %s", room_address);
  185. return true;
  186. end
  187. local auth_room = session.jitsi_meet_room;
  188. if room ~= string.lower(auth_room) then
  189. return false;
  190. end
  191. return true;
  192. end
  193. return Util;