Skip to content
This repository was archived by the owner on Dec 7, 2020. It is now read-only.

Commit 82df285

Browse files
authored
Merge pull request #247 from gambol99/token_header
Upstream Token Header
2 parents 6782490 + 2827b71 commit 82df285

File tree

6 files changed

+55
-6
lines changed

6 files changed

+55
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ FEATURES
2626
* added the --skip-client-id option to permit skipping the verification of the auduence against client in token [#PR236](https://github.com/gambol99/keycloak-proxy/pull/236)
2727
* updated the base image to apline 3.6 in commit [0fdebaf821](https://github.com/gambol99/keycloak-proxy/pull/236/commits/0fdebaf8215e9480896f01ec7ab2ef7caa242da1)
2828
* moved to use zap for the logging [#PR237](https://github.com/gambol99/keycloak-proxy/pull/237)
29+
* making the X-Auth-Token optional in the upstream headers via the --enable-token-header [#PR247](https://github.com/gambol99/keycloak-proxy/pull/247)
2930

3031
BREAKING CHANGES:
3132
* the proxy no longer uses prefixes for resources, if you wish to use wildcard urls you need

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ USAGE:
3333
keycloak-proxy [options]
3434

3535
VERSION:
36-
v2.1.0 (git+sha: 960c2e5-dirty, built: 25/04/2017)
36+
v2.1.0-rc2 (git+sha: 6782490-dirty, built: 06-07-2017)
3737

3838
AUTHOR:
3939
@@ -55,7 +55,8 @@ GLOBAL OPTIONS:
5555
--upstream-url value url for the upstream endpoint you wish to proxy [$PROXY_UPSTREAM_URL]
5656
--resources value list of resources 'uri=/admin|methods=GET,PUT|roles=role1,role2'
5757
--headers value custom headers to the upstream request, key=value
58-
--enable-encrypted-token indicates you want the access token encrypted (default: false)
58+
--enable-token-header enables the token authentication header X-Auth-Token to upstream (default: true)
59+
--enable-encrypted-token enable encryption for the access tokens (default: false)
5960
--enable-logging enable http logging of the requests (default: false)
6061
--enable-json-logging switch on json logging rather than text (default: false)
6162
--enable-forwarding enables the forwarding proxy mode, signing outbound request (default: false)
@@ -85,6 +86,7 @@ GLOBAL OPTIONS:
8586
--tls-ca-key value path the ca private key, used by the forward signing proxy
8687
--tls-client-certificate value path to the client certificate for outbound connections in reverse and forwarding proxy modes
8788
--skip-upstream-tls-verify skip the verification of any upstream TLS (default: true)
89+
--skip-client-id skip the check on the client token (default: false)
8890
--cors-origins value origins to add to the CORE origins control (Access-Control-Allow-Origin)
8991
--cors-methods value methods permitted in the access control (Access-Control-Allow-Methods)
9092
--cors-headers value set of headers to add to the CORS access control (Access-Control-Allow-Headers)
@@ -107,6 +109,7 @@ GLOBAL OPTIONS:
107109
--forwarding-username value username to use when logging into the openid provider
108110
--forwarding-password value password to use when logging into the openid provider
109111
--forwarding-domains value list of domains which should be signed; everything else is relayed unsigned
112+
--disable-all-logging disables all logging to stdout and stderr (default: false)
110113
--help, -h show help
111114
--version, -v print the version
112115
```

config.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func newDefaultConfig() *Config {
3434
UpstreamTimeout: time.Duration(10) * time.Second,
3535
UpstreamKeepaliveTimeout: time.Duration(10) * time.Second,
3636
EnableAuthorizationHeader: true,
37+
EnableTokenHeader: true,
3738
CookieAccessName: "kc-access",
3839
CookieRefreshName: "kc-state",
3940
SecureCookie: true,

doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ type Config struct {
130130
// Headers permits adding customs headers across the board
131131
Headers map[string]string `json:"headers" yaml:"headers" usage:"custom headers to the upstream request, key=value"`
132132

133+
// EnableTokenHeader adds the JWT token to the upstream authentication headers
134+
EnableTokenHeader bool `json:"enable-token-header" yaml:"enable-token-header" usage:"enables the token authentication header X-Auth-Token to upstream"`
133135
// EnableEncryptedToken indicates the access token should be encoded
134136
EnableEncryptedToken bool `json:"enable-encrypted-token" yaml:"enable-encrypted-token" usage:"enable encryption for the access tokens"`
135137
// EnableLogging indicates if we should log all the requests

middleware.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ import (
3434
const (
3535
// normalizeFlags is the options to purell
3636
normalizeFlags purell.NormalizationFlags = purell.FlagRemoveDotSegments | purell.FlagRemoveDuplicateSlashes
37-
// httpResponseName is the name of the http response hanlder
38-
httpResponseName = "http.response"
3937
)
4038

4139
// entrypointMiddleware is custom filtering for incoming requests
@@ -328,14 +326,17 @@ func (r *oauthProxy) headersMiddleware(custom []string) func(http.Handler) http.
328326
req.Header.Set("X-Auth-ExpiresIn", user.expiresAt.String())
329327
req.Header.Set("X-Auth-Roles", strings.Join(user.roles, ","))
330328
req.Header.Set("X-Auth-Subject", user.id)
331-
req.Header.Set("X-Auth-Token", user.token.Encode())
332329
req.Header.Set("X-Auth-Userid", user.name)
333330
req.Header.Set("X-Auth-Username", user.name)
334-
331+
// should we add the token header?
332+
if r.config.EnableTokenHeader {
333+
req.Header.Set("X-Auth-Token", user.token.Encode())
334+
}
335335
// add the authorization header if requested
336336
if r.config.EnableAuthorizationHeader {
337337
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", user.token.Encode()))
338338
}
339+
339340
// inject any custom claims
340341
for claim, header := range customClaims {
341342
if claim, found := user.claims[claim]; found {

server_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,46 @@ func TestSkipClientIDEnabled(t *testing.T) {
287287
p.RunTests(t, requests)
288288
}
289289

290+
func TestAuthTokenHeaderEnabled(t *testing.T) {
291+
p := newFakeProxy(nil)
292+
token := newTestToken(p.idp.getLocation())
293+
signed, _ := p.idp.signToken(token.claims)
294+
295+
requests := []fakeRequest{
296+
{
297+
URI: "/auth_all/test",
298+
RawToken: signed.Encode(),
299+
ExpectedProxyHeaders: map[string]string{
300+
"X-Auth-Token": signed.Encode(),
301+
},
302+
ExpectedProxy: true,
303+
ExpectedCode: http.StatusOK,
304+
},
305+
}
306+
p.RunTests(t, requests)
307+
}
308+
309+
func TestAuthTokenHeaderDisabled(t *testing.T) {
310+
c := newFakeKeycloakConfig()
311+
c.EnableTokenHeader = false
312+
p := newFakeProxy(c)
313+
token := newTestToken(p.idp.getLocation())
314+
signed, _ := p.idp.signToken(token.claims)
315+
316+
requests := []fakeRequest{
317+
{
318+
URI: "/auth_all/test",
319+
RawToken: signed.Encode(),
320+
ExpectedProxyHeaders: map[string]string{
321+
"X-Auth-Token": "",
322+
},
323+
ExpectedProxy: true,
324+
ExpectedCode: http.StatusOK,
325+
},
326+
}
327+
p.RunTests(t, requests)
328+
}
329+
290330
func newTestService() string {
291331
_, _, u := newTestProxyService(nil)
292332
return u
@@ -344,6 +384,7 @@ func newFakeKeycloakConfig() *Config {
344384
EnableAuthorizationHeader: true,
345385
EnableLogging: false,
346386
EnableLoginHandler: true,
387+
EnableTokenHeader: true,
347388
Listen: "127.0.0.1:0",
348389
Scopes: []string{},
349390
Verbose: true,

0 commit comments

Comments
 (0)