4141local BaseValidator = require " api-gateway.validation.validator"
4242local cjson = require " cjson"
4343
44- local _M = BaseValidator :new ()
45-
46- local RESPONSES = {
47- MISSING_TOKEN = { error_code = " 403010 " , message = " Oauth token is missing " },
48- INVALID_TOKEN = { error_code = " 401013 " , message = " Oauth token is not valid " },
49- -- TOKEN_MISSMATCH is reserved for classes overwriting the isTokenValid method
50- TOKEN_MISSMATCH = { error_code = " 401014 " , message = " Token not allowed in the current context " },
51- SCOPE_MISMATCH = { error_code = " 401015 " , message = " Scope mismatch " },
52- UNKNOWN_ERROR = { error_code = " 503010 " , message = " Could not validate the oauth token " }
53- }
44+ local _M = BaseValidator :new ({
45+ RESPONSES = {
46+ MISSING_TOKEN = { error_code = " 403010 " , message = " Oauth token is missing " },
47+ INVALID_TOKEN = { error_code = " 401013 " , message = " Oauth token is not valid " },
48+ -- TOKEN_MISSMATCH is reserved for classes overwriting the isTokenValid method
49+ TOKEN_MISSMATCH = { error_code = " 401014 " , message = " Token not allowed in the current context " },
50+ SCOPE_MISMATCH = { error_code = " 401015 " , message = " Scope mismatch " },
51+ UNKNOWN_ERROR = { error_code = " 503010 " , message = " Could not validate the oauth token " }
52+ }
53+ })
5454
5555---
5656-- Maximum time in seconds specifying how long to cache a valid token in GW's memory
5757local LOCAL_CACHE_TTL = 60
5858
5959-- Hook to override the logic verifying if a token is valid
60- function _M :isTokenValid (json , validation_config )
61- return json .valid or false , validation_config .RESPONSES .INVALID_TOKEN
60+ function _M :isTokenValid (json )
61+ return json .valid or false , self .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 , validation_config )
132+ function _M :checkResponseFromAuth (res , cacheLookupKey )
133133 local json = cjson .decode (res .body )
134134 if json ~= nil then
135135
136- local tokenValidity , error = self :isTokenValid (json , validation_config )
136+ local tokenValidity , error = self :isTokenValid (json )
137137 if not tokenValidity and error ~= nil then
138138 return tokenValidity , error
139139 end
@@ -166,16 +166,13 @@ function _M:getTokenFromCache(cacheLookupKey)
166166 return nil ;
167167end
168168
169- function _M :validateOAuthToken (validation_config )
170-
171- validation_config = validation_config or {}
172- validation_config .RESPONSES = validation_config .RESPONSES or RESPONSES ;
169+ function _M :validateOAuthToken ()
173170
174171 local oauth_host = ngx .var .oauth_host
175- local oauth_token = validation_config .authtoken or ngx .var .authtoken
172+ local oauth_token = self .authtoken or ngx .var .authtoken
176173
177174 if oauth_token == nil or oauth_token == " " then
178- return validation_config .RESPONSES .MISSING_TOKEN .error_code , cjson .encode (validation_config .RESPONSES .MISSING_TOKEN )
175+ return self .RESPONSES .MISSING_TOKEN .error_code , cjson .encode (self .RESPONSES .MISSING_TOKEN )
179176 end
180177
181178 -- 1. try to get token info from the cache first ( local or redis cache )
@@ -197,9 +194,9 @@ function _M:validateOAuthToken(validation_config)
197194 -- at this point the cached token is not valid
198195 ngx .log (ngx .WARN , " Invalid OAuth Token found in cache. OAuth host=" .. tostring (oauth_host ))
199196 if (error == nil ) then
200- error = validation_config .RESPONSES .INVALID_TOKEN
197+ error = self .RESPONSES .INVALID_TOKEN
201198 end
202- error .error_code = error .error_code or validation_config .RESPONSES .INVALID_TOKEN .error_code
199+ error .error_code = error .error_code or self .RESPONSES .INVALID_TOKEN .error_code
203200 return error .error_code , cjson .encode (error )
204201 end
205202
@@ -209,23 +206,23 @@ function _M:validateOAuthToken(validation_config)
209206 args = { authtoken = oauth_token }
210207 })
211208 if res .status == ngx .HTTP_OK then
212- local tokenValidity , error = self :checkResponseFromAuth (res , cacheLookupKey , validation_config )
209+ local tokenValidity , error = self :checkResponseFromAuth (res , cacheLookupKey )
213210 if (tokenValidity == true ) then
214211 return ngx .HTTP_OK
215212 end
216213 -- at this point the token is not valid
217214 ngx .log (ngx .WARN , " Invalid OAuth Token returned. OAuth host=" .. tostring (oauth_host ))
218215 if (error == nil ) then
219- error = validation_config .RESPONSES .INVALID_TOKEN
216+ error = self .RESPONSES .INVALID_TOKEN
220217 end
221- error .error_code = error .error_code or validation_config .RESPONSES .INVALID_TOKEN .error_code
218+ error .error_code = error .error_code or self .RESPONSES .INVALID_TOKEN .error_code
222219 return error .error_code , cjson .encode (error )
223220 end
224- return res .status , cjson .encode (validation_config .RESPONSES .UNKNOWN_ERROR );
221+ return res .status , cjson .encode (self .RESPONSES .UNKNOWN_ERROR );
225222end
226223
227- function _M :validateRequest (validation_config )
228- return self :exitFn (self :validateOAuthToken (validation_config ))
224+ function _M :validateRequest ()
225+ return self :exitFn (self :validateOAuthToken ())
229226end
230227
231228
0 commit comments