oauth2: join both probe errors when auth style is unknown#803
Conversation
When Endpoint.AuthStyle is AuthStyleUnknown (the default), RetrieveToken probes the token endpoint by trying credentials in the Authorization header first, then in form params if that fails. If both attempts failed, only the second error was returned, silently discarding the first. This is misleading when the first request fails for a reason unrelated to auth style (e.g. misconfiguration or an expired signing key) and the provider has already consumed the authorization code. The second request then fails with a different error (e.g. "code already redeemed") that hides the real cause. Join both errors with errors.Join so callers see the full picture, and update retrieveToken to convert every wrapped *internal.RetrieveError to the public *RetrieveError type so errors.As keeps working. Note: errors.As now unwraps to the first (header) probe error rather than the second (params) one. This is intentional, as the header error is typically the root cause. Fixes golang#786
|
This PR (HEAD: 6250ccf) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/oauth2/+/769980. Important tips:
|
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/769980. |
|
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be Please don’t reply on this GitHub thread. Visit golang.org/cl/769980. |
|
Message from Kenta Ishizaki: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/769980. |
|
Message from Kenta Ishizaki: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/769980. |
When Endpoint.AuthStyle is AuthStyleUnknown (the default),
RetrieveToken probes the token endpoint with two credential
delivery methods: first in the Authorization header, then in
form params if that fails. If both attempts fail, only the
second error was returned, silently discarding the first.
This is misleading when the first request fails for a reason
unrelated to auth style (e.g. misconfiguration or an expired
signing key) and the provider has already consumed the
authorization code. The caller then sees a confusing error
like "code already redeemed" instead of the real cause.
Fixes #786
Changes:
when both attempts fail.
converts *internal.RetrieveError values inside a joined
error to the public *RetrieveError type, so errors.As
keeps working.
header-fails-params-succeeds. Update existing tests.
Compatibility notes:
directly will get ok == false for the joined error in the
both-probes-fail path. Use errors.As instead.
(header) probe error rather than the second (params) one.
This is intentional, as the header error is typically the
root cause.
messages (newline-separated via errors.Join).
Credit: Thanks to @masonelmore for the thorough analysis
and reproduction repo
(https://github.com/masonelmore/authstyle-unknown).