[GH-ISSUE #4490] oauth2: "invalid_grant" "Code not valid" during login with Keycloak (self-hosted NetBird) #9218

Open
opened 2026-08-05 01:21:19 -04:00 by saavagebueno · 0 comments
Owner

Originally created by @tkloda on GitHub (Sep 12, 2025).
Original GitHub issue: https://github.com/netbirdio/netbird/issues/4490

Describe the problem
When using self-hosted NetBird with Keycloak as the IdP, authentication sometimes fails with: oauth2: "invalid_grant" "Code not valid"

Clearing browser cookies/cache and re-authenticating resolves the issue, but the problem recurs. This suggests stale or invalid tokens are being reused in the browser, leading to invalid_grant responses from Keycloak.

In NetBird management logs I can see only this (which does not make much sense as I use SSO login):
2025-09-12T12:36:01Z WARN [requestID: some_id, accountID: UNKNOWN, peerID: some_id, context: GRPC] management/server/grpcserver.go:543: failed logging in peer some_id: no peer auth method provided, please use a setup key or interactive SSO login

To Reproduce

  1. Configure self-hosted NetBird with Keycloak OIDC integration.
  2. Log in via the NetBird UI.
  3. After some time or on subsequent logins, attempt to authenticate again.
  4. Receive oauth2: "invalid_grant" "Code not valid" error.
  5. Clear browser cookies/cache → authentication works again.

Expected behavior

NetBird should either:

  • Refresh the session properly, or
  • Force a clean login without requiring the user to clear browser data manually.

Are you using NetBird Cloud?

No, I'm using Self-host NetBird's control plane.

NetBird version

0.57.0 but it was happening in version 0.56.0 too

Is any other VPN software installed?

No

Screenshots

Image

Additional context

Add any other context about the problem here.

  • This does not seem related to redirect URI or client secret mismatch, as login succeeds after cache/cookie reset.
  • Behavior suggests stale refresh token/session handling in the Management UI.
  • Can NetBird handle invalid/stale refresh tokens more gracefully (e.g., by forcing a new login instead of failing with invalid_grant)?
  • Is there a recommended config for Keycloak to avoid this issue?
  • Documentation mentions NETBIRD_AUTH_PKCE_DISABLE_PROMPT_LOGIN in Authentik setups, but it’s unclear if this applies to Keycloak. Clarification would help.
Originally created by @tkloda on GitHub (Sep 12, 2025). Original GitHub issue: https://github.com/netbirdio/netbird/issues/4490 **Describe the problem** When using self-hosted NetBird with Keycloak as the IdP, authentication sometimes fails with: oauth2: "invalid_grant" "Code not valid" Clearing browser cookies/cache and re-authenticating resolves the issue, but the problem recurs. This suggests stale or invalid tokens are being reused in the browser, leading to invalid_grant responses from Keycloak. In NetBird management logs I can see only this (which does not make much sense as I use SSO login): 2025-09-12T12:36:01Z WARN [requestID: some_id, accountID: UNKNOWN, peerID: some_id, context: GRPC] management/server/grpcserver.go:543: failed logging in peer some_id: no peer auth method provided, please use a setup key or interactive SSO login **To Reproduce** 1. Configure self-hosted NetBird with Keycloak OIDC integration. 2. Log in via the NetBird UI. 3. After some time or on subsequent logins, attempt to authenticate again. 4. Receive oauth2: "invalid_grant" "Code not valid" error. 5. Clear browser cookies/cache → authentication works again. **Expected behavior** NetBird should either: - Refresh the session properly, or - Force a clean login without requiring the user to clear browser data manually. **Are you using NetBird Cloud?** No, I'm using Self-host NetBird's control plane. **NetBird version** 0.57.0 but it was happening in version 0.56.0 too **Is any other VPN software installed?** No **Screenshots** <img width="726" height="400" alt="Image" src="https://github.com/user-attachments/assets/a1d1e1c7-f80b-479a-84ff-8a041632476c" /> **Additional context** Add any other context about the problem here. - This does not seem related to redirect URI or client secret mismatch, as login succeeds after cache/cookie reset. - Behavior suggests stale refresh token/session handling in the Management UI. - Can NetBird handle invalid/stale refresh tokens more gracefully (e.g., by forcing a new login instead of failing with `invalid_grant`)? - Is there a recommended config for Keycloak to avoid this issue? - Documentation mentions `NETBIRD_AUTH_PKCE_DISABLE_PROMPT_LOGIN` in Authentik setups, but it’s unclear if this applies to Keycloak. Clarification would help.
saavagebueno added the triage-needed label 2026-08-05 01:21:19 -04:00
Sign in to join this conversation.
No Label triage-needed
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: DYNR/netbird#9218