Skip to content

Conversation

@paskal
Copy link
Collaborator

@paskal paskal commented Dec 21, 2025

Summary

  • When SendJWTHeader is true, now sets both the JWT header AND cookies
  • Fixes OAuth authentication flows where HTTP headers don't survive browser redirects

Problem

When SendJWTHeader=true, OAuth authentication fails because:

  1. The OAuth callback handler sets the JWT via header only
  2. Then redirects back to the original page
  3. HTTP headers don't survive redirects, so the JWT is lost
  4. The user sees "failed to get token" error

Solution

Always set cookies alongside the header when SendJWTHeader=true. This way:

  • API calls can use the header (for cross-domain scenarios)
  • OAuth redirect flows work via cookies (which survive redirects)

This is a minimal, backwards-compatible change - clients expecting headers still get them.

Might fix umputun/remark42#1877, not sure before testing.

When SendJWTHeader is true, now sets both the JWT header AND cookies.
This fixes OAuth authentication flows where HTTP headers don't survive
browser redirects. Cookies are needed for the OAuth callback to complete
successfully, while headers are still set for direct API calls.

Fixes umputun/remark42#1877
@coveralls
Copy link

Pull Request Test Coverage Report for Build 20402130242

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.08%) to 83.577%

Totals Coverage Status
Change from base Build 20279596752: -0.08%
Covered Lines: 2687
Relevant Lines: 3215

💛 - Coveralls

@coveralls
Copy link

coveralls commented Dec 21, 2025

Pull Request Test Coverage Report for Build 20442452487

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.08%) to 83.577%

Totals Coverage Status
Change from base Build 20279596752: -0.08%
Covered Lines: 2687
Relevant Lines: 3215

💛 - Coveralls

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes OAuth authentication failures when SendJWTHeader is enabled by setting both JWT headers and cookies instead of only headers. The issue occurs because HTTP headers don't survive browser redirects during OAuth callback flows, causing the JWT token to be lost.

Key Changes:

  • Modified JWT token setting behavior to always set cookies alongside headers when SendJWTHeader=true
  • Updated tests to verify both cookies and headers are set correctly in header mode

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
token/jwt.go Removed early return in Set() function to allow cookie setting after header is set
v2/token/jwt.go Same fix as token/jwt.go for the v2 module version
token/jwt_test.go Updated TestJWT_SendJWTHeader to verify both cookies and header are set
v2/token/jwt_test.go Same test update as token/jwt_test.go for the v2 module version

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

require.Equal(t, 2, len(cookies), "cookies set alongside header")
assert.Equal(t, "JWT", cookies[0].Name)
assert.Equal(t, testJwtValid, cookies[0].Value)
assert.Equal(t, "XSRF-TOKEN", cookies[1].Name)
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test verifies that the XSRF-TOKEN cookie exists but does not check its value. For consistency with other tests (like TestJWT_SetWithDomain at line 241), this should also verify that the XSRF token value matches the expected claims ID ("random id" from testClaims). Consider adding an assertion like: assert.Equal(t, "random id", cookies[1].Value)

Copilot uses AI. Check for mistakes.
require.Equal(t, 2, len(cookies), "cookies set alongside header")
assert.Equal(t, "JWT", cookies[0].Name)
assert.Equal(t, testJwtValid, cookies[0].Value)
assert.Equal(t, "XSRF-TOKEN", cookies[1].Name)
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test verifies that the XSRF-TOKEN cookie exists but does not check its value. For consistency with other tests (like TestJWT_SetWithDomain at line 241), this should also verify that the XSRF token value matches the expected claims ID ("random id" from testClaims). Consider adding an assertion like: assert.Equal(t, "random id", cookies[1].Value)

Copilot uses AI. Check for mistakes.
verify XSRF-TOKEN cookie value matches claims ID for consistency
with TestJWT_SetWithDomain
Copy link
Member

@umputun umputun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - fixes OAuth redirect flow when SendJWTHeader is enabled, test improved to verify XSRF cookie value

@umputun umputun merged commit fe8d691 into master Dec 22, 2025
6 checks passed
@umputun umputun deleted the fix-oauth-sendjwtheader branch December 22, 2025 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SendJWTHeader leads to {"error":"failed to get token"}

4 participants