@@ -143,7 +143,7 @@ function _M:extractContextVars(tokenInfo)
143143 cachingObj .oauth_token_scope = tokenInfo .token .scope
144144 cachingObj .oauth_token_client_id = tokenInfo .token .client_id
145145 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
147147 return cachingObj
148148end
149149
@@ -161,7 +161,7 @@ function _M:checkResponseFromAuth(res, cacheLookupKey)
161161 local cachingObj = self :extractContextVars (json )
162162
163163 self :setContextProperties (cachingObj )
164- self :storeTokenInCache (cacheLookupKey , cachingObj , json . expires_at )
164+ self :storeTokenInCache (cacheLookupKey , cachingObj , cachingObj . oauth_token_expires_at )
165165 return true
166166 end
167167 end
@@ -210,14 +210,17 @@ function _M:validateOAuthToken()
210210 self :setKeyInLocalCache (cacheLookupKey , cachedToken , local_expire_in , " cachedOauthTokens" )
211211 self :setContextProperties (obj )
212212 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 )
213223 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 )
221224 end
222225
223226 ngx .log (ngx .INFO , " Failed to get oauth token from cache falling back to oauth provider" )
@@ -247,6 +250,12 @@ function _M:validateRequest()
247250 return self :exitFn (self :validateOAuthToken ())
248251end
249252
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
250259
251260return _M
252261
0 commit comments