M
M
Maxim Tarabrin2019-08-12 19:04:10
Django
Maxim Tarabrin, 2019-08-12 19:04:10

How to pass data during Oauth2 authorization using django-allauth?

Friends, I have oauth2 authorization using django-allauth. Written custom provider. I don’t understand how to send data during authorization to the User model?

provider

class ProjectProvider(OAuth2Provider):
    id = 'project'
    name = 'Project'
    account_class = ProjectAccount

    def extract_uid(self, data):
        try:
            return data['user_id']
        except KeyError:
            raise AuthExtractError('Error on extract user_id from response userinfo')

    def extract_common_fields(self, data):
        try:
            return dict(
                email=data['email'],
                username=data['username'],
                user_id=data['user_id'], # Не работает...
            )
        except KeyError:
            raise AuthExtractError('Error on extract user_id from response userinfo')

    def get_default_scope(self):
        scope = ["read write"]
        return scope


adapter

class ProjectAdapter(OAuth2Adapter):
    provider_id = ProjectProvider.id
    provider_base_url = settings.PROVIDER_AUTH_HOST

    @property
    def access_token_url(self):
        return '{0}/oauth2/access_token'.format(self.provider_base_url)

    @property
    def authorize_url(self):
        return '{0}/oauth2/authorize'.format(self.provider_base_url)

    @property
    def profile_url(self):
        return '{0}/api/userinfo'.format(self.provider_base_url)

    @backoff.on_exception(backoff.expo,
                          requests.exceptions.RequestException,
                          max_tries=5,
                          )
    def complete_login(self, request, app, token, response):
        response_profile = requests.get(self.profile_url, params={
            'access_token': token.token
        })
        if response_profile.status_code == 200:
            return self.get_provider().sociallogin_from_response(
                request,
                response_profile.json()
            )
        else:
            raise AuthLoginError('Error on request to api userinfo')


oauth2_login = OAuth2LoginView.adapter_view(ProjectAdapter)
oauth2_callback = OAuth2CallbackView.adapter_view(ProjectAdapter)


According to the result, I want that when authorizing through a third-party service, I can pass the data to the User model and already use this data.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question