Skip to content

Commit bbf9f0b

Browse files
committed
The user_store function was modified for including the token validation, for achieving this, an extra validation through the key_type value of the api_key variable was used. If the key_value is equal to "api_key", the key_value is checked in the Mongo database. In case of the value of key_values is equal to "token", the token is sent to the PEP Proxy for validation, if the token is valid, the user information is returned.
If the user information is retrieved from the database this information is stored in the variables of the platform as usually did. In case that the user information was retrieved using token validation, the variables the Nick_name is assigned to the user_id and the Roles is assigned to the roles of the platform. Moreover, if the user information coming from of the token validation with PEP Proxy doesn't have the value of "email", this field is stored in the platform using the Nick_name value, this value is needed for displaying, the user associated with each request in the Analytics option of the web app . The rest of the changes are related to the reassignment of the api_key variable using api_key ["key_value"]
1 parent 07e2aa5 commit bbf9f0b

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

src/api-umbrella/proxy/user_store.lua

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,33 @@ local mongo = require "api-umbrella.utils.mongo"
88
local shcache = require "shcache"
99
local types = require "pl.types"
1010
local utils = require "api-umbrella.proxy.utils"
11+
local pep = require "api-umbrella.utils.pep"
1112

1213
local cache_computed_settings = utils.cache_computed_settings
1314
local is_empty = types.is_empty
1415

1516
local function lookup_user(api_key)
16-
local raw_user, err = mongo.first("api_users", {
17-
query = {
18-
api_key = api_key,
19-
},
20-
})
21-
22-
if err then
23-
ngx.log(ngx.ERR, "failed to fetch user from mongodb: ", err)
17+
local raw_user
18+
local db_err
19+
local pep_err
20+
21+
-- Checking the field of api_key ["key_type"], if the key_type is api_key
22+
-- the api_key value is checked in the database and retrieve the user information
23+
-- else if the key_type is token, the token is checked using PEP Proxy and
24+
-- the user information is retrieved
25+
if not api_key["key_type"] or api_key["key_type"] == "api_key" then
26+
raw_user, db_err = mongo.first("api_users", {
27+
query = {
28+
api_key = api_key["key_value"],
29+
},
30+
})
31+
elseif api_key["key_type"] == "token" then
32+
raw_user, pep_err = pep.first(config["gatekeeper"]["pep_host"],config["gatekeeper"]["pep_port"],api_key["key_value"])
33+
end
34+
if pep_err then
35+
ngx.log(ngx.ERR, "failed to autenticate , status code:", pep_err)
36+
elseif db_err then
37+
ngx.log(ngx.ERR, "failed to fetch user from mongodb", db_err)
2438
elseif raw_user then
2539
local user = utils.pick_where_present(raw_user, {
2640
"created_at",
@@ -36,15 +50,31 @@ local function lookup_user(api_key)
3650
-- Ensure IDs get stored as strings, even if Mongo ObjectIds are in use.
3751
if raw_user["_id"] and raw_user["_id"]["$oid"] then
3852
user["id"] = raw_user["_id"]["$oid"]
53+
elseif raw_user.Nick_Name then
54+
user["id"] = raw_user.Nick_Name
55+
if not raw_user.Email then
56+
user["email"] = raw_user.Nick_Name
57+
else
58+
user["email"] = raw_user.Email
59+
end
3960
else
40-
user["id"] = raw_user["_id"]
61+
user["id"] = raw_user["_id"]
62+
end
63+
-- If the validation was made using a token, the Nick_Name associate to the token
64+
-- is assigned to the id attribute of the user
65+
if raw_user.Nick_Name then
66+
user["id"] = raw_user.Nick_Name
4167
end
4268

4369
-- Invert the array of roles into a hashy table for more optimized
4470
-- lookups (so we can just check if the key exists, rather than
4571
-- looping over each value).
72+
-- Moreover, in case that the user information have been retrieved using a token validation,
73+
-- the roles associated with the token are stored in user ["roles"]
4674
if user["roles"] then
4775
user["roles"] = invert_table(user["roles"])
76+
elseif raw_user.Roles then
77+
user["roles"] = invert_table(raw_user.Roles)
4878
end
4979

5080
if user["created_at"] and user["created_at"]["$date"] then
@@ -103,14 +133,14 @@ function _M.get(api_key)
103133
return nil
104134
end
105135

106-
user = shared_cache:load(api_key)
136+
user = shared_cache:load(api_key["key_value"])
107137
if user then
108-
local_cache:set(api_key, user, 2)
138+
local_cache:set(api_key["key_value"], user, 2)
109139
else
110-
local_cache:set(api_key, EMPTY_DATA, 2)
140+
local_cache:set(api_key["key_value"], EMPTY_DATA, 2)
111141
end
112142

113143
return user
114144
end
115145

116-
return _M
146+
return _M

0 commit comments

Comments
 (0)