mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2024-11-22 09:06:40 +03:00
Add support for custom Token model
This commit is contained in:
parent
4a56a9e7e5
commit
c9d55f768c
|
@ -29,10 +29,12 @@ Configuration
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- **REST_AUTH_TOKEN_MODEL** - model class for tokens, default value ``rest_framework.authtoken.models``
|
||||||
|
|
||||||
|
- **REST_AUTH_TOKEN_CREATOR** - callable to create tokens, default value ``rest_auth.utils.default_create_token``.
|
||||||
|
|
||||||
- **REST_SESSION_LOGIN** - Enable session login in Login API view (default: True)
|
- **REST_SESSION_LOGIN** - Enable session login in Login API view (default: True)
|
||||||
|
|
||||||
|
|
||||||
- **OLD_PASSWORD_FIELD_ENABLED** - set it to True if you want to have old password verification on password change enpoint (default: False)
|
- **OLD_PASSWORD_FIELD_ENABLED** - set it to True if you want to have old password verification on password change enpoint (default: False)
|
||||||
|
|
||||||
- **LOGOUT_ON_PASSWORD_CHANGE** - set to False if you want to keep the current user logged in after a password change
|
- **LOGOUT_ON_PASSWORD_CHANGE** - set to False if you want to keep the current user logged in after a password change
|
||||||
|
|
|
@ -7,8 +7,10 @@ from rest_auth.serializers import (
|
||||||
PasswordResetSerializer as DefaultPasswordResetSerializer,
|
PasswordResetSerializer as DefaultPasswordResetSerializer,
|
||||||
PasswordResetConfirmSerializer as DefaultPasswordResetConfirmSerializer,
|
PasswordResetConfirmSerializer as DefaultPasswordResetConfirmSerializer,
|
||||||
PasswordChangeSerializer as DefaultPasswordChangeSerializer)
|
PasswordChangeSerializer as DefaultPasswordChangeSerializer)
|
||||||
from .utils import import_callable
|
from .utils import import_callable, default_create_token
|
||||||
|
|
||||||
|
create_token = import_callable(
|
||||||
|
getattr(settings, 'REST_AUTH_TOKEN_CREATOR', default_create_token))
|
||||||
|
|
||||||
serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {})
|
serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {})
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,10 @@
|
||||||
# from django.db import models
|
from django.conf import settings
|
||||||
|
|
||||||
|
from rest_framework.authtoken.models import Token as DefaultTokenModel
|
||||||
|
|
||||||
|
from .utils import import_callable
|
||||||
|
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
|
|
||||||
|
TokenModel = import_callable(
|
||||||
|
getattr(settings, 'REST_AUTH_TOKEN_MODEL', DefaultTokenModel))
|
||||||
|
|
|
@ -3,7 +3,6 @@ from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.permissions import AllowAny
|
from rest_framework.permissions import AllowAny
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.authtoken.models import Token
|
|
||||||
|
|
||||||
from allauth.account.views import SignupView, ConfirmEmailView
|
from allauth.account.views import SignupView, ConfirmEmailView
|
||||||
from allauth.account.utils import complete_signup
|
from allauth.account.utils import complete_signup
|
||||||
|
@ -12,6 +11,7 @@ from allauth.account import app_settings
|
||||||
from rest_auth.app_settings import TokenSerializer
|
from rest_auth.app_settings import TokenSerializer
|
||||||
from rest_auth.registration.serializers import SocialLoginSerializer
|
from rest_auth.registration.serializers import SocialLoginSerializer
|
||||||
from rest_auth.views import LoginView
|
from rest_auth.views import LoginView
|
||||||
|
from rest_auth.models import TokenModel
|
||||||
|
|
||||||
|
|
||||||
class RegisterView(APIView, SignupView):
|
class RegisterView(APIView, SignupView):
|
||||||
|
@ -27,7 +27,7 @@ class RegisterView(APIView, SignupView):
|
||||||
|
|
||||||
permission_classes = (AllowAny,)
|
permission_classes = (AllowAny,)
|
||||||
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
||||||
token_model = Token
|
token_model = TokenModel
|
||||||
serializer_class = TokenSerializer
|
serializer_class = TokenSerializer
|
||||||
|
|
||||||
def get(self, *args, **kwargs):
|
def get(self, *args, **kwargs):
|
||||||
|
|
|
@ -6,8 +6,9 @@ from django.utils.http import urlsafe_base64_decode as uid_decoder
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
|
|
||||||
|
from .models import TokenModel
|
||||||
|
|
||||||
from rest_framework import serializers, exceptions
|
from rest_framework import serializers, exceptions
|
||||||
from rest_framework.authtoken.models import Token
|
|
||||||
from rest_framework.exceptions import ValidationError
|
from rest_framework.exceptions import ValidationError
|
||||||
|
|
||||||
# Get the UserModel
|
# Get the UserModel
|
||||||
|
@ -84,7 +85,7 @@ class TokenSerializer(serializers.ModelSerializer):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Token
|
model = TokenModel
|
||||||
fields = ('key',)
|
fields = ('key',)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -9,3 +9,9 @@ def import_callable(path_or_callable):
|
||||||
assert isinstance(path_or_callable, string_types)
|
assert isinstance(path_or_callable, string_types)
|
||||||
package, attr = path_or_callable.rsplit('.', 1)
|
package, attr = path_or_callable.rsplit('.', 1)
|
||||||
return getattr(import_module(package), attr)
|
return getattr(import_module(package), attr)
|
||||||
|
|
||||||
|
|
||||||
|
def default_create_token(token_model, serializer):
|
||||||
|
user = serializer.validated_data['user']
|
||||||
|
token, _ = token_model.objects.get_or_create(user=user)
|
||||||
|
return token
|
||||||
|
|
|
@ -7,14 +7,14 @@ from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.generics import GenericAPIView
|
from rest_framework.generics import GenericAPIView
|
||||||
from rest_framework.permissions import IsAuthenticated, AllowAny
|
from rest_framework.permissions import IsAuthenticated, AllowAny
|
||||||
from rest_framework.authtoken.models import Token
|
|
||||||
from rest_framework.generics import RetrieveUpdateAPIView
|
from rest_framework.generics import RetrieveUpdateAPIView
|
||||||
|
|
||||||
from .app_settings import (
|
from .app_settings import (
|
||||||
TokenSerializer, UserDetailsSerializer, LoginSerializer,
|
TokenSerializer, UserDetailsSerializer, LoginSerializer,
|
||||||
PasswordResetSerializer, PasswordResetConfirmSerializer,
|
PasswordResetSerializer, PasswordResetConfirmSerializer,
|
||||||
PasswordChangeSerializer
|
PasswordChangeSerializer, create_token
|
||||||
)
|
)
|
||||||
|
from .models import TokenModel
|
||||||
|
|
||||||
|
|
||||||
class LoginView(GenericAPIView):
|
class LoginView(GenericAPIView):
|
||||||
|
@ -30,13 +30,12 @@ class LoginView(GenericAPIView):
|
||||||
"""
|
"""
|
||||||
permission_classes = (AllowAny,)
|
permission_classes = (AllowAny,)
|
||||||
serializer_class = LoginSerializer
|
serializer_class = LoginSerializer
|
||||||
token_model = Token
|
token_model = TokenModel
|
||||||
response_serializer = TokenSerializer
|
response_serializer = TokenSerializer
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
self.user = self.serializer.validated_data['user']
|
self.user = self.serializer.validated_data['user']
|
||||||
self.token, created = self.token_model.objects.get_or_create(
|
self.token = create_token(self.token_model, self.serializer)
|
||||||
user=self.user)
|
|
||||||
if getattr(settings, 'REST_SESSION_LOGIN', True):
|
if getattr(settings, 'REST_SESSION_LOGIN', True):
|
||||||
login(self.request, self.user)
|
login(self.request, self.user)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user