41
41
local BaseValidator = require " api-gateway.validation.validator"
42
42
local cjson = require " cjson"
43
43
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
+ })
54
54
55
55
---
56
56
-- Maximum time in seconds specifying how long to cache a valid token in GW's memory
57
57
local LOCAL_CACHE_TTL = 60
58
58
59
59
-- 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
62
62
end
63
63
64
64
-- override this if other checks need to be in place
@@ -129,11 +129,11 @@ end
129
129
130
130
-- TODO: cache invalid tokens too for a short while
131
131
-- Check in the response if the token is valid --
132
- function _M :checkResponseFromAuth (res , cacheLookupKey , validation_config )
132
+ function _M :checkResponseFromAuth (res , cacheLookupKey )
133
133
local json = cjson .decode (res .body )
134
134
if json ~= nil then
135
135
136
- local tokenValidity , error = self :isTokenValid (json , validation_config )
136
+ local tokenValidity , error = self :isTokenValid (json )
137
137
if not tokenValidity and error ~= nil then
138
138
return tokenValidity , error
139
139
end
@@ -166,16 +166,13 @@ function _M:getTokenFromCache(cacheLookupKey)
166
166
return nil ;
167
167
end
168
168
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 ()
173
170
174
171
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
176
173
177
174
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 )
179
176
end
180
177
181
178
-- 1. try to get token info from the cache first ( local or redis cache )
@@ -197,9 +194,9 @@ function _M:validateOAuthToken(validation_config)
197
194
-- at this point the cached token is not valid
198
195
ngx .log (ngx .WARN , " Invalid OAuth Token found in cache. OAuth host=" .. tostring (oauth_host ))
199
196
if (error == nil ) then
200
- error = validation_config .RESPONSES .INVALID_TOKEN
197
+ error = self .RESPONSES .INVALID_TOKEN
201
198
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
203
200
return error .error_code , cjson .encode (error )
204
201
end
205
202
@@ -209,23 +206,23 @@ function _M:validateOAuthToken(validation_config)
209
206
args = { authtoken = oauth_token }
210
207
})
211
208
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 )
213
210
if (tokenValidity == true ) then
214
211
return ngx .HTTP_OK
215
212
end
216
213
-- at this point the token is not valid
217
214
ngx .log (ngx .WARN , " Invalid OAuth Token returned. OAuth host=" .. tostring (oauth_host ))
218
215
if (error == nil ) then
219
- error = validation_config .RESPONSES .INVALID_TOKEN
216
+ error = self .RESPONSES .INVALID_TOKEN
220
217
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
222
219
return error .error_code , cjson .encode (error )
223
220
end
224
- return res .status , cjson .encode (validation_config .RESPONSES .UNKNOWN_ERROR );
221
+ return res .status , cjson .encode (self .RESPONSES .UNKNOWN_ERROR );
225
222
end
226
223
227
- function _M :validateRequest (validation_config )
228
- return self :exitFn (self :validateOAuthToken (validation_config ))
224
+ function _M :validateRequest ()
225
+ return self :exitFn (self :validateOAuthToken ())
229
226
end
230
227
231
228
0 commit comments