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

Add enable_pkce config, True by default #765

Merged
merged 5 commits into from
Oct 1, 2024

Conversation

renan-r-santos
Copy link
Contributor

Fixes #468

oauthenticator/oauth2.py Outdated Show resolved Hide resolved
oauthenticator/oauth2.py Outdated Show resolved Hide resolved
oauthenticator/oauth2.py Outdated Show resolved Hide resolved
)


async def test_callback_handler_pkce():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test to verify an error is returned if PKCE is requested but the server doesn't support it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I guess the way an Oauth server tells it doesn't support PKCE would be by returning a 403 when the client tries to exchange the code for a token.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't it work but just not be checked for validity if the provider doesn't support it (ignored extra parameters)? If that's true, should it be on by default?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the parameters are definitely ignored then we could always send them, and change the property name to require_pkce to enforce it on the client side.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@renan-r-santos Sorry, I realise now I was unclear in my request. I was thinking of testing that raise web.HTTPError(400, "Missing code_verifier") is raised when the server silently ignores the PKCE request.

Regarding whether or not to always send the PKCE request, how about if we rename the parameter require_pkce instead of pkce, but keep the current implementation (only send the PKCE field when require_pkce = True? That lets us switch to always sending PKCE in future if we want, without having to change or add any parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding whether or not to always send the PKCE request, how about if we rename the parameter require_pkce instead of pkce, but keep the current implementation (only send the PKCE field when require_pkce = True?

That sounds like a good plan to me. I've updated the PR to reflect that.

I was thinking of testing that raise web.HTTPError(400, "Missing code_verifier") is raised when the server silently ignores the PKCE request.

raise web.HTTPError(400, "Missing code_verifier") won't get raised if the server silently ignores the PKCE request. code_verifier is data that we store in a cookie together with state_id and next_url in the login handler, but code_verifier isn't sent to or returned from the OAuth provider during login. It is only during code exchange that the client grabs the code_verifier previously stored in a cookie and sends it to the server so it can hash it and compare it with the code_challenge.

So, the error you mentioned can only happen if the cookie got somehow deleted or corrupted between login and callback handlers. If you still think it is worth adding a test for that, let me know and I'll update the PR. I could be wrong, but I don't think there's a way for a client to know if a server ignores PKCE parameters. On the other hand, a server can enforce that clients use PKCE.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think PKCE is only for the provider to check; clients only provide information. So the only reason to disable it that I can see is if some provider raises on unrecognized arguments, which is officially wrong:

The authorization server MUST ignore unrecognized request parameters.

So if we're only talking about valid OAuth providers, we don't even need to make it optional.

From the PKCE spec:

As the OAuth 2.0 [RFC6749] server responses are unchanged by this
specification, client implementations of this specification do not
need to know if the server has implemented this specification or not
and SHOULD send the additional parameters as defined in Section 4 to
all servers.

i.e. it's always right to send PKCE, and it's entirely up to the provider to decide whether to validate or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for the references, especially

The authorization server MUST ignore unrecognized request parameters.

which I didn't know and makes a lot of difference.

@renan-r-santos
Copy link
Contributor Author

renan-r-santos commented Sep 28, 2024

By the way, I think this PR also helps a bit in getting OAuthenticator compatible with OAuth 2.1 (still in draft) as:

PKCE is required for all OAuth clients using the authorization code

oauthenticator/oauth2.py Outdated Show resolved Hide resolved
oauthenticator/oauth2.py Outdated Show resolved Hide resolved
Change enable_pkce default to True
Factor out code_verifier and code_challenge generation
Improve docs
Update tests
@renan-r-santos
Copy link
Contributor Author

Guys, I think this is ready for another round of reviews. I made a few improvements and incorporated important points that we discussed here in the enable_pkce docs. Thank you for your patience!

Copy link
Member

@consideRatio consideRatio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read the docstring and considered the config naming and think its great, i'll leave the implementation review to another person as i think its already been considered and i've not onboarded myself enough to review it myself yet.

Thank you @renan-r-santos for working this so thoroughly!! ♥️🎉🥳

@consideRatio consideRatio changed the title Add PKCE Add enable_pkce config, defaults to True Sep 30, 2024
@consideRatio consideRatio changed the title Add enable_pkce config, defaults to True Add enable_pkce config, True by default Sep 30, 2024
@minrk
Copy link
Member

minrk commented Oct 1, 2024

Thank you!

@minrk minrk merged commit 0df5b82 into jupyterhub:main Oct 1, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Generic] Generic Oauthenticator Not support PKCE
4 participants