<commit_before>from rest_framework import serializers

from social_core import exceptions
from social_django.utils import load_backend, load_strategy

from djoser.conf import settings


class ProviderAuthSerializer(serializers.Serializer):
    # GET auth token
    token = serializers.CharField(read_only=True)
    user = serializers.CharField(read_only=True)

    # POST OAuth/OpenID values
    code = serializers.CharField(write_only=True)
    state = serializers.CharField(required=False, write_only=True)

    def create(self, validated_data):
        user = validated_data['user']
        return settings.SOCIAL_AUTH_TOKEN_STRATEGY.obtain(user)

    def validate_state(self, value):
        strategy = load_strategy(self.context['request'])
        redirect_uri = strategy.session_get('redirect_uri')

        backend_name = self.context['view'].kwargs['provider']
        backend = load_backend(
            strategy, backend_name, redirect_uri=redirect_uri
        )

        try:
            backend.validate_state()
        except exceptions.AuthException:
            raise serializers.ValidationError('State could not be verified.')

    def validate(self, attrs):
        strategy = load_strategy(self.context['request'])
        redirect_uri = strategy.session_get('redirect_uri')

        backend_name = self.context['view'].kwargs['provider']
        backend = load_backend(
            strategy, backend_name, redirect_uri=redirect_uri
        )

        try:
            user = backend.auth_complete()
        except exceptions.AuthException:
            raise serializers.ValidationError(
                'Failed to finish authentication.'
            )
        return {'user': user}
<commit_msg>Fix issue with request data not available<commit_after>from rest_framework import serializers

from social_core import exceptions
from social_django.utils import load_backend, load_strategy

from djoser.conf import settings


class ProviderAuthSerializer(serializers.Serializer):
    # GET auth token
    token = serializers.CharField(read_only=True)
    user = serializers.CharField(read_only=True)

    # POST OAuth/OpenID values
    code = serializers.CharField(write_only=True)
    state = serializers.CharField(required=False, write_only=True)

    def create(self, validated_data):
        user = validated_data['user']
        return settings.SOCIAL_AUTH_TOKEN_STRATEGY.obtain(user)

    def validate_state(self, value):
        strategy = load_strategy(self.context['request'])
        redirect_uri = strategy.session_get('redirect_uri')

        backend_name = self.context['view'].kwargs['provider']
        backend = load_backend(
            strategy, backend_name, redirect_uri=redirect_uri
        )

        try:
            backend.validate_state()
        except exceptions.AuthException:
            raise serializers.ValidationError('State could not be verified.')

    def validate(self, attrs):
        # Dirty hack because PSA does not respect request.data
        request = self.context['request']
        request.GET = request.data

        strategy = load_strategy(self.context['request'])
        redirect_uri = strategy.session_get('redirect_uri')

        backend_name = self.context['view'].kwargs['provider']
        backend = load_backend(
            strategy, backend_name, redirect_uri=redirect_uri
        )

        try:
            user = backend.auth_complete()
        except exceptions.AuthException:
            raise serializers.ValidationError(
                'Failed to finish authentication.'
            )
        return {'user': user}

{'fraction_non_alphanumeric': 0.08534890186266333, 'fraction_numerical': 0.0, 'mean_word_length': 3.0201117318435755, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '637701', 'n_tokens_mistral': 952, 'n_tokens_neox': 931, 'n_words': 204}