Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] - OIDC token exchange fails with "invalid client_secret" when using Dex #4633

Open
damacus opened this issue Nov 30, 2024 · 7 comments
Labels

Comments

@damacus
Copy link
Contributor

damacus commented Nov 30, 2024

Description

When using Mealie v2.3.0 with Dex as an OIDC provider, the authentication flow fails during the token exchange step. The initial authentication with the identity provider (Google through Dex) succeeds, but Mealie fails to exchange the authorization code for tokens.

Environment

  • Mealie version: v2.3.0
  • OIDC Provider: Dex v2.41.1
  • Identity Provider: Google (through Dex)
  • Deployment: Kubernetes

Configuration

Environment variables set:

OIDC_AUTH_ENABLED: "true"
OIDC_SIGNUP_ENABLED: "true"
OIDC_CONFIGURATION_URL: "https://dex.example.com/.well-known/openid-configuration"
OIDC_CLIENT_ID: "mealie"
OIDC_AUTO_REDIRECT: "false"
OIDC_PROVIDER_NAME: "Dex"
OIDC_REMEMBER_ME: "false"
OIDC_SCOPES: "openid profile email groups"
OIDC_USER_CLAIM: "email"
OIDC_NAME_CLAIM: "name"
OIDC_GROUPS_CLAIM: "groups"

Error Messages

Dex logs:

{"time":"2024-11-30T21:43:39.272147263Z","level":"INFO","msg":"login successful","connector_id":"google","username":"[redacted]","preferred_username":"","email":"[redacted]","groups":null,"request_id":"105916be-ed71-4c34-9f31-e376940f934e"}
{"time":"2024-11-30T21:43:39.701309503Z","level":"INFO","msg":"invalid client_secret on token request","client_id":"mealie","request_id":"4b7d55a8-5033-4c3b-96c0-1afc8138247d"}

Mealie logs:

authlib.integrations.base_client.errors.OAuthError: invalid_client: Invalid client credentials.

Expected Behavior

The token exchange should succeed, allowing the user to complete the OIDC authentication flow and log in to Mealie.

Actual Behavior

The token exchange fails with a 401 Unauthorized error, indicating an issue with the client secret handling during the token exchange phase.

Additional Context

  • The initial authentication with Google through Dex is successful
  • The error occurs specifically during the token exchange step
  • Other OIDC clients (like Grafana) work correctly with the same Dex configuration
@JoTec2002
Copy link
Contributor

Have you tried changing OIDC_NAME_CLAIM: "name" to "email" or "username"?

@cmintey
Copy link
Contributor

cmintey commented Dec 3, 2024

Your Dex logs give you a pretty good error.

invalid client_secret on token request","client_id":"mealie"

Mealie is sending the the wrong client_secret to your IdP. This can either be because you've entered it wrong, or it might have an invalid character in the string. Double check that you've entered the correct client_secret into Mealie and that it matches what Dex expects and check that the client secret doesn't contain any & characters as that can cause issues in the request

@hay-kot hay-kot added the OIDC label Dec 4, 2024
@redxef
Copy link

redxef commented Dec 9, 2024

My token contained a slash (/) and a plus (+) - I'm not sure which of them broke the authentication flow, it might be both or either. Tokens containing those characters work fine with other clients, but not Mealie. I guess this is not standards conform. Still, maybe a hint in the documentation would be appropriate?

@cmintey
Copy link
Contributor

cmintey commented Dec 9, 2024

Mealie (or rather Authlib) sends the client secret as a url parameter, which is valid per the spec, but it means the secret needs to be url safe. Definitely worth a mention in the doc

@damacus
Copy link
Contributor Author

damacus commented Dec 9, 2024

Strange, the secret is definitely URL safe and the secret being sent matches the one Dex is storing.

I'll try changing the claim name over to email shortly 👍
Thanks for your help on this one.

@damacus
Copy link
Contributor Author

damacus commented Dec 11, 2024

Strange, the secret is definitely URL safe and the secret being sent matches the one Dex is storing.

I'll try changing the claim name over to email shortly 👍 Thanks for your help on this one.

That definitely should have worked, by what the .well-known spec suggests. But it doesn't look like it has.

@redxef
Copy link

redxef commented Dec 11, 2024

Hm, I was actually using Authelia, but had the exact same error messages as you and figured it might be related. Sorry that that didn't work out for you. One particular thing I noticed: Authlib used the exact same error message returned by my IdP portal, as opposed to your errors. Did you try tracing the error where exactly it originates from? Maybe that will give you a clue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants