@@ -57,8 +57,8 @@ local RESPONSES = {
5757local LOCAL_CACHE_TTL = 60
5858
5959-- Hook to override the logic verifying if a token is valid
60- function _M :istokenValid (json )
61- return json .valid or false , RESPONSES .INVALID_TOKEN
60+ function _M :isTokenValid (json , validation_config )
61+ return json .valid or false , validation_config . RESPONSES .INVALID_TOKEN
6262end
6363
6464-- override this if other checks need to be in place
@@ -129,11 +129,11 @@ end
129129
130130-- TODO: cache invalid tokens too for a short while
131131-- Check in the response if the token is valid --
132- function _M :checkResponseFromAuth (res , cacheLookupKey )
132+ function _M :checkResponseFromAuth (res , cacheLookupKey , validation_config )
133133 local json = cjson .decode (res .body )
134134 if json ~= nil then
135135
136- local tokenValidity , error = self :istokenValid (json )
136+ local tokenValidity , error = self :isTokenValid (json , validation_config )
137137 if not tokenValidity and error ~= nil then
138138 return tokenValidity , error
139139 end
@@ -166,15 +166,8 @@ function _M:getTokenFromCache(cacheLookupKey)
166166 return nil ;
167167end
168168
169- -- imsAuth will validate the service token passed in "Authorization" header --
170- function _M :validate_ims_token ()
169+ function _M :validateOAuthToken (oauth_token , validation_config )
171170 local oauth_host = ngx .var .oauth_host
172- local oauth_token = ngx .var .authtoken
173-
174- -- ngx.var.authtoken needs to be set before calling this method
175- if oauth_token == nil or oauth_token == " " then
176- return self :exitFn (RESPONSES .MISSING_TOKEN .error_code , cjson .encode (RESPONSES .MISSING_TOKEN ))
177- end
178171
179172 -- 1. try to get token info from the cache first ( local or redis cache )
180173 local oauth_token_hash = ngx .md5 (oauth_token )
@@ -190,37 +183,49 @@ function _M:validate_ims_token()
190183 ngx .log (ngx .DEBUG , " Caching locally a new token for " .. tostring (local_expire_in ) .. " s, out of a total validity of " .. tostring (tokenValidity ) .. " s." )
191184 self :setKeyInLocalCache (cacheLookupKey , cachedToken , local_expire_in , " cachedOauthTokens" )
192185 self :setContextProperties (obj )
193- return self : exitFn ( ngx .HTTP_OK )
186+ return ngx .HTTP_OK
194187 end
195188 -- at this point the cached token is not valid
196189 ngx .log (ngx .WARN , " Invalid OAuth Token found in cache. OAuth host=" .. tostring (oauth_host ))
197190 if (error == nil ) then
198- error = RESPONSES .INVALID_TOKEN
191+ error = validation_config . RESPONSES .INVALID_TOKEN
199192 end
200- error .error_code = error .error_code or RESPONSES .INVALID_TOKEN .error_code
201- return self : exitFn ( error .error_code , cjson .encode (error ) )
193+ error .error_code = error .error_code or validation_config . RESPONSES .INVALID_TOKEN .error_code
194+ return error .error_code , cjson .encode (error )
202195 end
203196
204197 -- 2. validate the token with the OAuth endpoint
205- local res = ngx .location .capture (" /validate-token" , { share_all_vars = true })
198+ local res = ngx .location .capture (" /validate-token" , {
199+ share_all_vars = true ,
200+ args = { authtoken = oauth_token }
201+ })
206202 if res .status == ngx .HTTP_OK then
207- local tokenValidity , error = self :checkResponseFromAuth (res , cacheLookupKey )
203+ local tokenValidity , error = self :checkResponseFromAuth (res , cacheLookupKey , validation_config )
208204 if (tokenValidity == true ) then
209- return self : exitFn ( ngx .HTTP_OK )
205+ return ngx .HTTP_OK
210206 end
211207 -- at this point the token is not valid
212208 ngx .log (ngx .WARN , " Invalid OAuth Token returned. OAuth host=" .. tostring (oauth_host ))
213209 if (error == nil ) then
214- error = RESPONSES .INVALID_TOKEN
210+ error = validation_config . RESPONSES .INVALID_TOKEN
215211 end
216- error .error_code = error .error_code or RESPONSES .INVALID_TOKEN .error_code
217- return self : exitFn ( error .error_code , cjson .encode (error ) )
212+ error .error_code = error .error_code or validation_config . RESPONSES .INVALID_TOKEN .error_code
213+ return error .error_code , cjson .encode (error )
218214 end
219- return self : exitFn ( res .status , cjson .encode (RESPONSES .UNKNOWN_ERROR ) );
215+ return res .status , cjson .encode (validation_config . RESPONSES .UNKNOWN_ERROR );
220216end
221217
222- function _M :validateRequest (obj )
223- return self :validate_ims_token ()
218+ function _M :validateRequest (validation_config )
219+ validation_config = validation_config or {}
220+ validation_config .RESPONSES = validation_config .RESPONSES or RESPONSES ;
221+
222+ local oauth_token = validation_config .authtoken or ngx .var .authtoken
223+
224+ if oauth_token == nil or oauth_token == " " then
225+ return validation_config .RESPONSES .MISSING_TOKEN .error_code , cjson .encode (validation_config .RESPONSES .MISSING_TOKEN )
226+ end
227+
228+ return self :validateOAuthToken (oauth_token , validation_config )
224229end
225230
226231
0 commit comments