Skip to content

Commit f76b6bc

Browse files
authored
use formal JWT validation (#34)
Signed-off-by: Tiger Wang <[email protected]>
1 parent 18a4baf commit f76b6bc

File tree

7 files changed

+97
-104
lines changed

7 files changed

+97
-104
lines changed

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
# either 'goreleaser' (default) or 'goreleaser-pro'
3939
distribution: goreleaser
4040
version: latest
41-
args: release --rm-dist
41+
args: release --clean
4242
env:
4343
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4444
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution

common/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package common
22

3-
const Version = "0.4.3"
3+
const Version = "0.4.4"

go.mod

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/IceWhaleTech/CasaOS-Gateway
33
go 1.20
44

55
require (
6-
github.com/IceWhaleTech/CasaOS-Common v0.4.3
6+
github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha2
77
github.com/gin-contrib/gzip v0.0.6
88
github.com/gin-gonic/gin v1.9.0
99
github.com/spf13/viper v1.15.0
@@ -12,13 +12,13 @@ require (
1212
)
1313

1414
require (
15-
github.com/bytedance/sonic v1.8.0 // indirect
15+
github.com/bytedance/sonic v1.8.7 // indirect
1616
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
1717
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
1818
github.com/godbus/dbus/v5 v5.1.0 // indirect
19-
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
19+
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
2020
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
21-
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
21+
golang.org/x/arch v0.3.0 // indirect
2222
)
2323

2424
require (
@@ -33,7 +33,7 @@ require (
3333
github.com/google/go-cmp v0.5.9 // indirect
3434
github.com/hashicorp/hcl v1.0.0 // indirect
3535
github.com/json-iterator/go v1.1.12 // indirect
36-
github.com/leodido/go-urn v1.2.2 // indirect
36+
github.com/leodido/go-urn v1.2.3 // indirect
3737
github.com/magiconair/properties v1.8.7 // indirect
3838
github.com/mattn/go-isatty v0.0.18 // indirect
3939
github.com/mitchellh/mapstructure v1.5.0 // indirect
@@ -49,12 +49,12 @@ require (
4949
github.com/ugorji/go/codec v1.2.11 // indirect
5050
go.uber.org/atomic v1.10.0 // indirect
5151
go.uber.org/dig v1.16.1 // indirect
52-
go.uber.org/multierr v1.10.0 // indirect
52+
go.uber.org/multierr v1.11.0 // indirect
5353
go.uber.org/zap v1.24.0
54-
golang.org/x/crypto v0.7.0 // indirect
55-
golang.org/x/net v0.8.0 // indirect
56-
golang.org/x/sys v0.6.0 // indirect
57-
golang.org/x/text v0.8.0 // indirect
54+
golang.org/x/crypto v0.8.0 // indirect
55+
golang.org/x/net v0.9.0 // indirect
56+
golang.org/x/sys v0.7.0 // indirect
57+
golang.org/x/text v0.9.0 // indirect
5858
google.golang.org/protobuf v1.30.0 // indirect
5959
gopkg.in/ini.v1 v1.67.0 // indirect
6060
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect

go.sum

+22-19
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
3838
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
3939
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4040
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
41-
github.com/IceWhaleTech/CasaOS-Common v0.4.3 h1:sYsWYhP5EyksH6D0BDv8secIRGAKUZZDTIKqACdFZQ4=
42-
github.com/IceWhaleTech/CasaOS-Common v0.4.3/go.mod h1:zLzM1RhoBRDdW7DFlEoPq18OhbJYsvc8yFodW0bm/QI=
41+
github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha2 h1:EIuQbVpuU2QZVui+ApYCEt27qGCUaQhgHCdOeHmgAwc=
42+
github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha2/go.mod h1:iv6fJJs7IKv1tSrsbyA3Fq9xfeKNoaxhqA6IQUBeg9Q=
4343
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
4444
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
45-
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
46-
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
45+
github.com/bytedance/sonic v1.8.7 h1:d3sry5vGgVq/OpgozRUNP6xBsSo0mtNdwliApw+SAMQ=
46+
github.com/bytedance/sonic v1.8.7/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
4747
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
4848
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
4949
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
@@ -171,8 +171,9 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
171171
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
172172
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
173173
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
174-
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
175174
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
175+
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
176+
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
176177
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
177178
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
178179
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
@@ -183,8 +184,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
183184
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
184185
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
185186
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
186-
github.com/leodido/go-urn v1.2.2 h1:7z68G0FCGvDk646jz1AelTYNYWrTNm0bEcFAo147wt4=
187-
github.com/leodido/go-urn v1.2.2/go.mod h1:kUaIbLZWttglzwNuG0pgsh5vuV6u2YcGBYz1hIPjtOQ=
187+
github.com/leodido/go-urn v1.2.3 h1:6BE2vPT0lqoz3fmOesHZiaiFh7889ssCo2GMvLCfiuA=
188+
github.com/leodido/go-urn v1.2.3/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
188189
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
189190
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
190191
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
@@ -211,7 +212,6 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
211212
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
212213
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
213214
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
214-
github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef/go.mod h1:8AEUvGVi2uQ5b24BIhcr0GCcpd/RNAFWaN2CJFrWIIQ=
215215
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
216216
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
217217
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
@@ -260,13 +260,14 @@ go.uber.org/dig v1.16.1 h1:+alNIBsl0qfY0j6epRubp/9obgtrObRAc5aD+6jbWY8=
260260
go.uber.org/dig v1.16.1/go.mod h1:557JTAUZT5bUK0SvCwikmLPPtdQhfvLYtO5tJgQSbnk=
261261
go.uber.org/fx v1.19.2 h1:SyFgYQFr1Wl0AYstE8vyYIzP4bFz2URrScjwC4cwUvY=
262262
go.uber.org/fx v1.19.2/go.mod h1:43G1VcqSzbIv77y00p1DRAsyZS8WdzuYdhZXmEUkMyQ=
263-
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
264-
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
265-
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
263+
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
264+
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
265+
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
266266
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
267267
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
268-
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
269268
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
269+
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
270+
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
270271
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
271272
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
272273
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@@ -275,8 +276,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
275276
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
276277
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
277278
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
278-
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
279-
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
279+
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
280+
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
280281
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
281282
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
282283
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -342,8 +343,8 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v
342343
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
343344
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
344345
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
345-
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
346-
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
346+
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
347+
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
347348
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
348349
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
349350
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -400,9 +401,11 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w
400401
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
401402
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
402403
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
404+
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
403405
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
404-
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
405406
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
407+
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
408+
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
406409
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
407410
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
408411
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -412,8 +415,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
412415
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
413416
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
414417
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
415-
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
416-
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
418+
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
419+
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
417420
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
418421
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
419422
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

route/management_route.go

+50-44
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package route
22

33
import (
4+
"crypto/ecdsa"
45
"net/http"
56
"os"
67

8+
"github.com/IceWhaleTech/CasaOS-Common/external"
79
"github.com/IceWhaleTech/CasaOS-Common/middleware"
810
"github.com/IceWhaleTech/CasaOS-Common/model"
911
"github.com/IceWhaleTech/CasaOS-Common/utils/common_err"
@@ -64,59 +66,63 @@ func (m *ManagementRoute) buildV1RouteGroup(v1Group *gin.RouterGroup) {
6466
ctx.JSON(http.StatusOK, m.management.GetRoutes())
6567
})
6668

67-
v1GatewayGroup.POST("/routes", func(ctx *gin.Context) {
68-
var route *model.Route
69-
err := ctx.ShouldBindJSON(&route)
70-
if err != nil {
71-
ctx.JSON(http.StatusBadRequest, model.Result{
72-
Success: common_err.CLIENT_ERROR,
73-
Message: err.Error(),
74-
})
75-
return
76-
}
77-
78-
if err := m.management.CreateRoute(route); err != nil {
79-
ctx.JSON(http.StatusInternalServerError, model.Result{
80-
Success: common_err.SERVICE_ERROR,
81-
Message: err.Error(),
82-
})
83-
return
84-
}
85-
86-
ctx.Status(http.StatusCreated)
87-
})
69+
v1GatewayGroup.POST("/routes",
70+
jwt.ExceptLocalhost(func() (*ecdsa.PublicKey, error) { return external.GetPublicKey(m.management.State.GetRuntimePath()) }),
71+
func(ctx *gin.Context) {
72+
var route *model.Route
73+
err := ctx.ShouldBindJSON(&route)
74+
if err != nil {
75+
ctx.JSON(http.StatusBadRequest, model.Result{
76+
Success: common_err.CLIENT_ERROR,
77+
Message: err.Error(),
78+
})
79+
return
80+
}
81+
82+
if err := m.management.CreateRoute(route); err != nil {
83+
ctx.JSON(http.StatusInternalServerError, model.Result{
84+
Success: common_err.SERVICE_ERROR,
85+
Message: err.Error(),
86+
})
87+
return
88+
}
89+
90+
ctx.Status(http.StatusCreated)
91+
})
8892

89-
v1GatewayGroup.GET("/port", jwt.ExceptLocalhost(), func(ctx *gin.Context) {
93+
v1GatewayGroup.GET("/port", func(ctx *gin.Context) {
9094
ctx.JSON(http.StatusOK, model.Result{
9195
Success: common_err.SUCCESS,
9296
Message: common_err.GetMsg(common_err.SUCCESS),
9397
Data: m.management.GetGatewayPort(),
9498
})
9599
})
96100

97-
v1GatewayGroup.PUT("/port", jwt.ExceptLocalhost(), func(ctx *gin.Context) {
98-
var request *model.ChangePortRequest
99-
100-
if err := ctx.ShouldBindJSON(&request); err != nil {
101-
ctx.JSON(http.StatusBadRequest, model.Result{
102-
Success: common_err.CLIENT_ERROR,
103-
Message: err.Error(),
101+
v1GatewayGroup.PUT("/port",
102+
jwt.ExceptLocalhost(func() (*ecdsa.PublicKey, error) { return external.GetPublicKey(m.management.State.GetRuntimePath()) }),
103+
func(ctx *gin.Context) {
104+
var request *model.ChangePortRequest
105+
106+
if err := ctx.ShouldBindJSON(&request); err != nil {
107+
ctx.JSON(http.StatusBadRequest, model.Result{
108+
Success: common_err.CLIENT_ERROR,
109+
Message: err.Error(),
110+
})
111+
return
112+
}
113+
114+
if err := m.management.SetGatewayPort(request.Port); err != nil {
115+
ctx.JSON(http.StatusInternalServerError, model.Result{
116+
Success: common_err.SERVICE_ERROR,
117+
Message: err.Error(),
118+
})
119+
return
120+
}
121+
122+
ctx.JSON(http.StatusOK, model.Result{
123+
Success: common_err.SUCCESS,
124+
Message: common_err.GetMsg(common_err.SUCCESS),
104125
})
105-
return
106-
}
107-
108-
if err := m.management.SetGatewayPort(request.Port); err != nil {
109-
ctx.JSON(http.StatusInternalServerError, model.Result{
110-
Success: common_err.SERVICE_ERROR,
111-
Message: err.Error(),
112-
})
113-
return
114-
}
115-
116-
ctx.JSON(http.StatusOK, model.Result{
117-
Success: common_err.SUCCESS,
118-
Message: common_err.GetMsg(common_err.SUCCESS),
119126
})
120-
})
121127
}
122128
}

route/management_route_test.go

+7-24
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ import (
77
"net/http/httptest"
88
"os"
99
"testing"
10-
"time"
1110

1211
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
1312

1413
"github.com/IceWhaleTech/CasaOS-Common/model"
15-
"github.com/IceWhaleTech/CasaOS-Common/utils/jwt"
1614
"github.com/IceWhaleTech/CasaOS-Gateway/service"
1715
"github.com/gin-gonic/gin"
1816
"gotest.tools/v3/assert"
@@ -67,11 +65,11 @@ func TestCreateRoute(t *testing.T) {
6765
}
6866

6967
body, err := json.Marshal(route)
70-
if err != nil {
71-
t.Error(err)
72-
}
68+
assert.NilError(t, err)
7369

7470
req, _ := http.NewRequest(http.MethodPost, "/v1/gateway/routes", bytes.NewReader(body))
71+
req.RemoteAddr = "127.0.0.1:0"
72+
7573
w := httptest.NewRecorder()
7674
_router.ServeHTTP(w, req)
7775
assert.Equal(t, http.StatusCreated, w.Code)
@@ -86,11 +84,7 @@ func TestCreateRoute(t *testing.T) {
8684
decoder := json.NewDecoder(w.Body)
8785

8886
err = decoder.Decode(&routes)
89-
90-
if err != nil {
91-
t.Error(err)
92-
}
93-
87+
assert.NilError(t, err)
9488
assert.Equal(t, 1, len(routes))
9589
assert.Equal(t, route.Path, routes[0].Path)
9690
assert.Equal(t, route.Target, routes[0].Target)
@@ -114,17 +108,10 @@ func TestChangePort(t *testing.T) {
114108
}
115109

116110
body, err := json.Marshal(request)
117-
if err != nil {
118-
t.Error(err)
119-
}
120-
121-
token, err := jwt.GenerateToken("test", "test", 0, "casaos", 10*time.Second)
122-
if err != nil {
123-
t.Error(err)
124-
}
111+
assert.NilError(t, err)
125112

126113
req, _ := http.NewRequest(http.MethodPut, "/v1/gateway/port", bytes.NewReader(body))
127-
req.Header.Set("Authorization", token)
114+
req.RemoteAddr = "127.0.0.1:0"
128115

129116
w := httptest.NewRecorder()
130117
_router.ServeHTTP(w, req)
@@ -134,7 +121,6 @@ func TestChangePort(t *testing.T) {
134121

135122
// get
136123
req, _ = http.NewRequest(http.MethodGet, "/v1/gateway/port", nil)
137-
req.Header.Set("Authorization", token)
138124

139125
w = httptest.NewRecorder()
140126
_router.ServeHTTP(w, req)
@@ -145,9 +131,6 @@ func TestChangePort(t *testing.T) {
145131
decoder := json.NewDecoder(w.Body)
146132

147133
err = decoder.Decode(&result)
148-
if err != nil {
149-
t.Error(err)
150-
}
151-
134+
assert.NilError(t, err)
152135
assert.Equal(t, expectedPort, result.Data)
153136
}

0 commit comments

Comments
 (0)