Skip to content

Commit 030072a

Browse files
committed
Add new forwardAuth option
Signed-off-by: Georges-Etienne Legendre <legege@legege.com>
1 parent a3a9709 commit 030072a

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Start with command
88
```yaml
99
command:
1010
- "--experimental.plugins.jwt-validation-middleware.modulename=github.com/legege/jwt-validation-middleware"
11-
- "--experimental.plugins.jwt-validation-middleware.version=v0.2.0"
11+
- "--experimental.plugins.jwt-validation-middleware.version=v0.2.1"
1212
```
1313
1414
Activate plugin in your config
@@ -26,6 +26,7 @@ http:
2626
X-Custom-Header2: name
2727
authQueryParam: authToken
2828
authCookieName: authToken
29+
forwardAuth: false
2930
```
3031
3132
Use as docker-compose label

jwt.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Config struct {
2121
PayloadHeaders map[string]string `json:"payloadHeaders,omitempty"`
2222
AuthQueryParam string `json:"authQueryParam,omitempty"`
2323
AuthCookieName string `json:"authCookieName,omitempty"`
24+
ForwardAuth bool `json:"forwardAuth,omitempty"`
2425
}
2526

2627
func CreateConfig() *Config {
@@ -29,6 +30,7 @@ func CreateConfig() *Config {
2930
Optional: false,
3031
AuthQueryParam: "authToken",
3132
AuthCookieName: "authToken",
33+
ForwardAuth: false,
3234
}
3335
}
3436

@@ -40,6 +42,7 @@ type JWT struct {
4042
payloadHeaders map[string]string
4143
authQueryParam string
4244
authCookieName string
45+
forwardAuth bool
4346
}
4447

4548
type Token struct {
@@ -57,6 +60,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
5760
payloadHeaders: config.PayloadHeaders,
5861
authQueryParam: config.AuthQueryParam,
5962
authCookieName: config.AuthCookieName,
63+
forwardAuth: config.ForwardAuth,
6064
}, nil
6165
}
6266

@@ -159,12 +163,28 @@ func (j *JWT) extractTokenFromCookie(request *http.Request) string {
159163
if err != nil {
160164
return ""
161165
}
166+
if !j.forwardAuth {
167+
cookies := request.Cookies()
168+
request.Header.Del("Cookie")
169+
for _, c := range cookies {
170+
if c.Name != j.authCookieName {
171+
request.AddCookie(c)
172+
}
173+
}
174+
}
162175
return cookie.Value
163176
}
164177

165178
func (j *JWT) extractTokenFromQuery(request *http.Request) string {
166179
if request.URL.Query().Has(j.authQueryParam) {
167-
return request.URL.Query().Get(j.authQueryParam)
180+
token := request.URL.Query().Get(j.authQueryParam)
181+
if !j.forwardAuth {
182+
qry := request.URL.Query()
183+
qry.Del(j.authQueryParam)
184+
request.URL.RawQuery = qry.Encode()
185+
request.RequestURI = request.URL.RequestURI()
186+
}
187+
return token
168188
}
169189
return ""
170190
}
@@ -178,5 +198,9 @@ func (j *JWT) extractTokenFromHeader(request *http.Request) string {
178198
if !strings.HasPrefix(auth, "Bearer ") {
179199
return ""
180200
}
201+
202+
if !j.forwardAuth {
203+
request.Header.Del("Authorization")
204+
}
181205
return auth[7:]
182206
}

0 commit comments

Comments
 (0)