Skip to content

Commit e171899

Browse files
authored
Merge pull request #31 from holaplex/mpw/hydra-plugin-user-id
update hydra plugin - expose user id header
2 parents fe6c889 + 1dcf77c commit e171899

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

charts/hub-gateway/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type: application
1919
# to the chart and its templates, including the app version.
2020
# Versions are expected to follow Semantic Versioning (https://semver.org/)
2121

22-
version: "0.7.2"
22+
version: "0.7.3"
2323

2424
# This is the version number of the application being deployed. This version number should be
2525
# incremented each time you make changes to the application. Versions are not expected to

charts/hub-gateway/plugins/oauth2.lua

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function _M.access(conf, ctx)
7373

7474
if not api_token then
7575
return 401, json.encode({
76-
message = "Authorization header not found"
76+
message = "Authorization header not found"
7777
})
7878
end
7979

@@ -126,6 +126,34 @@ function _M.access(conf, ctx)
126126
return data.client_id
127127
end)
128128
end
129+
130+
-- Get kratos user id from hydra client contacts
131+
local params = {
132+
method = "GET",
133+
headers = {
134+
["Accept"] = "application/json"
135+
},
136+
keepalive = conf.keepalive,
137+
ssl_verify = conf.ssl_verify
138+
}
139+
140+
if conf.keepalive then
141+
params.keepalive_timeout = conf.keepalive_timeout
142+
params.keepalive_pool = conf.keepalive_pool
143+
end
144+
145+
local endpoint = conf.host .. "/admin/clients/" .. data.client_id
146+
147+
local res, err = httpc:request_uri(endpoint, params)
148+
149+
local data, err = json.decode(res.body)
150+
if not data then
151+
return 401, err
152+
end
153+
154+
core.request.set_header(ctx, "X-USER-ID", data.contacts[1])
155+
core.response.set_header("X-USER-ID", data.contacts[1])
156+
129157
end
130158

131159
return _M

charts/hub-gateway/plugins/session.lua

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ function _M.check_schema(conf)
7676
end
7777

7878
function _M.access(conf, ctx)
79-
local headers = core.request.headers()
80-
8179
local session_cookie_name = string.lower(conf.session_cookie_name or "ory_kratos_session")
8280
local cookie_header = string.lower("cookie_" .. session_cookie_name)
8381
local cookie_value = ngx.var[cookie_header]
8482

85-
-- Try to get session token from cookie header and $session_cookie_name
86-
local session_token = headers[session_cookie_name] or cookie_value
83+
-- Try to get session token from $session_cookie_name cookie
84+
local session_token = cookie_value
8785

8886
if not session_token then
89-
return
87+
return 401, json.encode({
88+
message = "session_cookie not found not found"
89+
})
9090
end
9191

9292
local kratos_cookie = session_cookie_name .. "=" .. session_token
@@ -113,25 +113,34 @@ function _M.access(conf, ctx)
113113

114114
-- block by default when user is not found
115115
if not res then
116-
return
116+
return 401, json.encode({
117+
message = err
118+
})
117119
end
118120

119121
-- parse the user data
120122
local data, err = json.decode(res.body)
121123
if not data then
122-
return
124+
return 401, json.encode({
125+
message = err
126+
})
127+
123128
end
124129

125130
-- block if user id is not found
126131
if not data.id then
127-
return
132+
return 401, json.encode({
133+
message = err
134+
})
128135
end
129136

130137
-- Expose user data response on $kratos_user_data variable
131138
if conf.expose_user_data then
132139
local user_data = ngx.encode_base64(res.body)
133140
if not user_data then
134-
return
141+
return 401, json.encode({
142+
message = "Error while reading user_data"
143+
})
135144
end
136145
core.ctx.register_var("kratos_user_data", function(ctx)
137146
return user_data

0 commit comments

Comments
 (0)