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 )
61-     return  json .valid  or  false , 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
@@ -133,7 +133,7 @@ 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 )
136+         local  tokenValidity , error  =  self :isTokenValid (json )
137137        if  not  tokenValidity  and  error  ~=  nil  then 
138138            return  tokenValidity , error 
139139        end 
@@ -166,14 +166,13 @@ 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 () 
170+ 
171171    local  oauth_host  =  ngx .var .oauth_host 
172-     local  oauth_token  =  ngx .var .authtoken 
172+     local  oauth_token  =  self . authtoken   or   ngx .var .authtoken 
173173
174-     --  ngx.var.authtoken needs to be set before calling this method
175174    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 )
177176    end 
178177
179178    -- 1. try to get token info from the cache first ( local or redis cache )
@@ -190,37 +189,40 @@ function _M:validate_ims_token()
190189            ngx .log (ngx .DEBUG , " Caching locally a new token for "  ..  tostring (local_expire_in ) ..  "  s, out of a total validity of "  ..  tostring (tokenValidity  ) ..  "  s."  )
191190            self :setKeyInLocalCache (cacheLookupKey , cachedToken , local_expire_in   , " cachedOauthTokens"  )
192191            self :setContextProperties (obj )
193-             return  self : exitFn ( ngx .HTTP_OK ) 
192+             return  ngx .HTTP_OK 
194193        end 
195194        --  at this point the cached token is not valid
196195        ngx .log (ngx .WARN , " Invalid OAuth Token found in cache. OAuth host="  ..  tostring (oauth_host ))
197196        if  (error  ==  nil ) then 
198-             error  =  RESPONSES .INVALID_TOKEN 
197+             error  =  self . RESPONSES .INVALID_TOKEN 
199198        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 )
202201    end 
203202
204203    --  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+     })
206208    if  res .status  ==  ngx .HTTP_OK  then 
207209        local  tokenValidity , error  =  self :checkResponseFromAuth (res , cacheLookupKey )
208210        if  (tokenValidity  ==  true ) then 
209-             return  self : exitFn ( ngx .HTTP_OK ) 
211+             return  ngx .HTTP_OK 
210212        end 
211213        --  at this point the token is not valid
212214        ngx .log (ngx .WARN , " Invalid OAuth Token returned. OAuth host="  ..  tostring (oauth_host ))
213215        if  (error  ==  nil ) then 
214-             error  =  RESPONSES .INVALID_TOKEN 
216+             error  =  self . RESPONSES .INVALID_TOKEN 
215217        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 )
218220    end 
219-     return  self : exitFn ( res .status , cjson .encode (RESPONSES .UNKNOWN_ERROR ) );
221+     return  res .status , cjson .encode (self . RESPONSES .UNKNOWN_ERROR );
220222end 
221223
222- function  _M :validateRequest (obj )
223-     return  self :validate_ims_token ( )
224+ function  _M :validateRequest ()
225+     return  self :exitFn ( self : validateOAuthToken () )
224226end 
225227
226228
0 commit comments