From 5e7bf3639f7634718fbc13503b9de67fb886a2ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Don=C3=A1t=20Hajszter?= Date: Thu, 8 Feb 2024 09:43:06 +0100 Subject: [PATCH 1/3] new parameter for setting the deletion of queryParamName from query --- .traefik.yml | 1 + plugin.go | 76 ++++++++++++++++++++++++++++------------------------ 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/.traefik.yml b/.traefik.yml index adced66..13789d7 100644 --- a/.traefik.yml +++ b/.traefik.yml @@ -13,6 +13,7 @@ testData: removeHeadersOnSuccess: true queryParam: true queryParamName: token + removeQueryParamsOnSuccess: false pathSegment: true keys: - 83AB3503-50AA-4B57-9386-B9F0BADF2013 diff --git a/plugin.go b/plugin.go index fd6e027..6fc6609 100644 --- a/plugin.go +++ b/plugin.go @@ -11,17 +11,18 @@ import ( ) type Config struct { - AuthenticationHeader bool `json:"authenticationHeader,omitempty"` - AuthenticationHeaderName string `json:"headerName,omitempty"` - BearerHeader bool `json:"bearerHeader,omitempty"` - BearerHeaderName string `json:"bearerHeaderName,omitempty"` - QueryParam bool `json:"queryParam,omitempty"` - QueryParamName string `json:"queryParamName,omitempty"` - PathSegment bool `json:"pathSegment,omitempty"` - Keys []string `json:"keys,omitempty"` - RemoveHeadersOnSuccess bool `json:"removeHeadersOnSuccess,omitempty"` - InternalForwardHeaderName string `json:"internalForwardHeaderName,omitempty"` - InternalErrorRoute string `json:"internalErrorRoute,omitempty"` + AuthenticationHeader bool `json:"authenticationHeader,omitempty"` + AuthenticationHeaderName string `json:"headerName,omitempty"` + BearerHeader bool `json:"bearerHeader,omitempty"` + BearerHeaderName string `json:"bearerHeaderName,omitempty"` + QueryParam bool `json:"queryParam,omitempty"` + QueryParamName string `json:"queryParamName,omitempty"` + PathSegment bool `json:"pathSegment,omitempty"` + removeQueryParamsOnSuccess bool `json:"removeQueryParamsOnSuccess,omitempty"` + Keys []string `json:"keys,omitempty"` + RemoveHeadersOnSuccess bool `json:"removeHeadersOnSuccess,omitempty"` + InternalForwardHeaderName string `json:"internalForwardHeaderName,omitempty"` + InternalErrorRoute string `json:"internalErrorRoute,omitempty"` } type Response struct { @@ -38,6 +39,7 @@ func CreateConfig() *Config { QueryParam: true, QueryParamName: "token", PathSegment: true, + removeQueryParamsOnSuccess:true, Keys: make([]string, 0), RemoveHeadersOnSuccess: true, InternalForwardHeaderName: "", @@ -46,18 +48,19 @@ func CreateConfig() *Config { } type KeyAuth struct { - next http.Handler - authenticationHeader bool - authenticationHeaderName string - bearerHeader bool - bearerHeaderName string - queryParam bool - queryParamName string - pathSegment bool - keys []string - removeHeadersOnSuccess bool - internalForwardHeaderName string - internalErrorRoute string + next http.Handler + authenticationHeader bool + authenticationHeaderName string + bearerHeader bool + bearerHeaderName string + queryParam bool + queryParamName string + pathSegment bool + removeQueryParamsOnSuccess bool + keys []string + removeHeadersOnSuccess bool + internalForwardHeaderName string + internalErrorRoute string } func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) { @@ -74,18 +77,19 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h } return &KeyAuth{ - next: next, - authenticationHeader: config.AuthenticationHeader, - authenticationHeaderName: config.AuthenticationHeaderName, - bearerHeader: config.BearerHeader, - bearerHeaderName: config.BearerHeaderName, - queryParam: config.QueryParam, - queryParamName: config.QueryParamName, - pathSegment: config.PathSegment, - keys: config.Keys, - removeHeadersOnSuccess: config.RemoveHeadersOnSuccess, - internalForwardHeaderName: config.InternalForwardHeaderName, - internalErrorRoute: config.InternalErrorRoute, + next: next, + authenticationHeader: config.AuthenticationHeader, + authenticationHeaderName: config.AuthenticationHeaderName, + bearerHeader: config.BearerHeader, + bearerHeaderName: config.BearerHeaderName, + queryParam: config.QueryParam, + queryParamName: config.QueryParamName, + pathSegment: config.PathSegment, + removeQueryParamsOnSuccess: config.removeQueryParamsOnSuccess, + keys: config.Keys, + removeHeadersOnSuccess: config.RemoveHeadersOnSuccess, + internalForwardHeaderName: config.InternalForwardHeaderName, + internalErrorRoute: config.InternalErrorRoute, }, nil } @@ -166,7 +170,9 @@ func (ka *KeyAuth) ServeHTTP(rw http.ResponseWriter, req *http.Request) { var qs = req.URL.Query() var matchedKey = contains(qs.Get(ka.queryParamName), ka.keys, true) if matchedKey != "" { + if ka.removeQueryParamsOnSuccess{ qs.Del(ka.queryParamName) + } req.URL.RawQuery = qs.Encode() ka.ok(rw, req, matchedKey) return From 11443fe7380c45b386f1328a797452e783f0a982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Don=C3=A1t=20Hajszter?= Date: Tue, 5 Mar 2024 18:40:50 +0100 Subject: [PATCH 2/3] changing Septima for realCity --- .traefik.yml | 2 +- go.mod | 2 +- plugin.go | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.traefik.yml b/.traefik.yml index 13789d7..6e5c095 100644 --- a/.traefik.yml +++ b/.traefik.yml @@ -1,7 +1,7 @@ displayName: API Key Auth type: middleware -import: github.com/Septima/traefik-api-key-auth +import: github.com/realCity/traefik-api-key-auth summary: "Authorise API requests with an API Key" diff --git a/go.mod b/go.mod index 72e83c4..5b2f1a1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/Septima/traefik-api-key-auth +module github.com/realCity/traefik-api-key-auth go 1.19 diff --git a/plugin.go b/plugin.go index 6fc6609..8539dc8 100644 --- a/plugin.go +++ b/plugin.go @@ -18,7 +18,7 @@ type Config struct { QueryParam bool `json:"queryParam,omitempty"` QueryParamName string `json:"queryParamName,omitempty"` PathSegment bool `json:"pathSegment,omitempty"` - removeQueryParamsOnSuccess bool `json:"removeQueryParamsOnSuccess,omitempty"` + RemoveQueryParamsOnSuccess bool `json:"removeQueryParamsOnSuccess,omitempty"` Keys []string `json:"keys,omitempty"` RemoveHeadersOnSuccess bool `json:"removeHeadersOnSuccess,omitempty"` InternalForwardHeaderName string `json:"internalForwardHeaderName,omitempty"` @@ -39,7 +39,7 @@ func CreateConfig() *Config { QueryParam: true, QueryParamName: "token", PathSegment: true, - removeQueryParamsOnSuccess:true, + RemoveQueryParamsOnSuccess:true, Keys: make([]string, 0), RemoveHeadersOnSuccess: true, InternalForwardHeaderName: "", @@ -85,7 +85,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h queryParam: config.QueryParam, queryParamName: config.QueryParamName, pathSegment: config.PathSegment, - removeQueryParamsOnSuccess: config.removeQueryParamsOnSuccess, + removeQueryParamsOnSuccess: config.RemoveQueryParamsOnSuccess, keys: config.Keys, removeHeadersOnSuccess: config.RemoveHeadersOnSuccess, internalForwardHeaderName: config.InternalForwardHeaderName, @@ -171,7 +171,7 @@ func (ka *KeyAuth) ServeHTTP(rw http.ResponseWriter, req *http.Request) { var matchedKey = contains(qs.Get(ka.queryParamName), ka.keys, true) if matchedKey != "" { if ka.removeQueryParamsOnSuccess{ - qs.Del(ka.queryParamName) + qs.Del(ka.queryParamName) } req.URL.RawQuery = qs.Encode() ka.ok(rw, req, matchedKey) From cac04ccb4122479c388b036a336c5a585f51734d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Don=C3=A1t=20Hajszter?= Date: Wed, 6 Mar 2024 14:27:28 +0100 Subject: [PATCH 3/3] new diplay name --- .traefik.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.traefik.yml b/.traefik.yml index 6e5c095..e54e62e 100644 --- a/.traefik.yml +++ b/.traefik.yml @@ -1,4 +1,4 @@ -displayName: API Key Auth +displayName: API Key Auth RC type: middleware import: github.com/realCity/traefik-api-key-auth