diff --git a/src/lua/api-gateway/validation/oauth2/userProfileValidator.lua b/src/lua/api-gateway/validation/oauth2/userProfileValidator.lua index be1d479..14b057f 100644 --- a/src/lua/api-gateway/validation/oauth2/userProfileValidator.lua +++ b/src/lua/api-gateway/validation/oauth2/userProfileValidator.lua @@ -186,12 +186,11 @@ function _M:extractContextVars(profile) return cachingObj end -function _M:validateRequest() +function _M:validateUserProfile() -- ngx.var.authtoken needs to be set before calling this method local oauth_token = ngx.var.authtoken if oauth_token == nil or oauth_token == "" then - --return self:exitFn(ngx.HTTP_BAD_REQUEST) - return self:exitFn(RESPONSES.P_MISSING_TOKEN.error_code, cjson.encode(RESPONSES.P_MISSING_TOKEN)) + return RESPONSES.P_MISSING_TOKEN.error_code, cjson.encode(RESPONSES.P_MISSING_TOKEN) end --1. try to get user's profile from the cache first ( local or redis cache ) @@ -205,9 +204,9 @@ function _M:validateRequest() end self:setContextProperties(self:getContextPropertiesObject(cachedUserProfile)) if ( self:isProfileValid(cachedUserProfile) == true ) then - return self:exitFn(ngx.HTTP_OK) + return ngx.HTTP_OK else - return self:exitFn(RESPONSES.INVALID_PROFILE.error_code, cjson.encode(RESPONSES.INVALID_PROFILE)) + return RESPONSES.INVALID_PROFILE.error_code, cjson.encode(RESPONSES.INVALID_PROFILE) end end @@ -223,9 +222,9 @@ function _M:validateRequest() self:storeProfileInCache(cacheLookupKey, cachingObj) if ( self:isProfileValid(cachingObj) == true ) then - return self:exitFn(ngx.HTTP_OK) + return ngx.HTTP_OK else - return self:exitFn(RESPONSES.INVALID_PROFILE.error_code, cjson.encode(RESPONSES.INVALID_PROFILE)) + return RESPONSES.INVALID_PROFILE.error_code, cjson.encode(RESPONSES.INVALID_PROFILE) end else ngx.log(ngx.WARN, "Could not decode /validate-user response:" .. tostring(res.body) ) @@ -234,11 +233,15 @@ function _M:validateRequest() -- ngx.log(ngx.WARN, "Could not read /ims-profile. status=" .. res.status .. ".body=" .. res.body .. ". token=" .. ngx.var.authtoken) ngx.log(ngx.WARN, "Could not read /validate-user. status=" .. res.status .. ".body=" .. res.body ) if ( res.status == ngx.HTTP_UNAUTHORIZED or res.status == ngx.HTTP_BAD_REQUEST ) then - return self:exitFn(RESPONSES.NOT_ALLOWED.error_code, cjson.encode(RESPONSES.NOT_ALLOWED)) + return RESPONSES.NOT_ALLOWED.error_code, cjson.encode(RESPONSES.NOT_ALLOWED) end end --ngx.log(ngx.WARN, "Error validating Profile for Token:" .. tostring(ngx.var.authtoken)) - return self:exitFn(RESPONSES.P_UNKNOWN_ERROR.error_code, cjson.encode(RESPONSES.P_UNKNOWN_ERROR)) + return RESPONSES.P_UNKNOWN_ERROR.error_code, cjson.encode(RESPONSES.P_UNKNOWN_ERROR) +end + +function _M:validateRequest() + return self:exitFn(self:validateUserProfile()) end return _M \ No newline at end of file diff --git a/src/lua/api-gateway/validation/validator.lua b/src/lua/api-gateway/validation/validator.lua index bac55ca..39c4a62 100644 --- a/src/lua/api-gateway/validation/validator.lua +++ b/src/lua/api-gateway/validation/validator.lua @@ -91,14 +91,44 @@ function BaseValidator:getRedisUpstream(upstream_name) end -- retrieves a saved information from the Redis cache -- --- the method uses HGET redis command -- +-- the method uses GET redis command -- -- it returns the value of the key, when found in the cache, nil otherwise -- +-- for backward compatibility this method accepts a second argument, in which case it will perform a HGET instead. function BaseValidator:getKeyFromRedis(key, hash_name) + + if hash_name ~= nil then + return self:getHashValueFromRedis(key, hash_name) + end + + local redisread = redis:new() + local redis_host, redis_port = self:getRedisUpstream(redis_RO_upstream) + local ok, err = redisread:connect(redis_host, redis_port) + if ok then + local result, err = redisread:get(key) + redisread:set_keepalive(30000, 100) + if ( not result and err ~= nil ) then + ngx.log(ngx.WARN, "Failed to read key " .. tostring(key) .. " from Redis cache:[", redis_host, ":", redis_port, "]. Error:", err) + return nil + else + if (type(result) == 'string') then + return result + end + end + else + ngx.log(ngx.WARN, "Failed to read key " .. tostring(key) .. " from Redis cache:[", redis_host, ":", redis_port, "]. Error:", err) + end + return nil; +end + +-- retrieves a saved information from the Redis cache -- +-- the method uses HGET redis command -- +-- it returns the value of the key, when found in the cache, nil otherwise -- +function BaseValidator:getHashValueFromRedis(key, hash_field) local redisread = redis:new() local redis_host, redis_port = self:getRedisUpstream(redis_RO_upstream) local ok, err = redisread:connect(redis_host, redis_port) if ok then - local redis_key, selecterror = redisread:hget(key, hash_name) + local redis_key, selecterror = redisread:hget(key, hash_field) redisread:set_keepalive(30000, 100) if (type(redis_key) == 'string') then return redis_key