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 )
61
- return json .valid or false , 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
@@ -133,7 +133,7 @@ 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 )
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,14 +166,13 @@ function _M:getTokenFromCache(cacheLookupKey)
166
166
return nil ;
167
167
end
168
168
169
- -- imsAuth will validate the service token passed in "Authorization" header --
170
- function _M : validate_ims_token ()
169
+ function _M : validateOAuthToken ()
170
+
171
171
local oauth_host = ngx .var .oauth_host
172
- local oauth_token = ngx .var .authtoken
172
+ local oauth_token = self . authtoken or ngx .var .authtoken
173
173
174
- -- ngx.var.authtoken needs to be set before calling this method
175
174
if oauth_token == nil or oauth_token == " " then
176
- return self : exitFn ( RESPONSES .MISSING_TOKEN .error_code , cjson .encode (RESPONSES .MISSING_TOKEN ) )
175
+ return self . RESPONSES .MISSING_TOKEN .error_code , cjson .encode (self . RESPONSES .MISSING_TOKEN )
177
176
end
178
177
179
178
-- 1. try to get token info from the cache first ( local or redis cache )
@@ -190,37 +189,40 @@ function _M:validate_ims_token()
190
189
ngx .log (ngx .DEBUG , " Caching locally a new token for " .. tostring (local_expire_in ) .. " s, out of a total validity of " .. tostring (tokenValidity ) .. " s." )
191
190
self :setKeyInLocalCache (cacheLookupKey , cachedToken , local_expire_in , " cachedOauthTokens" )
192
191
self :setContextProperties (obj )
193
- return self : exitFn ( ngx .HTTP_OK )
192
+ return ngx .HTTP_OK
194
193
end
195
194
-- at this point the cached token is not valid
196
195
ngx .log (ngx .WARN , " Invalid OAuth Token found in cache. OAuth host=" .. tostring (oauth_host ))
197
196
if (error == nil ) then
198
- error = RESPONSES .INVALID_TOKEN
197
+ error = self . RESPONSES .INVALID_TOKEN
199
198
end
200
- error .error_code = error .error_code or RESPONSES .INVALID_TOKEN .error_code
201
- return self : exitFn ( error .error_code , cjson .encode (error ) )
199
+ error .error_code = error .error_code or self . RESPONSES .INVALID_TOKEN .error_code
200
+ return error .error_code , cjson .encode (error )
202
201
end
203
202
204
203
-- 2. validate the token with the OAuth endpoint
205
- local res = ngx .location .capture (" /validate-token" , { share_all_vars = true })
204
+ local res = ngx .location .capture (" /validate-token" , {
205
+ share_all_vars = true ,
206
+ args = { authtoken = oauth_token }
207
+ })
206
208
if res .status == ngx .HTTP_OK then
207
209
local tokenValidity , error = self :checkResponseFromAuth (res , cacheLookupKey )
208
210
if (tokenValidity == true ) then
209
- return self : exitFn ( ngx .HTTP_OK )
211
+ return ngx .HTTP_OK
210
212
end
211
213
-- at this point the token is not valid
212
214
ngx .log (ngx .WARN , " Invalid OAuth Token returned. OAuth host=" .. tostring (oauth_host ))
213
215
if (error == nil ) then
214
- error = RESPONSES .INVALID_TOKEN
216
+ error = self . RESPONSES .INVALID_TOKEN
215
217
end
216
- error .error_code = error .error_code or RESPONSES .INVALID_TOKEN .error_code
217
- return self : exitFn ( error .error_code , cjson .encode (error ) )
218
+ error .error_code = error .error_code or self . RESPONSES .INVALID_TOKEN .error_code
219
+ return error .error_code , cjson .encode (error )
218
220
end
219
- return self : exitFn ( res .status , cjson .encode (RESPONSES .UNKNOWN_ERROR ) );
221
+ return res .status , cjson .encode (self . RESPONSES .UNKNOWN_ERROR );
220
222
end
221
223
222
- function _M :validateRequest (obj )
223
- return self :validate_ims_token ( )
224
+ function _M :validateRequest ()
225
+ return self :exitFn ( self : validateOAuthToken () )
224
226
end
225
227
226
228
0 commit comments