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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
local tablex = require "pl.tablex" local EMPTY = tablex.readonly {} local kong = kong local type = type local mt_cache = { __mode = "k" } local setmetatable = setmetatable local consumer_apikeys_cache = setmetatable({}, mt_cache) local consumer_in_apikeys_cache = setmetatable({}, mt_cache) local apikeys_secret_cache = setmetatable({}, mt_cache) local function load_secret_into_memory(apikey) local secrets, err = kong.db.wettpersign_credentials:select_by_apikey(apikey) if err or secrets == nil then return nil, err end return secrets end local function get_apikeys_secret_raw(apikey) local cache_key = kong.db.wettpersign_credentials:cache_key(apikey)
local raw_secret, err = kong.cache:get(cache_key, nil, load_secret_into_memory, apikey) if err then return nil, err end return raw_secret or EMPTY end local function load_apikeys_into_memory(consumer_pk) local apikeys = {} local len = 0 for row, err in kong.db.wettpersign_credentials:each_for_consumer(consumer_pk, 1000) do if err then return nil, err end len = len + 1 apikeys[len] = row end return apikeys end local function get_consumer_apikeys_raw(consumer_id) local cache_key = kong.db.wettpersign_credentials:cache_key(consumer_id) local raw_apikeys, err = kong.cache:get(cache_key, nil, load_apikeys_into_memory, { id = consumer_id }) if err then return nil, err end return raw_apikeys or EMPTY end local function get_consumer_apikeys(consumer_id) local raw_apikeys, err = get_consumer_apikeys_raw(consumer_id) if not raw_apikeys then return nil, err end local apikeys = consumer_apikeys_cache[raw_apikeys] if not apikeys then apikeys = {} consumer_apikeys_cache[raw_apikeys] = apikeys for i = 1, #raw_apikeys do local apikey = raw_apikeys[i].apikey apikeys[i] = apikey apikeys[apikey] = apikey end end return apikeys end local function consumer_in_apikeys(apikeys_to_check, consumer_apikeys) local result1 = consumer_in_apikeys_cache[apikeys_to_check] if result1 == nil then result1 = setmetatable({}, mt_cache) consumer_in_apikeys_cache[apikeys_to_check] = result1 end local result2 = result1[consumer_apikeys] if result2 ~= nil then return result2 end result2 = false for i = 1, #apikeys_to_check do if consumer_apikeys[apikeys_to_check[i]] then result2 = true break end end result1[consumer_apikeys] = result2 return result2 end local function get_current_consumer_id() return (kong.client.get_consumer() or EMPTY).id or (kong.client.get_credential() or EMPTY).consumer_id end local function get_authenticated_apikeys() local authenticated_apikeys = kong.ctx.shared.authenticated_apikeys if type(authenticated_apikeys) ~= "table" then authenticated_apikeys = ngx.ctx.authenticated_apikeys if authenticated_apikeys == nil then return nil end if type(authenticated_apikeys) ~= "table" then kong.log.warn("invalid authenticated_apikeys, a table was expected") return nil end end local apikeys = {} for i = 1, #authenticated_apikeys do apikeys[i] = authenticated_apikeys[i] apikeys[authenticated_apikeys[i]] = authenticated_apikeys[i] end return apikeys end local function apikey_in_apikeys(apikeys_to_check, apikeys) for i = 1, #apikeys_to_check do if apikeys[apikeys_to_check[i]] then return true end end end return { get_current_consumer_id = get_current_consumer_id, get_consumer_apikeys = get_consumer_apikeys, get_authenticated_apikeys = get_authenticated_apikeys, consumer_in_apikeys = consumer_in_apikeys, apikey_in_apikeys = apikey_in_apikeys, get_apikeys_secret_raw = get_apikeys_secret_raw, }
|