Skip to content

Commit 1c53e75

Browse files
committed
oauthserver: preserve X-Remote-User header
1 parent 9590253 commit 1c53e75

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

pkg/oauthserver/oauth_apiserver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,15 @@ func (c *OAuthServerConfig) buildHandlerChainForOAuth(startingHandler http.Handl
345345
panic(err)
346346
}
347347

348-
// add back the Authorization header so that WithOAuth can use it even after WithAuthentication deletes it
348+
// add back the Authorization and X-Remote-User headers so that WithOAuth can use it even after WithAuthentication deletes it
349349
// WithOAuth sees users' passwords and can mint tokens so this is not really an issue
350-
handler = headers.WithRestoreAuthorizationHeader(handler)
350+
handler = headers.WithRestoreOAuthHeaders(handler)
351351

352352
// this is the normal kube handler chain
353353
handler = genericapiserver.DefaultBuildHandlerChain(handler, genericConfig)
354354

355-
// store a copy of the Authorization header for later use
356-
handler = headers.WithPreserveAuthorizationHeader(handler)
355+
// store a copy of the Authorization and X-Remote-User headers for later use
356+
handler = headers.WithPreserveOAuthHeaders(handler)
357357

358358
// protected endpoints should not be cached
359359
handler = headers.WithStandardHeaders(handler)

pkg/server/headers/oauthbasic.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,35 @@ package headers
33
import "net/http"
44

55
const (
6-
authzHeader = "Authorization"
7-
copyAuthzHeader = "oauth.openshift.io:" + authzHeader // will never conflict because : is not a valid header key
6+
headerCopyPrefix = "oauth.openshift.io:" // will never conflict because : is not a valid header key
87
)
98

10-
func WithPreserveAuthorizationHeader(handler http.Handler) http.Handler {
9+
var oauthHeaders = []string{
10+
"Authorization",
11+
"X-Remote-User",
12+
}
13+
14+
func WithPreserveOAuthHeaders(handler http.Handler) http.Handler {
1115
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
12-
if vv, ok := r.Header[authzHeader]; ok {
13-
r.Header[copyAuthzHeader] = vv // capture the values before they are deleted
16+
for _, header := range oauthHeaders {
17+
if vv, ok := r.Header[header]; ok {
18+
headerCopy := headerCopyPrefix + header
19+
r.Header[headerCopy] = vv // capture the values before they are deleted
20+
}
1421
}
1522

1623
handler.ServeHTTP(w, r)
1724
})
1825
}
1926

20-
func WithRestoreAuthorizationHeader(handler http.Handler) http.Handler {
27+
func WithRestoreOAuthHeaders(handler http.Handler) http.Handler {
2128
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22-
if vv, ok := r.Header[copyAuthzHeader]; ok {
23-
r.Header[authzHeader] = vv // add them back afterwards for use in OAuth flows
24-
delete(r.Header, copyAuthzHeader)
29+
for _, header := range oauthHeaders {
30+
headerCopy := headerCopyPrefix + header
31+
if vv, ok := r.Header[headerCopy]; ok {
32+
r.Header[header] = vv // add them back afterwards for use in OAuth flows
33+
delete(r.Header, headerCopy)
34+
}
2535
}
2636

2737
handler.ServeHTTP(w, r)

0 commit comments

Comments
 (0)