import logging from allauth.socialaccount import providers from allauth.socialaccount.providers.base import ProviderAccount from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider from allauth.account.models import EmailAddress from . import app_settings log = logging.getLogger(__name__) class Authentic2Account(ProviderAccount): def to_str(self): return self.account.uid class Authentic2Provider(OAuth2Provider): id = 'authentic2' name = 'Authentic2' package = 'portail_citoyen2.allauth_authentic2' account_class = Authentic2Account def extract_uid(self, data): return str(data['username']) def extract_common_fields(self, data): return dict(email=data.get('email'), username=data.get('username'), first_name=data.get('first_name'), last_name=data.get('last_name')) def extract_email_addresses(self, data): ret = [EmailAddress(email=data['email'], verified=True, primary=True)] return ret def sociallogin_from_response(self, request, response): sociallogin = super(Authentic2Provider, self).sociallogin_from_response( request, response) if app_settings.ADMIN_ROLE in response.get('role', []): log.debug('giving admin role to user') sociallogin.account.user.is_superuser = True sociallogin.account.user.is_staff = True return sociallogin providers.registry.register(Authentic2Provider)