# Path: rest_social_auth/serializers.py
# class JWTPairSerializer(JWTBaseSerializer):
#     token = serializers.SerializerMethodField()
#     refresh = serializers.SerializerMethodField()
# 
#     jwt_token_class_name = 'RefreshToken'
# 
#     def get_token(self, obj):
#         return str(self.get_token_instance().access_token)
# 
#     def get_refresh(self, obj):
#         return str(self.get_token_instance())
# 
# class JWTSlidingSerializer(JWTBaseSerializer):
#     token = serializers.SerializerMethodField()
# 
#     jwt_token_class_name = 'SlidingToken'
# 
#     def get_token(self, obj):
#         return str(self.get_token_instance())
# 
# class KnoxSerializer(TokenSerializer):
#     def get_token(self, obj):
#         try:
#             from knox.models import AuthToken
#         except ImportError:
#             warnings.warn(
#                 'django-rest-knox must be installed for Knox authentication',
#                 ImportWarning,
#             )
#             raise
# 
#         token_instance, token_key = AuthToken.objects.create(obj)
#         return token_key
# 
# class OAuth1InputSerializer(serializers.Serializer):
# 
#     provider = serializers.CharField(required=False)
#     oauth_token = serializers.CharField()
#     oauth_verifier = serializers.CharField()
# 
# class OAuth2InputSerializer(serializers.Serializer):
# 
#     provider = serializers.CharField(required=False)
#     code = serializers.CharField()
#     redirect_uri = serializers.CharField(required=False)
# 
# class TokenSerializer(serializers.Serializer):
# 
#     token = serializers.SerializerMethodField()
# 
#     def get_token(self, obj):
#         token, created = Token.objects.get_or_create(user=obj)
#         return token.key
# 
# class UserJWTSlidingSerializer(JWTSlidingSerializer, UserSerializer):
# 
#     def get_token_payload(self, user):
#         payload = dict(UserSerializer(user).data)
#         payload.pop('id', None)
#         return payload
# 
# class UserKnoxSerializer(KnoxSerializer, UserSerializer):
#     pass
# 
# class UserJWTPairSerializer(JWTPairSerializer, UserSerializer):
# 
#     def get_token_payload(self, user):
#         payload = dict(UserSerializer(user).data)
#         payload.pop('id', None)
#         return payload
# 
# class UserSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = get_user_model()
#         # Custom user model may not have some fields from the list below,
#         # excluding them based on actual fields
#         exclude = [
#             field for field in (
#                 'is_staff', 'is_active', 'date_joined', 'password', 'last_login',
#                 'user_permissions', 'groups', 'is_superuser',
#             ) if field in [mfield.name for mfield in get_user_model()._meta.get_fields()]
#         ]
# 
# class UserTokenSerializer(TokenSerializer, UserSerializer):
#     pass


import logging
import warnings
    from urllib.parse import urljoin, urlencode, urlparse  # python 3x
    from urllib import urlencode  # python 2x
    from urlparse import urljoin, urlparse
from django.conf import settings
from django.http import HttpResponse
from django.utils.decorators import method_decorator
from django.utils.encoding import iri_to_uri
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_protect
from social_django.utils import psa, STORAGE
from social_django.views import _do_login as social_auth_login
from social_core.backends.oauth import BaseOAuth1
from social_core.utils import get_strategy, parse_qs, user_is_authenticated, setting_name
from social_core.exceptions import AuthException
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import AllowAny
from requests.exceptions import HTTPError
from .serializers import (
    JWTPairSerializer,
    JWTSlidingSerializer,
    KnoxSerializer,
    OAuth1InputSerializer,
    OAuth2InputSerializer,
    TokenSerializer,
    UserJWTSlidingSerializer,
    UserKnoxSerializer,
    UserJWTPairSerializer,
    UserSerializer,
    UserTokenSerializer,
)
            from knox.auth import TokenAuthentication
            from rest_framework_simplejwt.authentication import JWTAuthentication
    def log_exception(self, error):
        err_msg = error.args[0] if error.args else ''
        if getattr(error, 'response', None) is not None:
            try:
                err_data = error.response.json()
            except (ValueError, AttributeError):
                logger.error(u'%s; %s', error, err_msg)
            else:
                logger.error(u'%s; %s; %s', error, err_msg, err_data)
        else:
            logger.exception(u'%s; %s', error, err_msg)


class SocialSessionAuthView(BaseSocialAuthView):
    serializer_class = UserSerializer

    def do_login(self, backend, user):
        social_auth_login(backend, user, user.social_user)

    @method_decorator(csrf_protect)  # just to be sure csrf is not disabled
    def post(self, request, *args, **kwargs):
        return super(SocialSessionAuthView, self).post(request, *args, **kwargs)


class SocialTokenOnlyAuthView(BaseSocialAuthView):
    serializer_class = TokenSerializer
    authentication_classes = (TokenAuthentication, )


class SocialTokenUserAuthView(BaseSocialAuthView):

# Path: rest_social_auth/serializers.py
# class JWTPairSerializer(JWTBaseSerializer):
#     token = serializers.SerializerMethodField()
#     refresh = serializers.SerializerMethodField()
# 
#     jwt_token_class_name = 'RefreshToken'
# 
#     def get_token(self, obj):
#         return str(self.get_token_instance().access_token)
# 
#     def get_refresh(self, obj):
#         return str(self.get_token_instance())
# 
# class JWTSlidingSerializer(JWTBaseSerializer):
#     token = serializers.SerializerMethodField()
# 
#     jwt_token_class_name = 'SlidingToken'
# 
#     def get_token(self, obj):
#         return str(self.get_token_instance())
# 
# class KnoxSerializer(TokenSerializer):
#     def get_token(self, obj):
#         try:
#             from knox.models import AuthToken
#         except ImportError:
#             warnings.warn(
#                 'django-rest-knox must be installed for Knox authentication',
#                 ImportWarning,
#             )
#             raise
# 
#         token_instance, token_key = AuthToken.objects.create(obj)
#         return token_key
# 
# class OAuth1InputSerializer(serializers.Serializer):
# 
#     provider = serializers.CharField(required=False)
#     oauth_token = serializers.CharField()
#     oauth_verifier = serializers.CharField()
# 
# class OAuth2InputSerializer(serializers.Serializer):
# 
#     provider = serializers.CharField(required=False)
#     code = serializers.CharField()
#     redirect_uri = serializers.CharField(required=False)
# 
# class TokenSerializer(serializers.Serializer):
# 
#     token = serializers.SerializerMethodField()
# 
#     def get_token(self, obj):
#         token, created = Token.objects.get_or_create(user=obj)
#         return token.key
# 
# class UserJWTSlidingSerializer(JWTSlidingSerializer, UserSerializer):
# 
#     def get_token_payload(self, user):
#         payload = dict(UserSerializer(user).data)
#         payload.pop('id', None)
#         return payload
# 
# class UserKnoxSerializer(KnoxSerializer, UserSerializer):
#     pass
# 
# class UserJWTPairSerializer(JWTPairSerializer, UserSerializer):
# 
#     def get_token_payload(self, user):
#         payload = dict(UserSerializer(user).data)
#         payload.pop('id', None)
#         return payload
# 
# class UserSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = get_user_model()
#         # Custom user model may not have some fields from the list below,
#         # excluding them based on actual fields
#         exclude = [
#             field for field in (
#                 'is_staff', 'is_active', 'date_joined', 'password', 'last_login',
#                 'user_permissions', 'groups', 'is_superuser',
#             ) if field in [mfield.name for mfield in get_user_model()._meta.get_fields()]
#         ]
# 
# class UserTokenSerializer(TokenSerializer, UserSerializer):
#     pass


# Path: rest_social_auth/views.py
import logging
import warnings
    from urllib.parse import urljoin, urlencode, urlparse  # python 3x
    from urllib import urlencode  # python 2x
    from urlparse import urljoin, urlparse
from django.conf import settings
from django.http import HttpResponse
from django.utils.decorators import method_decorator
from django.utils.encoding import iri_to_uri
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_protect
from social_django.utils import psa, STORAGE
from social_django.views import _do_login as social_auth_login
from social_core.backends.oauth import BaseOAuth1
from social_core.utils import get_strategy, parse_qs, user_is_authenticated, setting_name
from social_core.exceptions import AuthException
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import AllowAny
from requests.exceptions import HTTPError
from .serializers import (
    JWTPairSerializer,
    JWTSlidingSerializer,
    KnoxSerializer,
    OAuth1InputSerializer,
    OAuth2InputSerializer,
    TokenSerializer,
    UserJWTSlidingSerializer,
    UserKnoxSerializer,
    UserJWTPairSerializer,
    UserSerializer,
    UserTokenSerializer,
)
            from knox.auth import TokenAuthentication
            from rest_framework_simplejwt.authentication import JWTAuthentication

    def log_exception(self, error):
        err_msg = error.args[0] if error.args else ''
        if getattr(error, 'response', None) is not None:
            try:
                err_data = error.response.json()
            except (ValueError, AttributeError):
                logger.error(u'%s; %s', error, err_msg)
            else:
                logger.error(u'%s; %s; %s', error, err_msg, err_data)
        else:
            logger.exception(u'%s; %s', error, err_msg)


class SocialSessionAuthView(BaseSocialAuthView):
    serializer_class = UserSerializer

    def do_login(self, backend, user):
        social_auth_login(backend, user, user.social_user)

    @method_decorator(csrf_protect)  # just to be sure csrf is not disabled
    def post(self, request, *args, **kwargs):
        return super(SocialSessionAuthView, self).post(request, *args, **kwargs)


class SocialTokenOnlyAuthView(BaseSocialAuthView):
    serializer_class = TokenSerializer
    authentication_classes = (TokenAuthentication, )


class SocialTokenUserAuthView(BaseSocialAuthView):

    serializer_class = UserTokenSerializer