Skip to content

Commit d0e2992

Browse files
authored
Merge pull request #14 from holaplex/espi/gateway-user-id-header
[Hub Gateway] Optional X-User-Id, No opa, More Routes
2 parents 5fb4bec + c069148 commit d0e2992

File tree

11 files changed

+81
-744
lines changed

11 files changed

+81
-744
lines changed

charts/hub-gateway/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ type: application
1818
# This is the chart version. This version number should be incremented each time you make changes
1919
# to the chart and its templates, including the app version.
2020
# Versions are expected to follow Semantic Versioning (https://semver.org/)
21-
version: 0.1.9
21+
version: "0.2"
2222

2323
# This is the version number of the application being deployed. This version number should be
2424
# incremented each time you make changes to the application. Versions are not expected to
2525
# follow Semantic Versioning. They should reflect the version the application is using.
2626
# It is recommended to use it with quotes.
27-
appVersion: "0.1.9"
27+
appVersion: "0.2"
2828
sources:
2929
- https://github.com/holaplex/helm-charts
3030

charts/hub-gateway/plugins/hub-orgs.lua

Lines changed: 0 additions & 115 deletions
This file was deleted.

charts/hub-gateway/plugins/kratos.lua

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,63 @@
1414
-- See the License for the specific language governing permissions and
1515
-- limitations under the License.
1616
--
17-
18-
local core = require("apisix.core")
19-
local http = require("resty.http")
20-
local json = require("apisix.core.json")
17+
local core = require("apisix.core")
18+
local http = require("resty.http")
19+
local json = require("apisix.core.json")
2120

2221
local schema = {
2322
type = "object",
2423
properties = {
25-
host = {type = "string"},
24+
host = {
25+
type = "string"
26+
},
2627
ssl_verify = {
2728
type = "boolean",
28-
default = true,
29+
default = true
2930
},
3031
timeout = {
3132
type = "integer",
3233
minimum = 1,
3334
maximum = 60000,
3435
default = 3000,
35-
description = "timeout in milliseconds",
36+
description = "timeout in milliseconds"
37+
},
38+
keepalive = {
39+
type = "boolean",
40+
default = true
41+
},
42+
keepalive_timeout = {
43+
type = "integer",
44+
minimum = 1000,
45+
default = 60000
46+
},
47+
keepalive_pool = {
48+
type = "integer",
49+
minimum = 1,
50+
default = 5
51+
},
52+
expose_user_data = {
53+
type = "boolean",
54+
default = false
55+
},
56+
expose_user_id = {
57+
type = "boolean",
58+
default = false
59+
},
60+
session_cookie_name = {
61+
type = "string"
3662
},
37-
keepalive = {type = "boolean", default = true},
38-
keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
39-
keepalive_pool = {type = "integer", minimum = 1, default = 5},
40-
expose_user_data = {type = "boolean", default = false},
41-
expose_user_id = {type = "boolean", default = false},
42-
session_cookie_name = {type = "string"},
43-
redirect_unauthorized = {type = "boolean", default = false},
44-
redirect_uri = {type = "string"},
4563
},
4664
required = {"host"}
4765
}
4866

49-
5067
local _M = {
5168
version = 0.1,
5269
priority = 1030,
5370
name = "kratos",
54-
schema = schema,
71+
schema = schema
5572
}
5673

57-
5874
function _M.check_schema(conf)
5975
return core.schema.check(schema, conf)
6076
end
@@ -64,46 +80,37 @@ local function build_json_error(code, status, reason)
6480
core.response.set_header(ctx, "content", "application/json")
6581
local res = {
6682
error = {
67-
code = code,
68-
status = status,
69-
reason = reason
83+
code = code,
84+
status = status,
85+
reason = reason
7086
}
71-
}
87+
}
7288
return json.encode(res)
7389
end
7490

7591
function _M.access(conf, ctx)
7692
local ret_code
7793
local headers = core.request.headers()
7894
local method_name = ngx.req.get_method()
79-
80-
if method_name == "GET" and conf.redirect_unauthorized then
81-
ret_code = 301
82-
else
83-
ret_code = 401
84-
end
8595

8696
local session_cookie_name = string.lower(conf.session_cookie_name or "ory_kratos_session")
8797
local cookie_header = string.lower("cookie_" .. session_cookie_name)
8898
local cookie_value = ngx.var[cookie_header]
89-
99+
90100
-- Try to get session token from cookie header and $session_cookie_name
91101
local session_token = headers[session_cookie_name] or cookie_value
92102

93103
if not session_token then
94-
local res = build_json_error(ret_code, "Unauthorized", "Missing " .. session_cookie_name .. " header or cookie")
95-
if ret_code == 301 then
96-
core.response.set_header("Location", conf.redirect_uri)
97-
end
98-
return ret_code, res
104+
local res = build_json_error(ret_code, "Unauthorized", "Missing " .. session_cookie_name .. " header or cookie")
105+
return
99106
end
100107

101-
local kratos_cookie = session_cookie_name .. "=" .. session_token
102-
108+
local kratos_cookie = session_cookie_name .. "=" .. session_token
109+
103110
local params = {
104111
method = "POST",
105112
headers = {
106-
["Cookie"] = kratos_cookie,
113+
["Cookie"] = kratos_cookie
107114
},
108115
keepalive = conf.keepalive,
109116
ssl_verify = conf.ssl_verify
@@ -122,44 +129,38 @@ function _M.access(conf, ctx)
122129

123130
-- block by default when user is not found
124131
if not res then
125-
return 403, res.body
132+
return
126133
end
127134

128135
-- parse the user data
129136
local data, err = json.decode(res.body)
130137
if not data then
131-
return 503, res.body
138+
return
132139
end
133140

134141
-- block if user id is not found
135142
if not data.id then
136-
local reason = res.body
137-
core.log.error(reason)
138-
if ret_code == 301 then
139-
core.response.set_header("Location", conf.redirect_uri)
140-
end
141-
142-
return ret_code, reason
143+
return
143144
end
144145

145146
-- Expose user data response on $kratos_user_data variable
146147
if conf.expose_user_data then
147148
local user_data = ngx.encode_base64(res.body)
148149
if not user_data then
149-
return 503, res.body
150+
return
150151
end
151152
core.ctx.register_var("kratos_user_data", function(ctx)
152-
return user_data
153+
return user_data
153154
end)
154155
end
155156

156157
-- Expose user id on $kratos_user_id variable
157158
if conf.expose_user_id then
158-
core.request.set_header(ctx, "x-user-id", data.identity.id)
159-
core.response.set_header("x-user-id", data.identity.id)
160-
core.ctx.register_var("kratos_user_id", function(ctx)
161-
return data.identity.id
162-
end)
159+
core.request.set_header(ctx, "x-user-id", data.identity.id)
160+
core.response.set_header("x-user-id", data.identity.id)
161+
core.ctx.register_var("kratos_user_id", function(ctx)
162+
return data.identity.id
163+
end)
163164
end
164165
end
165166

0 commit comments

Comments
 (0)