Skip to content

Commit

Permalink
Extensibility improvements in the user profile validator
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianconstantin committed Aug 28, 2016
1 parent 37ead48 commit c83bb6f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
21 changes: 12 additions & 9 deletions src/lua/api-gateway/validation/oauth2/userProfileValidator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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

Expand All @@ -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) )
Expand All @@ -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
34 changes: 32 additions & 2 deletions src/lua/api-gateway/validation/validator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c83bb6f

Please sign in to comment.