-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add working OIDC implementation with user and groups sync
- Loading branch information
Showing
4 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from mozilla_django_oidc.auth import OIDCAuthenticationBackend | ||
from django.contrib.auth.models import Group | ||
|
||
class TeknologOIDCAB(OIDCAuthenticationBackend): | ||
def get_username(self, claims): | ||
return claims.get('preferred_username') | ||
|
||
def create_user(self, claims): | ||
user = super(TeknologOIDCAB, self).create_user(claims) | ||
|
||
user.first_name = claims.get('given_name', '') | ||
user.last_name = claims.get('family_name', '') | ||
groups = self.get_or_create_groups(claims.get('groups')) | ||
user.groups.set(groups) | ||
user.save() | ||
|
||
return user | ||
|
||
def update_user(self, user, claims): | ||
user.first_name = claims.get('given_name', '') | ||
user.last_name = claims.get('family_name', '') | ||
groups = self.get_or_create_groups(claims.get('groups')) | ||
user.groups.set(groups) | ||
user.save() | ||
|
||
return user | ||
|
||
def get_or_create_groups(self, group_names): | ||
groups = [] | ||
for group_name in group_names: | ||
obj, _ = Group.objects.get_or_create(name=group_name) | ||
groups.append(obj) | ||
|
||
return groups |
This file contains 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