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

Wrong PKCE padding in client example. #275

Open
coryschwartz opened this issue Feb 14, 2025 · 0 comments · May be fixed by #276
Open

Wrong PKCE padding in client example. #275

coryschwartz opened this issue Feb 14, 2025 · 0 comments · May be fixed by #276

Comments

@coryschwartz
Copy link

A small bug in your example client -- the PKCE challenge is not correct.

I discovered this bug while I was testing an authentication service, and I used the example client in this repo as the 3rd party client. The authentication service returned an error to the client indicating that
the code_verifier did not match. When I looked into it, the code does, in fact, except for the encoding padding.

Unsure which was correct, I checked the RFC.

https://www.rfc-editor.org/rfc/rfc7636.txt

Section 3, regarding the Base64url Encoding states the following:

      Base64 encoding using the URL- and filename-safe character set
      defined in Section 5 of [RFC4648], with all trailing '='
      characters omitted (as permitted by Section 3.2 of [RFC4648]) and
      without the inclusion of any line breaks, whitespace, or other
      additional characters.  (See Appendix A for notes on implementing
      base64url encoding without padding.)

The problem is that this function https://github.com/go-oauth2/oauth2/blob/master/example/client/client.go#L138 does not trim the pesky '=' symbols off the end, and we have to specifically turn the padding off to be compliant.

And in case you are wondering how it is that the server package in this repo works. This server trims the padding for us so it would accept the challenge with or without the padding.

https://github.com/go-oauth2/oauth2/blob/master/const.go#L63

...
	case CodeChallengeS256:
		s256 := sha256.Sum256([]byte(ver))
		// trim padding
		a := strings.TrimRight(base64.URLEncoding.EncodeToString(s256[:]), "=")
		b := strings.TrimRight(cc, "=")
		return a == b

Apparently, the authentication server I'm testing with is a bit more strict.

@coryschwartz coryschwartz linked a pull request Feb 14, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant