|
@@ -1,7 +1,7 @@
|
1
|
1
|
-- Token authentication
|
2
|
2
|
-- Copyright (C) 2015 Atlassian
|
3
|
3
|
|
4
|
|
-local basexx = require 'basexx'
|
|
4
|
+local basexx = require 'basexx';
|
5
|
5
|
local have_async, async = pcall(require, "util.async");
|
6
|
6
|
local formdecode = require "util.http".formdecode;
|
7
|
7
|
local generate_uuid = require "util.uuid".generate;
|
|
@@ -25,6 +25,10 @@ local asapKeyServer = module:get_option_string("asap_key_server");
|
25
|
25
|
local allowEmptyToken = module:get_option_boolean("allow_empty_token");
|
26
|
26
|
local disableRoomNameConstraints = module:get_option_boolean("disable_room_name_constraints");
|
27
|
27
|
|
|
28
|
+-- TODO: Figure out a less arbitrary default cache size.
|
|
29
|
+local cacheSize = module:get_option_number("jwt_pubkey_cache_size", 128);
|
|
30
|
+local cache = require"util.cache".new(cacheSize);
|
|
31
|
+
|
28
|
32
|
if allowEmptyToken == true then
|
29
|
33
|
module:log("warn", "WARNING - empty tokens allowed");
|
30
|
34
|
end
|
|
@@ -82,23 +86,31 @@ local http_headers = {
|
82
|
86
|
["User-Agent"] = "Prosody ("..prosody.version.."; "..prosody.platform..")"
|
83
|
87
|
};
|
84
|
88
|
|
85
|
89
|
function get_public_key(keyId)
|
86
|
|
- local wait, done = async.waiter();
|
87
|
|
- local content, code; --, request, response;
|
88
|
|
- local function cb(content_, code_, response_, request_)
|
89
|
|
- content, code = content_, code_;
|
90
|
|
- done();
|
91
|
|
- end
|
92
|
|
- local request = http.request(path.join(asapKeyServer, keyId), {
|
93
|
|
- headers = http_headers or {},
|
94
|
|
- method = "GET"
|
95
|
|
- }, cb);
|
96
|
|
- -- TODO: Is the done() call racey?
|
97
|
|
- timer.add_task(http_timeout, function() http.destroy_request(request); done(); end);
|
98
|
|
- wait();
|
99
|
|
-
|
100
|
|
- if code == 200 or code == 204 then
|
|
90
|
+ local content = cache:get(keyId);
|
|
91
|
+ if content == nil then
|
|
92
|
+ -- If the key is not found in the cache.
|
|
93
|
+ module:log("debug", "Cache miss for key: "..keyId);
|
|
94
|
+ local code;
|
|
95
|
+ local wait, done = async.waiter();
|
|
96
|
+ local function cb(content_, code_, response_, request_)
|
|
97
|
+ content, code = content_, code_;
|
|
98
|
+ done();
|
|
99
|
+ end
|
|
100
|
+ local request = http.request(path.join(asapKeyServer, keyId), {
|
|
101
|
+ headers = http_headers or {},
|
|
102
|
+ method = "GET"
|
|
103
|
+ }, cb);
|
|
104
|
+ -- TODO: Is the done() call racey?
|
|
105
|
+ timer.add_task(http_timeout, function() http.destroy_request(request); done(); end);
|
|
106
|
+ wait();
|
|
107
|
+
|
|
108
|
+ if code == 200 or code == 204 then
|
|
109
|
+ module:log("debug", "Cache hit for key: "..keyId);
|
|
110
|
+ return content;
|
|
111
|
+ end
|
|
112
|
+ else
|
|
113
|
+ -- If the key is in the cache, use it.
|
101
|
114
|
return content;
|
102
|
115
|
end
|
103
|
116
|
|