Skip to content

Commit c83bb6f

Browse files
Extensibility improvements in the user profile validator
1 parent 37ead48 commit c83bb6f

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

src/lua/api-gateway/validation/oauth2/userProfileValidator.lua

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,11 @@ function _M:extractContextVars(profile)
186186
return cachingObj
187187
end
188188

189-
function _M:validateRequest()
189+
function _M:validateUserProfile()
190190
-- ngx.var.authtoken needs to be set before calling this method
191191
local oauth_token = ngx.var.authtoken
192192
if oauth_token == nil or oauth_token == "" then
193-
--return self:exitFn(ngx.HTTP_BAD_REQUEST)
194-
return self:exitFn(RESPONSES.P_MISSING_TOKEN.error_code, cjson.encode(RESPONSES.P_MISSING_TOKEN))
193+
return RESPONSES.P_MISSING_TOKEN.error_code, cjson.encode(RESPONSES.P_MISSING_TOKEN)
195194
end
196195

197196
--1. try to get user's profile from the cache first ( local or redis cache )
@@ -205,9 +204,9 @@ function _M:validateRequest()
205204
end
206205
self:setContextProperties(self:getContextPropertiesObject(cachedUserProfile))
207206
if ( self:isProfileValid(cachedUserProfile) == true ) then
208-
return self:exitFn(ngx.HTTP_OK)
207+
return ngx.HTTP_OK
209208
else
210-
return self:exitFn(RESPONSES.INVALID_PROFILE.error_code, cjson.encode(RESPONSES.INVALID_PROFILE))
209+
return RESPONSES.INVALID_PROFILE.error_code, cjson.encode(RESPONSES.INVALID_PROFILE)
211210
end
212211
end
213212

@@ -223,9 +222,9 @@ function _M:validateRequest()
223222
self:storeProfileInCache(cacheLookupKey, cachingObj)
224223

225224
if ( self:isProfileValid(cachingObj) == true ) then
226-
return self:exitFn(ngx.HTTP_OK)
225+
return ngx.HTTP_OK
227226
else
228-
return self:exitFn(RESPONSES.INVALID_PROFILE.error_code, cjson.encode(RESPONSES.INVALID_PROFILE))
227+
return RESPONSES.INVALID_PROFILE.error_code, cjson.encode(RESPONSES.INVALID_PROFILE)
229228
end
230229
else
231230
ngx.log(ngx.WARN, "Could not decode /validate-user response:" .. tostring(res.body) )
@@ -234,11 +233,15 @@ function _M:validateRequest()
234233
-- ngx.log(ngx.WARN, "Could not read /ims-profile. status=" .. res.status .. ".body=" .. res.body .. ". token=" .. ngx.var.authtoken)
235234
ngx.log(ngx.WARN, "Could not read /validate-user. status=" .. res.status .. ".body=" .. res.body )
236235
if ( res.status == ngx.HTTP_UNAUTHORIZED or res.status == ngx.HTTP_BAD_REQUEST ) then
237-
return self:exitFn(RESPONSES.NOT_ALLOWED.error_code, cjson.encode(RESPONSES.NOT_ALLOWED))
236+
return RESPONSES.NOT_ALLOWED.error_code, cjson.encode(RESPONSES.NOT_ALLOWED)
238237
end
239238
end
240239
--ngx.log(ngx.WARN, "Error validating Profile for Token:" .. tostring(ngx.var.authtoken))
241-
return self:exitFn(RESPONSES.P_UNKNOWN_ERROR.error_code, cjson.encode(RESPONSES.P_UNKNOWN_ERROR))
240+
return RESPONSES.P_UNKNOWN_ERROR.error_code, cjson.encode(RESPONSES.P_UNKNOWN_ERROR)
241+
end
242+
243+
function _M:validateRequest()
244+
return self:exitFn(self:validateUserProfile())
242245
end
243246

244247
return _M

src/lua/api-gateway/validation/validator.lua

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,44 @@ function BaseValidator:getRedisUpstream(upstream_name)
9191
end
9292

9393
-- retrieves a saved information from the Redis cache --
94-
-- the method uses HGET redis command --
94+
-- the method uses GET redis command --
9595
-- it returns the value of the key, when found in the cache, nil otherwise --
96+
-- for backward compatibility this method accepts a second argument, in which case it will perform a HGET instead.
9697
function BaseValidator:getKeyFromRedis(key, hash_name)
98+
99+
if hash_name ~= nil then
100+
return self:getHashValueFromRedis(key, hash_name)
101+
end
102+
103+
local redisread = redis:new()
104+
local redis_host, redis_port = self:getRedisUpstream(redis_RO_upstream)
105+
local ok, err = redisread:connect(redis_host, redis_port)
106+
if ok then
107+
local result, err = redisread:get(key)
108+
redisread:set_keepalive(30000, 100)
109+
if ( not result and err ~= nil ) then
110+
ngx.log(ngx.WARN, "Failed to read key " .. tostring(key) .. " from Redis cache:[", redis_host, ":", redis_port, "]. Error:", err)
111+
return nil
112+
else
113+
if (type(result) == 'string') then
114+
return result
115+
end
116+
end
117+
else
118+
ngx.log(ngx.WARN, "Failed to read key " .. tostring(key) .. " from Redis cache:[", redis_host, ":", redis_port, "]. Error:", err)
119+
end
120+
return nil;
121+
end
122+
123+
-- retrieves a saved information from the Redis cache --
124+
-- the method uses HGET redis command --
125+
-- it returns the value of the key, when found in the cache, nil otherwise --
126+
function BaseValidator:getHashValueFromRedis(key, hash_field)
97127
local redisread = redis:new()
98128
local redis_host, redis_port = self:getRedisUpstream(redis_RO_upstream)
99129
local ok, err = redisread:connect(redis_host, redis_port)
100130
if ok then
101-
local redis_key, selecterror = redisread:hget(key, hash_name)
131+
local redis_key, selecterror = redisread:hget(key, hash_field)
102132
redisread:set_keepalive(30000, 100)
103133
if (type(redis_key) == 'string') then
104134
return redis_key

0 commit comments

Comments
 (0)