From 9b4e37c8fdbe6d3446866772282d67d7b00bd687 Mon Sep 17 00:00:00 2001 From: Bruce Date: Wed, 11 May 2022 14:16:32 +0800 Subject: [PATCH] feat: dynamicly add query params func --- server-v2/api/studio/etc/studio-api.yaml | 2 +- server-v2/api/studio/pkg/auth/authorize.go | 8 ++------ server-v2/api/studio/pkg/utils/http.go | 9 +++++++++ server-v2/api/studio/studio.go | 1 - server-v2/go.mod | 2 ++ server-v2/go.sum | 4 ++++ 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/server-v2/api/studio/etc/studio-api.yaml b/server-v2/api/studio/etc/studio-api.yaml index 002e4cd9..6423c0ab 100644 --- a/server-v2/api/studio/etc/studio-api.yaml +++ b/server-v2/api/studio/etc/studio-api.yaml @@ -6,6 +6,6 @@ Debug: Enable: false Auth: AccessSecret: "login_secret" - AccessExpire: 7200 + AccessExpire: 1800 File: UploadDir: "./upload/" \ No newline at end of file diff --git a/server-v2/api/studio/pkg/auth/authorize.go b/server-v2/api/studio/pkg/auth/authorize.go index 6513a4df..21e86ce4 100644 --- a/server-v2/api/studio/pkg/auth/authorize.go +++ b/server-v2/api/studio/pkg/auth/authorize.go @@ -116,9 +116,7 @@ func AuthMiddlewareWithConfig(config *config.Config) rest.Middleware { MaxAge: 1800, } - query := r.URL.Query() - query.Set("nebulaVersion", string(clientInfo.NebulaVersion)) - r.URL, _ = r.URL.Parse(r.URL.Path + "?" + query.Encode()) + utils.AddQueryParams(r, map[string]string{"nebulaVersion": string(clientInfo.NebulaVersion)}) // w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Set-Cookie", token.String()) @@ -127,9 +125,7 @@ func AuthMiddlewareWithConfig(config *config.Config) rest.Middleware { nsidCookie, err := r.Cookie("nsid") if err == nil { - query := r.URL.Query() - query.Set("nsid", nsidCookie.Value) - r.URL, _ = r.URL.Parse(r.URL.Path + "?" + query.Encode()) + utils.AddQueryParams(r, map[string]string{"nsid": nsidCookie.Value}) } w.Header().Set("Set-Cookie", utils.DisabledCookie("token").String()) diff --git a/server-v2/api/studio/pkg/utils/http.go b/server-v2/api/studio/pkg/utils/http.go index a3ae820d..0ad3b8cb 100644 --- a/server-v2/api/studio/pkg/utils/http.go +++ b/server-v2/api/studio/pkg/utils/http.go @@ -45,3 +45,12 @@ func DisabledCookie(name string) *http.Cookie { MaxAge: -1, } } + +// dynamicly add query params to the request +func AddQueryParams(r *http.Request, params map[string]string) { + query := r.URL.Query() + for k, v := range params { + query.Set(k, v) + } + r.URL, _ = r.URL.Parse(r.URL.Path + "?" + query.Encode()) +} diff --git a/server-v2/api/studio/studio.go b/server-v2/api/studio/studio.go index c455e1d9..77ad786e 100644 --- a/server-v2/api/studio/studio.go +++ b/server-v2/api/studio/studio.go @@ -37,7 +37,6 @@ func main() { defer server.Stop() // global middleware - // server.Use(auth.AuthMiddleware) server.Use(auth.AuthMiddlewareWithConfig(&c)) // api handlers diff --git a/server-v2/go.mod b/server-v2/go.mod index cf55c8de..6cde3328 100644 --- a/server-v2/go.mod +++ b/server-v2/go.mod @@ -16,6 +16,7 @@ require ( ) require ( + github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/go-logr/logr v1.2.2 // indirect @@ -36,6 +37,7 @@ require ( github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.30.0 // indirect github.com/prometheus/procfs v0.7.3 // indirect + github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca github.com/spaolacci/murmur3 v1.1.0 // indirect go.opentelemetry.io/otel v1.3.0 // indirect go.opentelemetry.io/otel/exporters/jaeger v1.3.0 // indirect diff --git a/server-v2/go.sum b/server-v2/go.sum index 94fe5128..4abb9dd0 100644 --- a/server-v2/go.sum +++ b/server-v2/go.sum @@ -58,6 +58,8 @@ github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGn github.com/alicebob/miniredis/v2 v2.17.0/go.mod h1:gquAfGbzn92jvtrSC69+6zZnwSODVXVpYDRaGhWaL6I= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= +github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= +github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= @@ -360,6 +362,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca h1:NugYot0LIVPxTvN8n+Kvkn6TrbMyxQiuvKdEwFdR9vI= +github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca/go.mod h1:uugorj2VCxiV1x+LzaIdVa9b4S4qGAcH6cbhh4qVxOU= github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b h1:gQZ0qzfKHQIybLANtM3mBXNUtOfsCFXeTsnBqCsx1KM= github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=