-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add pkce-openid-backend #255
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1584426
feat: add pkce-openid-backend
johanseto 53dee2f
refactor: apply suggestions from code review
johanseto 6664e76
feat: reorder some logic for the mixin
johanseto 816e40f
refactor: avoid repeated pkce conf defintion
johanseto bab0bd5
feat: update with small changes social_core
johanseto 5a4fc33
refactor: suggestions from code review
johanseto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,8 @@ | ||||||||||||
| """ | ||||||||||||
| Extensions to the regular defined third party auth backends | ||||||||||||
| """ | ||||||||||||
| import base64 | ||||||||||||
| import hashlib | ||||||||||||
| import logging | ||||||||||||
|
|
||||||||||||
| from django.conf import settings | ||||||||||||
|
|
@@ -199,3 +201,82 @@ def get_user_id(self, *args, **kwargs): | |||||||||||
| LOG.info("Updating uid: %s to %s", uid, slug_uid) | ||||||||||||
|
|
||||||||||||
| return slug_uid | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class BaseOAuth2PKCEMixin: | ||||||||||||
| """ | ||||||||||||
| TO-DO: Use the `from social_core.backends.oauth import BaseOAuth2PKCE` base class once the pull request is merged: https://github.com/python-social-auth/social-core/pull/856/files#diff-d44db201b48f2ec7cab2a0c981213a2991630567778cc6608d03fa0e3804e466R467 | ||||||||||||
| Base class for providers using OAuth2 with Proof Key for Code Exchange (PKCE). | ||||||||||||
| OAuth2 details at: | ||||||||||||
| https://datatracker.ietf.org/doc/html/rfc6749 | ||||||||||||
| PKCE details at: | ||||||||||||
| https://datatracker.ietf.org/doc/html/rfc7636 | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
| PKCE_DEFAULT_CODE_CHALLENGE_METHOD = "s256" | ||||||||||||
| PKCE_DEFAULT_CODE_VERIFIER_LENGTH = 32 | ||||||||||||
| DEFAULT_USE_PKCE = True | ||||||||||||
|
|
||||||||||||
| def create_code_verifier(self): | ||||||||||||
| name = f"{self.name}_code_verifier" | ||||||||||||
| code_verifier_len = self.setting( | ||||||||||||
| "PKCE_CODE_VERIFIER_LENGTH", default=self.PKCE_DEFAULT_CODE_VERIFIER_LENGTH | ||||||||||||
|
johanseto marked this conversation as resolved.
|
||||||||||||
| ) | ||||||||||||
| code_verifier = self.strategy.random_string(code_verifier_len) | ||||||||||||
| self.strategy.session_set(name, code_verifier) | ||||||||||||
| return code_verifier | ||||||||||||
|
johanseto marked this conversation as resolved.
|
||||||||||||
|
|
||||||||||||
| def get_code_verifier(self): | ||||||||||||
| name = f"{self.name}_code_verifier" | ||||||||||||
| code_verifier = self.strategy.session_get(name) | ||||||||||||
| return code_verifier | ||||||||||||
|
Comment on lines
+231
to
+232
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| def generate_code_challenge(self, code_verifier, challenge_method): | ||||||||||||
| method = challenge_method.lower() | ||||||||||||
| if method == "s256": | ||||||||||||
| hashed = hashlib.sha256(code_verifier.encode()).digest() | ||||||||||||
| encoded = base64.urlsafe_b64encode(hashed) | ||||||||||||
| code_challenge = encoded.decode().replace("=", "") # remove padding | ||||||||||||
| return code_challenge | ||||||||||||
|
johanseto marked this conversation as resolved.
|
||||||||||||
| if method == "plain": | ||||||||||||
| return code_verifier | ||||||||||||
| raise AuthException("Unsupported code challenge method.") | ||||||||||||
|
|
||||||||||||
| def auth_params(self, state=None): | ||||||||||||
| params = super().auth_params(state=state) | ||||||||||||
|
|
||||||||||||
| if self.setting("USE_PKCE", default=self.DEFAULT_USE_PKCE): | ||||||||||||
| code_challenge_method = self.setting( | ||||||||||||
| "PKCE_CODE_CHALLENGE_METHOD", | ||||||||||||
| default=self.PKCE_DEFAULT_CODE_CHALLENGE_METHOD, | ||||||||||||
| ) | ||||||||||||
| code_verifier = self.create_code_verifier() | ||||||||||||
| code_challenge = self.generate_code_challenge( | ||||||||||||
| code_verifier, code_challenge_method | ||||||||||||
| ) | ||||||||||||
|
johanseto marked this conversation as resolved.
|
||||||||||||
| params["code_challenge_method"] = code_challenge_method | ||||||||||||
| params["code_challenge"] = code_challenge | ||||||||||||
| return params | ||||||||||||
|
johanseto marked this conversation as resolved.
|
||||||||||||
|
|
||||||||||||
| def auth_complete_params(self, state=None): | ||||||||||||
| params = super().auth_complete_params(state=state) | ||||||||||||
|
|
||||||||||||
| if self.setting("USE_PKCE", default=self.DEFAULT_USE_PKCE): | ||||||||||||
| code_verifier = self.get_code_verifier() | ||||||||||||
| params["code_verifier"] = code_verifier | ||||||||||||
|
|
||||||||||||
| return params | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class ConfigurableOpenIdConnectAuthPKCE(BaseOAuth2PKCEMixin, ConfigurableOpenIdConnectAuth): | ||||||||||||
| """ | ||||||||||||
| Generic backend based in ConfigurableOpenIdConnectAuth but | ||||||||||||
| with PKCE. | ||||||||||||
| This backend is inspired in the social-core way to implement PKCE. | ||||||||||||
| There is a current PR in working, but for the moment, that class is not merged and accesible. | ||||||||||||
| So after that is finished we use `BaseOAuth2PKCEMixin` for `code_challenge` and `code_challenge_method`implementation. | ||||||||||||
| PR: https://github.com/python-social-auth/social-core/pull/856 | ||||||||||||
| Block code: https://github.com/python-social-auth/social-core/pull/856/files#diff-d44db201b48f2ec7cab2a0c981213a2991630567778cc6608d03fa0e3804e466R467-R530 | ||||||||||||
|
|
||||||||||||
| """ | ||||||||||||
| name = 'config-based-openidconnect-PKCE' | ||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.