Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new parameter for setting the deletion of queryParamName from query #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .traefik.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
displayName: API Key Auth
displayName: API Key Auth RC
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"

Expand All @@ -13,6 +13,7 @@ testData:
removeHeadersOnSuccess: true
queryParam: true
queryParamName: token
removeQueryParamsOnSuccess: false
pathSegment: true
keys:
- 83AB3503-50AA-4B57-9386-B9F0BADF2013
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/Septima/traefik-api-key-auth
module github.com/realCity/traefik-api-key-auth

go 1.19
78 changes: 42 additions & 36 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -38,6 +39,7 @@ func CreateConfig() *Config {
QueryParam: true,
QueryParamName: "token",
PathSegment: true,
RemoveQueryParamsOnSuccess:true,
Keys: make([]string, 0),
RemoveHeadersOnSuccess: true,
InternalForwardHeaderName: "",
Expand All @@ -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) {
Expand All @@ -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
}

Expand Down Expand Up @@ -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 != "" {
qs.Del(ka.queryParamName)
if ka.removeQueryParamsOnSuccess{
qs.Del(ka.queryParamName)
}
req.URL.RawQuery = qs.Encode()
ka.ok(rw, req, matchedKey)
return
Expand Down