@@ -143,7 +143,7 @@ function _M:extractContextVars(tokenInfo)
143
143
cachingObj .oauth_token_scope = tokenInfo .token .scope
144
144
cachingObj .oauth_token_client_id = tokenInfo .token .client_id
145
145
cachingObj .oauth_token_user_id = tokenInfo .token .user_id
146
- cachingObj .oauth_token_expires_at = tokenInfo .expires_at -- NOTE: Assumption: value in ms
146
+ cachingObj .oauth_token_expires_at = self : getMaxLocalCacheTTL ( tokenInfo .expires_at ) -- NOTE: Assumption: value in ms
147
147
return cachingObj
148
148
end
149
149
@@ -161,7 +161,7 @@ function _M:checkResponseFromAuth(res, cacheLookupKey)
161
161
local cachingObj = self :extractContextVars (json )
162
162
163
163
self :setContextProperties (cachingObj )
164
- self :storeTokenInCache (cacheLookupKey , cachingObj , json . expires_at )
164
+ self :storeTokenInCache (cacheLookupKey , cachingObj , cachingObj . oauth_token_expires_at )
165
165
return true
166
166
end
167
167
end
@@ -210,14 +210,17 @@ function _M:validateOAuthToken()
210
210
self :setKeyInLocalCache (cacheLookupKey , cachedToken , local_expire_in , " cachedOauthTokens" )
211
211
self :setContextProperties (obj )
212
212
return ngx .HTTP_OK
213
+ elseif (tokenValidity == 0 ) then
214
+ ngx .log (ngx .DEBUG , " Token still valid but about to expire in less than 1s" )
215
+ else
216
+ -- at this point the cached token is not valid
217
+ ngx .log (ngx .INFO , " Invalid OAuth Token found in cache. OAuth host=" .. tostring (oauth_host ))
218
+ if (error == nil ) then
219
+ error = self .RESPONSES .INVALID_TOKEN
220
+ end
221
+ error .error_code = error .error_code or self .RESPONSES .INVALID_TOKEN .error_code
222
+ return error .error_code , cjson .encode (error )
213
223
end
214
- -- at this point the cached token is not valid
215
- ngx .log (ngx .INFO , " Invalid OAuth Token found in cache. OAuth host=" .. tostring (oauth_host ))
216
- if (error == nil ) then
217
- error = self .RESPONSES .INVALID_TOKEN
218
- end
219
- error .error_code = error .error_code or self .RESPONSES .INVALID_TOKEN .error_code
220
- return error .error_code , cjson .encode (error )
221
224
end
222
225
223
226
ngx .log (ngx .INFO , " Failed to get oauth token from cache falling back to oauth provider" )
@@ -247,6 +250,12 @@ function _M:validateRequest()
247
250
return self :exitFn (self :validateOAuthToken ())
248
251
end
249
252
253
+ function _M :getMaxLocalCacheTTL (expires_at )
254
+ if ngx .var .max_oauth_local_cache_ttl ~= nil and ngx .var .max_oauth_local_cache_ttl ~= ' ' then
255
+ expires_at = math.min (expires_at , (ngx .var .max_oauth_local_cache_ttl + ngx .time ()) * 1000 )
256
+ end
257
+ return expires_at
258
+ end
250
259
251
260
return _M
252
261
0 commit comments