forked from 3scale/APIcast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauths_cache.lua
More file actions
66 lines (53 loc) · 1.92 KB
/
Copy pathauths_cache.lua
File metadata and controls
66 lines (53 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
local keys_helper = require('apicast.policy.3scale_batcher.keys_helper')
local str_find = string.find
local str_sub = string.sub
local setmetatable = setmetatable
local format = string.format
local tonumber = tonumber
local _M = {}
local mt = { __index = _M }
--- Initialize a cache for authorizations.
-- @tparam storage ngx.shared.dict Shared dict to store the authorizations
-- @tparam ttl integer TTL for the cached authorizations
-- @treturn AuthsCache New cache for authorizations
function _M.new(storage, ttl)
local self = setmetatable({}, mt)
self.storage = storage
self.ttl = ttl
return self
end
local function value_to_cache(auth_status, rejection_reason)
if rejection_reason then
return format("%s:%s", auth_status, rejection_reason)
else
return auth_status
end
end
--- Get a cached authorization.
-- @tparam transaction Transaction A transaction
-- @treturn table The table has a "status" and a "rejection_reason"
function _M:get(transaction)
local key = keys_helper.key_for_cached_auth(transaction)
local cached_value = self.storage:get(key)
if not cached_value then return nil end
local colon = str_find(cached_value, ':', 1, true)
if colon then
return tonumber(str_sub(cached_value, 1, colon - 1)),
str_sub(cached_value, colon + 1)
end
return tonumber(cached_value)
end
--- Store an authorization in the cache.
-- @tparam transaction Transaction A transaction
-- @tparam auth_status integer Status returned by backend (200, 409, etc.)
-- @tparam[opt] rejection_reason string Rejection reason given by backend
-- when it denies an authorization
function _M:set(transaction, auth_status, rejection_reason)
local key = keys_helper.key_for_cached_auth(transaction)
local val_to_cache = value_to_cache(auth_status, rejection_reason)
local ok, err = self.storage:set(key, val_to_cache, self.ttl)
if not ok then
ngx.log(ngx.ERR, 'Failed to set value in storage: ', err)
end
end
return _M