Skip to content

Commit

Permalink
Add custom user mapper for openid
Browse files Browse the repository at this point in the history
  • Loading branch information
timonegk committed Feb 5, 2024
1 parent b53e9e5 commit 797dca3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 22 additions & 0 deletions karma/karma/openid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.contrib.auth import get_user_model
from simple_openid_connect.integrations.django.models import OpenidUser
from simple_openid_connect.integrations.django.user_mapping import UserMapper


class KarmaUserMapper(UserMapper):
def handle_federated_userinfo(self, user_data):
# if there is already a user with this username, we create the openid association if it does not exist yet
User = get_user_model()
try:
user = User.objects.get(username=user_data.preferred_username)
OpenidUser.objects.get_or_create(
sub=user_data.sub,
defaults={
"user": user,
},
)
except User.DoesNotExist:
# if the user does not exist, it is automatically created by the super class
pass
return super().handle_federated_userinfo(user_data)

2 changes: 0 additions & 2 deletions karma/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@
path('', lambda req: redirect('karma_index'), name='index'),
path('api/', include('karma.api.urls')),
path('calibration/', include('karma.calibration.urls')),
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path("auth/openid/", include("simple_openid_connect.integrations.django.urls")),
]

0 comments on commit 797dca3

Please sign in to comment.