| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import HttpRequest |
| 4 |
|
| 5 |
from allauth.account.models import EmailAddress |
| 6 |
from allauth.socialaccount.app_settings import QUERY_EMAIL |
| 7 |
from allauth.socialaccount.providers.base import AuthAction, ProviderAccount |
| 8 |
from allauth.socialaccount.providers.cilogon.views import CILogonOAuth2Adapter |
| 9 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 10 |
|
| 11 |
|
| 12 |
class Scope: |
| 13 |
OPENID = "openid" |
| 14 |
EMAIL = "email" |
| 15 |
PROFILE = "profile" |
| 16 |
USERINFO = "org.cilogon.userinfo" |
| 17 |
|
| 18 |
|
| 19 |
class CILogonAccount(ProviderAccount): |
| 20 |
pass |
| 21 |
|
| 22 |
|
| 23 |
class CILogonProvider(OAuth2Provider): |
| 24 |
id = "cilogon" |
| 25 |
name = "CILogon" |
| 26 |
account_class = CILogonAccount |
| 27 |
oauth2_adapter_class = CILogonOAuth2Adapter |
| 28 |
|
| 29 |
def get_default_scope(self): |
| 30 |
scope = [Scope.PROFILE, Scope.USERINFO, Scope.OPENID] |
| 31 |
if QUERY_EMAIL: |
| 32 |
scope.append(Scope.EMAIL) |
| 33 |
return scope |
| 34 |
|
| 35 |
def get_auth_params_from_request(self, request: HttpRequest, action): |
| 36 |
ret = super().get_auth_params_from_request(request, action) |
| 37 |
if action == AuthAction.REAUTHENTICATE: |
| 38 |
ret["prompt"] = "select_account consent" |
| 39 |
return ret |
| 40 |
|
| 41 |
def extract_uid(self, data): |
| 42 |
return str(data.get("sub")) |
| 43 |
|
| 44 |
def extract_common_fields(self, data): |
| 45 |
return dict( |
| 46 |
email=data.get("email"), |
| 47 |
last_name=data.get("family_name"), |
| 48 |
first_name=data.get("given_name"), |
| 49 |
eppn=data.get("eppn"), |
| 50 |
) |
| 51 |
|
| 52 |
def extract_email_addresses(self, data): |
| 53 |
ret = [] |
| 54 |
email = data.get("email") |
| 55 |
if email and data.get("verified_email"): |
| 56 |
ret.append(EmailAddress(email=email, verified=True, primary=True)) |
| 57 |
return ret |
| 58 |
|
| 59 |
|
| 60 |
provider_classes = [CILogonProvider] |