from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider


class KeycloakAccount(ProviderAccount):

    def get_avatar_url(self):
        return self.account.extra_data.get('picture')

    def to_str(self):
        dflt = super(KeycloakAccount, self).to_str()
        return self.account.extra_data.get('name', dflt)


class KeycloakProvider(OAuth2Provider):
    id = 'keycloak'
    name = 'Keycloak'
    account_class = KeycloakAccount

    def get_default_scope(self):
        return ['openid', 'profile', 'email']

    def extract_uid(self, data):
        return str(data['id'])

    def extract_common_fields(self, data):
        return dict(
            email=data.get('email'),
            username=data.get('username'),
            name=data.get('name'),
            user_id=data.get('user_id'),
            picture=data.get('picture'),
        )


provider_classes = [KeycloakProvider]

from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider


class KeycloakAccount(ProviderAccount):

    def get_avatar_url(self):
        return self.account.extra_data.get('picture')

    def to_str(self):
        dflt = super(KeycloakAccount, self).to_str()
        return self.account.extra_data.get('name', dflt)


class KeycloakProvider(OAuth2Provider):
    id = 'keycloak'
    name = 'Keycloak'
    account_class = KeycloakAccount

    def get_default_scope(self):
        return ['openid', 'profile', 'email']

    def extract_uid(self, data):
        return str(data['id'])

    def extract_common_fields(self, data):
        return dict(
            email=data.get('email'),
            username=data.get('preferred_username'),
            name=data.get('name'),
            user_id=data.get('user_id'),
            picture=data.get('picture'),
        )


provider_classes = [KeycloakProvider]

Use preferred_username claim for username
fix(keycloak): Use preferred_username claim for username

As per the OpenID Connect spec the standard username claim is
`preferred_username`.

By default Keycloak confirms to OpenID Connect spec and provides a
`preferred_username` claim, but no `username` claim in the profile
scope.

ref: https://openid.net/specs/openid-connect-basic-1_0-28.html#StandardClaims

  from allauth.socialaccount.providers.base import ProviderAccount
  from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
  
  
  class KeycloakAccount(ProviderAccount):
  
      def get_avatar_url(self):
          return self.account.extra_data.get('picture')
  
      def to_str(self):
          dflt = super(KeycloakAccount, self).to_str()
          return self.account.extra_data.get('name', dflt)
  
  
  class KeycloakProvider(OAuth2Provider):
      id = 'keycloak'
      name = 'Keycloak'
      account_class = KeycloakAccount
  
      def get_default_scope(self):
          return ['openid', 'profile', 'email']
  
      def extract_uid(self, data):
          return str(data['id'])
  
      def extract_common_fields(self, data):
          return dict(
              email=data.get('email'),
-             username=data.get('username'),
+             username=data.get('preferred_username'),
              name=data.get('name'),
              user_id=data.get('user_id'),
              picture=data.get('picture'),
          )
  
  
  provider_classes = [KeycloakProvider]
  
Use preferred_username claim for username
## Code Before:
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider


class KeycloakAccount(ProviderAccount):

    def get_avatar_url(self):
        return self.account.extra_data.get('picture')

    def to_str(self):
        dflt = super(KeycloakAccount, self).to_str()
        return self.account.extra_data.get('name', dflt)


class KeycloakProvider(OAuth2Provider):
    id = 'keycloak'
    name = 'Keycloak'
    account_class = KeycloakAccount

    def get_default_scope(self):
        return ['openid', 'profile', 'email']

    def extract_uid(self, data):
        return str(data['id'])

    def extract_common_fields(self, data):
        return dict(
            email=data.get('email'),
            username=data.get('username'),
            name=data.get('name'),
            user_id=data.get('user_id'),
            picture=data.get('picture'),
        )


provider_classes = [KeycloakProvider]

## Instruction:
Use preferred_username claim for username
## Code After:
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider


class KeycloakAccount(ProviderAccount):

    def get_avatar_url(self):
        return self.account.extra_data.get('picture')

    def to_str(self):
        dflt = super(KeycloakAccount, self).to_str()
        return self.account.extra_data.get('name', dflt)


class KeycloakProvider(OAuth2Provider):
    id = 'keycloak'
    name = 'Keycloak'
    account_class = KeycloakAccount

    def get_default_scope(self):
        return ['openid', 'profile', 'email']

    def extract_uid(self, data):
        return str(data['id'])

    def extract_common_fields(self, data):
        return dict(
            email=data.get('email'),
            username=data.get('preferred_username'),
            name=data.get('name'),
            user_id=data.get('user_id'),
            picture=data.get('picture'),
        )


provider_classes = [KeycloakProvider]
