Skip to content

Commit 3cb341b

Browse files
committed
Merge branch 'main' into x-forwarded-headers
2 parents 7ecb722 + 8f30869 commit 3cb341b

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

pkg/auth/oauth.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type OAuthMiddleware struct {
2424
hubHost string
2525
hubPrefix string
2626
cookieName string
27+
headerName string
2728
callbackPath string // Custom callback path (e.g., "oauth_callback" or "_temp/jhub-app-proxy/oauth_callback")
2829
logger *logger.Logger
2930
}
@@ -83,6 +84,7 @@ func NewOAuthMiddlewareWithCallbackPath(log *logger.Logger, callbackPath string)
8384
hubHost: hubHost,
8485
hubPrefix: hubPrefix,
8586
cookieName: clientID,
87+
headerName: "X-Jupyterhub-Api-Token",
8688
callbackPath: callbackPath,
8789
logger: log.WithComponent("oauth"),
8890
}, nil
@@ -98,19 +100,33 @@ func (m *OAuthMiddleware) Wrap(next http.Handler) http.Handler {
98100
return
99101
}
100102

101-
// Check for token in cookie
102-
cookie, err := r.Cookie(m.cookieName)
103-
if err == nil && cookie.Value != "" {
104-
if user, err := m.getUser(cookie.Value); err == nil {
105-
pr := new(http.Request)
106-
*pr = *r
107-
108-
u, _ := json.Marshal(user)
109-
pr.Header.Set("X-Forwarded-User-Data", string(u))
103+
maybeProxy := func(token string) bool {
104+
if token == "" {
105+
return false
106+
}
110107

111-
next.ServeHTTP(w, r)
112-
return
108+
user, err := m.getUser(token)
109+
if err != nil {
110+
return false
113111
}
112+
113+
pr := new(http.Request)
114+
*pr = *r
115+
116+
userData, _ := json.Marshal(user)
117+
pr.Header.Set("X-Forwarded-User-Data", string(userData))
118+
119+
next.ServeHTTP(w, r)
120+
return true
121+
}
122+
123+
if maybeProxy(r.Header.Get(m.headerName)) {
124+
return
125+
}
126+
127+
cookie, err := r.Cookie(m.cookieName)
128+
if err == nil && maybeProxy(cookie.Value) {
129+
return
114130
}
115131

116132
// No valid token, redirect to OAuth

0 commit comments

Comments
 (0)