35 lines
1.4 KiB
Python
35 lines
1.4 KiB
Python
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
|
|
from allauth.socialaccount.models import SocialLogin
|
|
|
|
from dgsi.models import User
|
|
|
|
|
|
class SharedAccountAdapter(DefaultSocialAccountAdapter):
|
|
"""
|
|
Overrides the Account Adapter, to allow a simpler connection via CAS.
|
|
"""
|
|
|
|
def pre_social_login(self, request, sociallogin):
|
|
match sociallogin.account.provider:
|
|
# TODO: Add a correspondance table between ENS logins and ours
|
|
case "ens_cas":
|
|
# In this case, the username is located in extra_data["uid"]
|
|
username = sociallogin.account.extra_data["uid"]
|
|
case "kanidm":
|
|
username = sociallogin.account.extra_data["preferred_username"]
|
|
case _p:
|
|
raise KeyError(f"No sociallogin '{_p}' is supposed to exist.")
|
|
|
|
try:
|
|
# Connect an existing user if the login already exists, even if it
|
|
# with another social method
|
|
user = User.objects.get(username=username)
|
|
sociallogin.connect(request, user)
|
|
except User.DoesNotExist:
|
|
pass
|
|
|
|
def populate_user(self, request, sociallogin, data):
|
|
return super().populate_user(request, sociallogin, data)
|
|
|
|
def save_user(self, request, sociallogin: SocialLogin, form=None):
|
|
return super().save_user(request, sociallogin, form)
|