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

Make Keycloak's ID_KEY configurable #815

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- Fixed Azure AD Tenant authentication with custom signing keys
- Added CAS OIDC backend
- Made Keycloak `ID_KEY` configurable

## [4.4.1](https://github.com/python-social-auth/social-core/releases/tag/4.4.1) - 2023-03-30

Expand Down
14 changes: 11 additions & 3 deletions social_core/backends/keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class KeycloakOAuth2(BaseOAuth2): # pylint: disable=abstract-method
"""

name = "keycloak"
ID_KEY = "username"
ACCESS_TOKEN_METHOD = "POST"
REDIRECT_STATE = False

Expand All @@ -121,6 +120,9 @@ def public_key(self):
]
)

def id_key(self):
return self.setting("ID_KEY", default="username")

def user_data(
self, access_token, *args, **kwargs
): # pylint: disable=unused-argument
Expand Down Expand Up @@ -149,5 +151,11 @@ def get_user_details(self, response):
}

def get_user_id(self, details, response):
"""Get and associate Django User by the field indicated by ID_KEY"""
return details.get(self.ID_KEY)
"""Get and associate Django User by the field indicated by ID_KEY

The ID_KEY can be any field in the user details or the access token.
"""
id_key = self.id_key()
if id_key in details:
return details[id_key]
return response.get(id_key)