Skip to content

Commit 28bd825

Browse files
committed
Set cookies for OAuth flows when SendJWTHeader is enabled
This fixes umputun/remark42#1877 where OAuth authentication fails when the send-jwt-header option is enabled. The problem occurred because: 1. When SendJWTHeader is enabled, the auth service only sends the JWT as a header without setting cookies 2. During OAuth flows, the authentication involves redirects between the app and the provider 3. HTTP headers don't persist through redirects, so the authentication state was lost The solution: - Modified the jwt.go token Set method to always set cookies during OAuth handshake phases (when claims.Handshake != nil), even when SendJWTHeader is enabled - For normal authentication (non-handshake), maintain the original behavior where SendJWTHeader=true will only set headers - This ensures the OAuth flow works properly while maintaining the correct behavior for API requests
1 parent a36c268 commit 28bd825

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

token/jwt.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,25 @@ func (j *Service) Set(w http.ResponseWriter, claims Claims) (Claims, error) {
247247
return Claims{}, fmt.Errorf("failed to make token token: %w", err)
248248
}
249249

250-
if j.SendJWTHeader {
251-
w.Header().Set(j.JWTHeaderKey, tokenString)
252-
return claims, nil
253-
}
254-
255250
cookieExpiration := 0 // session cookie
256251
if !claims.SessionOnly && claims.Handshake == nil {
257252
cookieExpiration = int(j.CookieDuration.Seconds())
258253
}
259254

255+
// For OAuth handshake, always set cookies regardless of SendJWTHeader flag
256+
// This allows the OAuth flow to complete successfully
257+
needsCookies := claims.Handshake != nil
258+
259+
// Set the JWT in the header if requested
260+
if j.SendJWTHeader {
261+
w.Header().Set(j.JWTHeaderKey, tokenString)
262+
// Skip setting cookies unless this is part of OAuth handshake
263+
if !needsCookies {
264+
return claims, nil
265+
}
266+
}
267+
268+
// Set cookies (always for OAuth handshake, or when SendJWTHeader is false)
260269
jwtCookie := http.Cookie{Name: j.JWTCookieName, Value: tokenString, HttpOnly: true, Path: "/", Domain: j.JWTCookieDomain,
261270
MaxAge: cookieExpiration, Secure: j.SecureCookies, SameSite: j.SameSite}
262271
http.SetCookie(w, &jwtCookie)

v2/token/jwt.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,25 @@ func (j *Service) Set(w http.ResponseWriter, claims Claims) (Claims, error) {
264264
return Claims{}, fmt.Errorf("failed to make token token: %w", err)
265265
}
266266

267-
if j.SendJWTHeader {
268-
w.Header().Set(j.JWTHeaderKey, tokenString)
269-
return claims, nil
270-
}
271-
272267
cookieExpiration := 0 // session cookie
273268
if !claims.SessionOnly && claims.Handshake == nil {
274269
cookieExpiration = int(j.CookieDuration.Seconds())
275270
}
276271

272+
// For OAuth handshake, always set cookies regardless of SendJWTHeader flag
273+
// This allows the OAuth flow to complete successfully
274+
needsCookies := claims.Handshake != nil
275+
276+
// Set the JWT in the header if requested
277+
if j.SendJWTHeader {
278+
w.Header().Set(j.JWTHeaderKey, tokenString)
279+
// Skip setting cookies unless this is part of OAuth handshake
280+
if !needsCookies {
281+
return claims, nil
282+
}
283+
}
284+
285+
// Set cookies (always for OAuth handshake, or when SendJWTHeader is false)
277286
jwtCookie := http.Cookie{Name: j.JWTCookieName, Value: tokenString, HttpOnly: true, Path: "/", Domain: j.JWTCookieDomain,
278287
MaxAge: cookieExpiration, Secure: j.SecureCookies, SameSite: j.SameSite}
279288
http.SetCookie(w, &jwtCookie)

0 commit comments

Comments
 (0)