Skip to content

Commit cdebcfe

Browse files
committed
feat: add plugins for checking cookie session
1 parent ea05eae commit cdebcfe

File tree

6 files changed

+177
-29
lines changed

6 files changed

+177
-29
lines changed

charts/hub-gateway/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ 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.4.0"
21+
version: "0.5.0"
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

charts/hub-gateway/plugins/kratos.lua

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,17 @@ local schema = {
6666

6767
local _M = {
6868
version = 0.1,
69-
priority = 1030,
70-
name = "kratos",
69+
priority = 2,
70+
name = "session",
7171
schema = schema
7272
}
7373

7474
function _M.check_schema(conf)
7575
return core.schema.check(schema, conf)
7676
end
7777

78-
local function build_json_error(code, status, reason)
79-
80-
core.response.set_header(ctx, "content", "application/json")
81-
local res = {
82-
error = {
83-
code = code,
84-
status = status,
85-
reason = reason
86-
}
87-
}
88-
return json.encode(res)
89-
end
90-
9178
function _M.access(conf, ctx)
92-
local ret_code
9379
local headers = core.request.headers()
94-
local method_name = ngx.req.get_method()
9580

9681
local session_cookie_name = string.lower(conf.session_cookie_name or "ory_kratos_session")
9782
local cookie_header = string.lower("cookie_" .. session_cookie_name)
@@ -101,7 +86,6 @@ function _M.access(conf, ctx)
10186
local session_token = headers[session_cookie_name] or cookie_value
10287

10388
if not session_token then
104-
local res = build_json_error(ret_code, "Unauthorized", "Missing " .. session_cookie_name .. " header or cookie")
10589
return
10690
end
10791

@@ -157,10 +141,10 @@ function _M.access(conf, ctx)
157141
-- Expose user id on $kratos_user_id variable
158142
-- Expose user email on $kratos_user_email variable
159143
if conf.expose_user_id then
160-
core.request.set_header(ctx, "x-user-id", data.identity.id)
161-
core.response.set_header("x-user-id", data.identity.id)
162-
core.request.set_header(ctx, "x-user-email", data.identity.traits.email)
163-
core.response.set_header("x-user-email", data.identity.traits.email)
144+
core.request.set_header(ctx, "X-USER-ID", data.identity.id)
145+
core.response.set_header("X-USER-ID", data.identity.id)
146+
core.request.set_header(ctx, "X-USER-EMAIL", data.identity.traits.email)
147+
core.response.set_header("X-USER-EMAIL", data.identity.traits.email)
164148
core.ctx.register_var("kratos_user_id", function(ctx)
165149
return data.identity.id
166150
end)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--
2+
-- Licensed to the Apache Software Foundation (ASF) under one or more
3+
-- contributor license agreements. See the NOTICE file distributed with
4+
-- this work for additional information regarding copyright ownership.
5+
-- The ASF licenses this file to You under the Apache License, Version 2.0
6+
-- (the "License"); you may not use this file except in compliance with
7+
-- the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing, software
12+
-- distributed under the License is distributed on an "AS IS" BASIS,
13+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
-- See the License for the specific language governing permissions and
15+
-- limitations under the License.
16+
--
17+
local core = require("apisix.core")
18+
local http = require("resty.http")
19+
local json = require("apisix.core.json")
20+
21+
local schema = {
22+
type = "object",
23+
properties = {}
24+
}
25+
26+
local _M = {
27+
version = 0.1,
28+
priority = 1,
29+
name = "session-json",
30+
schema = schema
31+
}
32+
33+
function _M.check_schema(conf)
34+
return core.schema.check(schema, conf)
35+
end
36+
37+
function _M.access(conf, ctx)
38+
local user_id = core.request.header(ctx, "X-USER-ID")
39+
local uri = ctx.var.uri
40+
41+
if not user_id then
42+
core.response.set_header("Content-Type", "application/json")
43+
44+
return 403, {
45+
message = "no valid session"
46+
}
47+
end
48+
end
49+
50+
return _M
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
--
2+
-- Licensed to the Apache Software Foundation (ASF) under one or more
3+
-- contributor license agreements. See the NOTICE file distributed with
4+
-- this work for additional information regarding copyright ownership.
5+
-- The ASF licenses this file to You under the Apache License, Version 2.0
6+
-- (the "License"); you may not use this file except in compliance with
7+
-- the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing, software
12+
-- distributed under the License is distributed on an "AS IS" BASIS,
13+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
-- See the License for the specific language governing permissions and
15+
-- limitations under the License.
16+
--
17+
local core = require("apisix.core")
18+
local http = require("resty.http")
19+
local json = require("apisix.core.json")
20+
21+
local schema = {
22+
type = "object",
23+
properties = {
24+
login_uri = {
25+
type = "string"
26+
},
27+
redirect_to = {
28+
type = "boolean",
29+
default = false
30+
}
31+
},
32+
require = {"login_uri"}
33+
}
34+
35+
local _M = {
36+
version = 0.1,
37+
priority = 1,
38+
name = "session-redirect",
39+
schema = schema
40+
}
41+
42+
function _M.check_schema(conf)
43+
return core.schema.check(schema, conf)
44+
end
45+
46+
function _M.access(conf, ctx)
47+
local redirect_to = conf.redirect_to
48+
local user_id = core.request.header(ctx, "X-USER-ID")
49+
local uri = ctx.var.uri
50+
local redirect_uri = conf.login_uri
51+
52+
if redirect_to then
53+
redirect_uri = redirect_uri .. "?return_to=" .. uri
54+
end
55+
56+
if not user_id then
57+
core.response.set_header("Location", redirect_uri)
58+
59+
return 302, "Unauthorized please login"
60+
end
61+
end
62+
63+
return _M

charts/hub-gateway/templates/apisixroute.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{{- $namespace := .Values.hubNamespace -}}
33
{{- $domain := .Values.domain -}}
44
{{- $sessionCookie := .Values.sessionCookieName -}}
5+
{{- $loginUri := .Values.loginUri -}}
56
{{- with .Values.routes }}
67
{{- range . }}
78
apiVersion: apisix.apache.org/v2
@@ -26,7 +27,8 @@ spec:
2627
methods:
2728
{{- .methods | toYaml | nindent 8 }}
2829
plugins:
29-
{{- if .setUserHeader }}
30+
{{- with .kratos }}
31+
{{- if .enabled | default false }}
3032
- name: kratos
3133
enable: true
3234
config:
@@ -35,6 +37,16 @@ spec:
3537
expose_user_id: true
3638
session_cookie_name: {{ $sessionCookie }}
3739
{{- end }}
40+
{{- end }}
41+
{{- with .sessionRedirect }}
42+
{{- if .enabled }}
43+
- name: session-redirect
44+
enable: true
45+
config:
46+
login_uri: {{ $loginUri }}
47+
redirect_to: {{ .redirectTo | default false }}
48+
{{- end }}
49+
{{- end }}
3850
{{- if .regexUri }}
3951
- name: proxy-rewrite
4052
enable: true

charts/hub-gateway/values.yaml

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ hubNamespace: default
22
domain: 127.0.0.1.nip.io
33
port: 9080
44
sessionCookieName: "hub_session"
5+
loginUri: "http://hub.127.0.0.1.nip.io:9080/login"
56

67
routes:
78
- name: api
@@ -13,7 +14,8 @@ routes:
1314
methods:
1415
- POST
1516
- OPTIONS
16-
setUserHeader: true
17+
kratos:
18+
enabled: true
1719
regexUri:
1820
- "/graphql"
1921
- "/"
@@ -27,7 +29,8 @@ routes:
2729
methods:
2830
- POST
2931
- OPTIONS
30-
setUserHeader: true
32+
kratos:
33+
enabled: true
3134
regexUri:
3235
- "/graphql"
3336
- "/"
@@ -41,7 +44,10 @@ routes:
4144
methods:
4245
- POST
4346
- OPTIONS
44-
setUserHeader: true
47+
kratos:
48+
enabled: true
49+
sessionJson:
50+
enabled: true
4551
regexUri:
4652
- "/graphql"
4753
- "/"
@@ -55,13 +61,19 @@ routes:
5561
- /browser/organizations/*
5662
methods:
5763
- POST
58-
setUserHeader: true
64+
kratos:
65+
enabled: true
66+
sessionRedirect:
67+
enabled: true
5968

6069
- name: ui-private
6170
subdomain: hub
6271
serviceName: hub
6372
servicePort: 80
64-
setUserHeader: true
73+
kratos:
74+
enabled: true
75+
sessionRedirect:
76+
enabled: true
6577
methods:
6678
- GET
6779
paths:
@@ -70,12 +82,27 @@ routes:
7082
- /webhooks/*
7183
- /members
7284
- /members/*
85+
- /organizations
7386
- /organizations/new
7487
- /projects
7588
- /projects/*
7689
- /treasuries
7790
- /treasuries/*
7891

92+
- name: ui-private-invite
93+
subdomain: hub
94+
serviceName: hub
95+
servicePort: 80
96+
kratos:
97+
enabled: true
98+
sessionRedirect:
99+
enabled: true
100+
redirectTo: true
101+
methods:
102+
- GET
103+
paths:
104+
- /invites/*
105+
79106
- name: ui-public
80107
subdomain: hub
81108
serviceName: hub
@@ -103,6 +130,12 @@ apisixPlugins:
103130
servicePort: 80
104131
files:
105132
- plugins/kratos.lua
133+
sessionRedirect:
134+
files:
135+
- plugins/session-redirect.lua
136+
sessionJson:
137+
files:
138+
- plugins/session-json.lua
106139

107140
apisix:
108141
enabled: true
@@ -158,6 +191,8 @@ apisix:
158191

159192
plugins:
160193
- kratos
194+
- session-redirect
195+
- session-json
161196
- mocking
162197
- cors
163198
- redirect
@@ -175,6 +210,10 @@ apisix:
175210
mounts:
176211
- key: "kratos.lua"
177212
path: "/opts/custom_plugins/apisix/plugins/kratos.lua"
213+
- key: "session-redirect.lua"
214+
path: "/opts/custom_plugins/apisix/plugins/session-redirect.lua"
215+
- key: "session-json.lua"
216+
path: "/opts/custom_plugins/apisix/plugins/session-json.lua"
178217

179218
logs:
180219
enableAccessLog: true

0 commit comments

Comments
 (0)