mirror of
				https://github.com/Tivix/django-rest-auth.git
				synced 2025-11-04 09:37:35 +03:00 
			
		
		
		
	Add django-axes package with custom serializer
This commit is contained in:
		
							parent
							
								
									f0b96a8aa7
								
							
						
					
					
						commit
						3e8a7e308c
					
				| 
						 | 
					@ -31,7 +31,7 @@ INSTALLED_APPS = (
 | 
				
			||||||
    'django.contrib.auth',
 | 
					    'django.contrib.auth',
 | 
				
			||||||
    'django.contrib.contenttypes',
 | 
					    'django.contrib.contenttypes',
 | 
				
			||||||
    'django.contrib.sessions',
 | 
					    'django.contrib.sessions',
 | 
				
			||||||
    # 'django.contrib.messages',
 | 
					    'django.contrib.messages',
 | 
				
			||||||
    'django.contrib.staticfiles',
 | 
					    'django.contrib.staticfiles',
 | 
				
			||||||
    'django.contrib.sites',
 | 
					    'django.contrib.sites',
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,12 +39,15 @@ INSTALLED_APPS = (
 | 
				
			||||||
    'rest_framework.authtoken',
 | 
					    'rest_framework.authtoken',
 | 
				
			||||||
    'rest_auth',
 | 
					    'rest_auth',
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    'allauth',
 | 
					    'allauth',
 | 
				
			||||||
    'allauth.account',
 | 
					    'allauth.account',
 | 
				
			||||||
    'rest_auth.registration',
 | 
					    'rest_auth.registration',
 | 
				
			||||||
    'allauth.socialaccount',
 | 
					    'allauth.socialaccount',
 | 
				
			||||||
    'allauth.socialaccount.providers.facebook',
 | 
					    'allauth.socialaccount.providers.facebook',
 | 
				
			||||||
    'rest_framework_swagger',
 | 
					    'rest_framework_swagger',
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    'axes',
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
MIDDLEWARE = (
 | 
					MIDDLEWARE = (
 | 
				
			||||||
| 
						 | 
					@ -54,10 +57,9 @@ MIDDLEWARE = (
 | 
				
			||||||
    'django.contrib.auth.middleware.AuthenticationMiddleware',
 | 
					    'django.contrib.auth.middleware.AuthenticationMiddleware',
 | 
				
			||||||
    'django.contrib.messages.middleware.MessageMiddleware',
 | 
					    'django.contrib.messages.middleware.MessageMiddleware',
 | 
				
			||||||
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
 | 
					    'django.middleware.clickjacking.XFrameOptionsMiddleware',
 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# For backwards compatibility for Django 1.8
 | 
					    'axes.middleware.AxesMiddleware',
 | 
				
			||||||
MIDDLEWARE_CLASSES = MIDDLEWARE
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ROOT_URLCONF = 'demo.urls'
 | 
					ROOT_URLCONF = 'demo.urls'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -127,3 +129,27 @@ SWAGGER_SETTINGS = {
 | 
				
			||||||
    'LOGIN_URL': 'login',
 | 
					    'LOGIN_URL': 'login',
 | 
				
			||||||
    'LOGOUT_URL': 'logout',
 | 
					    'LOGOUT_URL': 'logout',
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					AUTHENTICATION_BACKENDS = [
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # AxesBackend should be the first backend in the AUTHENTICATION_BACKENDS list.
 | 
				
			||||||
 | 
					    'axes.backends.AxesBackend',
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Required for rest-auth when using Axes to prevent 'Unable to log in with provided credentials.'
 | 
				
			||||||
 | 
					    'allauth.account.auth_backends.AuthenticationBackend',
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Django ModelBackend is the default authentication backend.
 | 
				
			||||||
 | 
					    'django.contrib.auth.backends.ModelBackend',
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CACHES = {
 | 
				
			||||||
 | 
					    'default': {
 | 
				
			||||||
 | 
					        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
 | 
				
			||||||
 | 
					        'LOCATION': 'django_database_cache',
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					REST_AUTH_SERIALIZERS = {
 | 
				
			||||||
 | 
					    'LOGIN_SERIALIZER': 'myapp.serializers.RestAuthLoginSerializer',
 | 
				
			||||||
 | 
					    # 'TOKEN_SERIALIZER': 'path.to.custom.TokenSerializer',
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										0
									
								
								demo/myapp/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								demo/myapp/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										3
									
								
								demo/myapp/admin.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								demo/myapp/admin.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,3 @@
 | 
				
			||||||
 | 
					from django.contrib import admin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Register your models here.
 | 
				
			||||||
							
								
								
									
										5
									
								
								demo/myapp/apps.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								demo/myapp/apps.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,5 @@
 | 
				
			||||||
 | 
					from django.apps import AppConfig
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class MyappConfig(AppConfig):
 | 
				
			||||||
 | 
					    name = 'myapp'
 | 
				
			||||||
							
								
								
									
										0
									
								
								demo/myapp/migrations/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								demo/myapp/migrations/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										3
									
								
								demo/myapp/models.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								demo/myapp/models.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,3 @@
 | 
				
			||||||
 | 
					from django.db import models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Create your models here.
 | 
				
			||||||
							
								
								
									
										16
									
								
								demo/myapp/serializers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								demo/myapp/serializers.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,16 @@
 | 
				
			||||||
 | 
					from axes.helpers import get_lockout_message
 | 
				
			||||||
 | 
					from rest_auth import serializers
 | 
				
			||||||
 | 
					from rest_auth.serializers import LoginSerializer
 | 
				
			||||||
 | 
					from rest_framework import exceptions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# noinspection PyAbstractClass
 | 
				
			||||||
 | 
					class RestAuthLoginSerializer(LoginSerializer):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def validate(self, attrs):
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            attrs = super().validate(attrs)
 | 
				
			||||||
 | 
					        except exceptions.ValidationError:
 | 
				
			||||||
 | 
					            if getattr(self.context['request'], 'axes_locked_out', None):
 | 
				
			||||||
 | 
					                raise serializers.ValidationError(get_lockout_message())
 | 
				
			||||||
 | 
					        return attrs
 | 
				
			||||||
							
								
								
									
										3
									
								
								demo/myapp/tests.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								demo/myapp/tests.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,3 @@
 | 
				
			||||||
 | 
					from django.test import TestCase
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Create your tests here.
 | 
				
			||||||
							
								
								
									
										3
									
								
								demo/myapp/views.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								demo/myapp/views.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,3 @@
 | 
				
			||||||
 | 
					from django.shortcuts import render
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Create your views here.
 | 
				
			||||||
| 
						 | 
					@ -4,3 +4,4 @@ djangorestframework>=3.9.4
 | 
				
			||||||
django-allauth>=0.39.1
 | 
					django-allauth>=0.39.1
 | 
				
			||||||
six==1.12.0
 | 
					six==1.12.0
 | 
				
			||||||
django-rest-swagger==2.2.0
 | 
					django-rest-swagger==2.2.0
 | 
				
			||||||
 | 
					django-axes==5.0.7
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user