How to Configure OIDC Invalid Grant
An oidc invalid grant error happens when an identity server rejects an expired or mismatched login code. You fix it by matching redirect URLs, syncing server clocks, and setting clear token lifetimes. Identity servers use strict rules to check every login request. When a user or app sends a bad code or an old refresh token, the server stops the login. Setting up these token rules the right way keeps your apps working and blocks bad login attempts. How oidc invalid grant rules work in practice
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
An oidc invalid grant error happens when an identity server rejects an expired or mismatched login code. You fix it by matching redirect URLs, syncing server clocks, and setting clear token lifetimes.
Identity servers use strict rules to check every login request. When a user or app sends a bad code or an old refresh token, the server stops the login. Setting up these token rules the right way keeps your apps working and blocks bad login attempts.
How oidc invalid grant rules work in practice
OpenID Connect uses short-lived codes and refresh tokens to log users and workloads in. The identity provider gives the app a one-time code after the user logs in. The app must trade that code for a session token within a very short time window.
Several common setup mistakes trigger an oidc invalid grant response during this trade. First, the app might send a code that already expired or was used once before. Second, the redirect URL in the token request might not match the URL from the first login step. Third, clock drift between the app server and the identity server can make new tokens look expired.
Refresh tokens also cause this error when they pass their max age or get revoked. If an admin changes a user role or resets a password, the server often kills active refresh tokens. When the app tries to use that dead token, the server rejects it right away.
Safe defaults prevent most of these login failures across your team and apps. Keep authorization code lifetimes short so stolen codes cannot work later. Set up Network Time Protocol on every server so clocks stay in sync at all times. Turn on refresh token rotation so each token works only once before the app gets a new one.
End-to-End Encryption + K8s OIDC Authentication: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Checking token setups and what static rules miss
To check your setup, read the identity provider logs right when a login fails. The server sends a plain error message to the user, but internal logs show the exact cause. You can see if a code was used twice, if a clock drifted, or if a URL failed.
Test your login flow by forcing token expirations in a test app. Make sure your app catches the error, clears local session data, and sends the user back to log in. Never let an app get stuck in an endless loop retrying a dead refresh token.
Identity provider checks only happen at the moment a user logs in or refreshes a token. Once the app gets a valid session token, the identity server steps out of the way. If a user or AI tool runs a bad database query, the identity provider does not see it. Static login setups cannot check single commands or rewrite sensitive fields like emails, phone numbers, or card numbers.
Check database and API queries on the wire
Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.
Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.
Frequently asked questions
Why does an oidc invalid grant error happen on refresh tokens?
Refresh tokens fail when they expire, get revoked by an admin, or get used twice during rotation. If a user logs out or changes their password, the server kills the old refresh token.
How does server clock drift cause login failures?
Identity servers put timestamps on every code and token they issue. If your app server clock runs behind or ahead, the identity provider sees the token timestamp as invalid.
Can using the back button in a browser trigger this error?
Yes, hitting the back button during login can make the browser send the same authorization code twice. Because authorization codes work only once, the identity server rejects the second attempt right away.
Related guides
In this post