Custom OIDC: token exchange url-encodes client secret (RFC 6749 §2.3.1), breaking providers that expect a raw secret (Epic); real error masked by auto-detect fallback
Describe the bug
When using a Custom OIDC provider whose token endpoint expects the raw (un-encoded) client secret, the authorization-code exchange fails with invalid_client, and the real error is masked by the golang.org/x/oauth2 auto-detect fallback.
GoTrue builds the token-endpoint request via oauth2.Config.Exchange (see internal/api/provider/custom_oauth.go → CustomOIDCProvider.GetOAuthToken) and never sets Endpoint.AuthStyle, so it uses AuthStyleUnknown (auto-detect). In golang.org/x/[email protected], internal/token.go:
// newTokenRequest
if authStyle == AuthStyleInHeader {
req.SetBasicAuth(url.QueryEscape(clientID), url.QueryEscape(clientSecret))
}
The client secret is url.QueryEscape-d before being placed in the HTTP Basic header (RFC 6749 §2.3.1). Providers that expect the raw secret (here: Epic Games, issuer https://api.epicgames.dev/epic/oauth/v2) then reject it. Because auto-detect retries in body mode when attempt 1 errors, the surfaced error is from the second attempt, not the first — hiding the true cause.
This only bites when the client secret contains a character url.QueryEscape rewrites (space, +, /, =, @, …). Regenerating the secret until it only contains A–Z a–z 0–9 - _ . ~ makes it work, which confirms the diagnosis.
Evidence
I intercepted the exact requests GoTrue sends by pointing the provider issuer at a proxy that served a custom discovery doc whose token_endpoint was my own logging endpoint (forwarding verbatim to Epic). Two exchanges per link attempt, same authorization code:
Attempt 1 — client_secret_basic (Basic header, secret url-encoded):
Authorization: Basic present (client_id=<CLIENT_ID>, secret present)
body: grant_type=authorization_code, code=…, code_verifier=…, redirect_uri=…/auth/v1/callback
Epic → HTTP 400
{"error":"invalid_client","errorCode":"errors.com.epicgames.account.invalid_client_credentials",
"numericErrorCode":18033,"errorMessage":"Sorry the client credentials you are using are invalid"}
Attempt 2 — auto-detect fallback to client_secret_post (no Authorization header):
Authorization: ABSENT
body: client_id=<CLIENT_ID>, client_secret=…, grant_type=authorization_code, code=…, redirect_uri=…
Epic → HTTP 400
{"error":"invalid_client","errorCode":"errors.com.epicgames.common.oauth.invalid_client",
"numericErrorCode":1011,
"errorMessage":"It appears that your Authorization header may be invalid or not present, please verify that you are sending the correct headers."}
The user only ever sees the Attempt-2 message ("Authorization header … not present"), which is misleading — the real failure is Attempt 1's invalid_client_credentials.
Independently, curl -u "<CLIENT_ID>:<RAW_SECRET>" -d grant_type=client_credentials https://api.epicgames.dev/epic/oauth/v2/token succeeds (HTTP 200) with the raw secret, proving the credentials are valid and the difference is purely the url.QueryEscape encoding.
To Reproduce
- Configure a Custom OIDC provider for Epic Games (issuer
https://api.epicgames.dev/epic/oauth/v2, scopesopenid basic_profile), using a client whose secret contains e.g.+or/. - Call
supabase.auth.linkIdentity({ provider: "custom:epic-games" })and complete Epic login. - Callback fails; GoTrue returns
invalid_client/ "Authorization header may be invalid or not present".
Expected behavior
- The primary (Attempt 1) provider error should be surfaced, not masked by the body-mode fallback.
- Ideally, a way to avoid double-encoding the secret for providers that expect the raw value (e.g. honor
token_endpoint_auth_methods_supported/ allow configuring auth style), or at minimum document this gotcha for Custom OIDC + Epic.
Environment
- Supabase Auth (hosted), Custom OIDC provider.
golang.org/x/oauth2 v0.35.0.- Provider: Epic Games (
api.epicgames.dev/epic/oauth/v2). Its token endpoint expects a raw client secret and rejects RFC 6749 §2.3.1-encoded credentials; that non-compliance is being reported to Epic separately.
Additional context
Workaround: regenerate the Epic client secret until it contains only A–Z a–z 0–9 - _ . ~ (characters url.QueryEscape leaves untouched).