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.

util.lib.lua 7.3KB

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