deploy login & reCaptcha
This commit is contained in:
0
apps/authentication/api/__init__.py
Normal file
0
apps/authentication/api/__init__.py
Normal file
0
apps/authentication/api/v1/__init__.py
Normal file
0
apps/authentication/api/v1/__init__.py
Normal file
27
apps/authentication/api/v1/api.py
Normal file
27
apps/authentication/api/v1/api.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from apps.authentication.api.v1.serializers.jwt import CustomizedTokenObtainPairSerializer
|
||||
from rest_framework_simplejwt.views import TokenObtainPairView
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
from rest_framework.decorators import action
|
||||
from apps.authentication.models import User
|
||||
from django.db import transaction
|
||||
|
||||
|
||||
class CustomizedTokenObtainPairView(TokenObtainPairView):
|
||||
serializer_class = CustomizedTokenObtainPairSerializer
|
||||
|
||||
|
||||
class Authentication(ModelViewSet):
|
||||
queryset = User
|
||||
serializer_class = ''
|
||||
permission_classes = ''
|
||||
|
||||
@action(
|
||||
methods=['post', ],
|
||||
detail=False,
|
||||
name='login',
|
||||
url_name='login',
|
||||
url_path='login'
|
||||
)
|
||||
@transaction.atomic
|
||||
def login(self, request):
|
||||
pass
|
||||
0
apps/authentication/api/v1/serializers/__init__.py
Normal file
0
apps/authentication/api/v1/serializers/__init__.py
Normal file
55
apps/authentication/api/v1/serializers/jwt.py
Normal file
55
apps/authentication/api/v1/serializers/jwt.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
|
||||
from apps.captcha_app import exceptions as captcha_exception
|
||||
from rest_framework_simplejwt.settings import api_settings
|
||||
from django.contrib.auth.models import update_last_login
|
||||
from rest_framework import exceptions
|
||||
from django.core.cache import cache
|
||||
from typing import Any
|
||||
|
||||
|
||||
class CustomizedTokenObtainPairSerializer(TokenObtainPairSerializer): # noqa
|
||||
"""
|
||||
customize jwt token
|
||||
'set new variables in generated token'
|
||||
"""
|
||||
|
||||
def validate(self, attrs: dict[str, Any]) -> dict[str, str]:
|
||||
"""
|
||||
override validate method to add more conditions
|
||||
"""
|
||||
captcha_code, captcha_key = attrs['captcha_code'], attrs['captcha_key']
|
||||
|
||||
if captcha_code != cache.get(captcha_key) or captcha_code not in attrs.keys():
|
||||
raise captcha_exception.CaptchaFailed()
|
||||
|
||||
data = super().validate(attrs)
|
||||
|
||||
refresh = self.get_token(self.user)
|
||||
|
||||
data["refresh"] = str(refresh)
|
||||
data["access"] = str(refresh.access_token)
|
||||
data["otp_status"] = self.user.otp_status
|
||||
|
||||
if not self.user.is_active:
|
||||
raise exceptions.AuthenticationFailed(
|
||||
self.error_messages["no_active_account"],
|
||||
"no_active_account",
|
||||
)
|
||||
|
||||
if api_settings.UPDATE_LAST_LOGIN:
|
||||
update_last_login(None, self.user)
|
||||
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def get_token(cls, user):
|
||||
"""
|
||||
set variables in encoded jwt token
|
||||
"""
|
||||
|
||||
token = super().get_token(user)
|
||||
|
||||
# Add custom claims
|
||||
token['name'] = user.username
|
||||
|
||||
return token
|
||||
14
apps/authentication/api/v1/urls.py
Normal file
14
apps/authentication/api/v1/urls.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from rest_framework_simplejwt.views import (
|
||||
TokenObtainPairView,
|
||||
TokenRefreshView,
|
||||
TokenVerifyView
|
||||
)
|
||||
from .api import CustomizedTokenObtainPairView
|
||||
|
||||
urlpatterns = [
|
||||
path('login/', CustomizedTokenObtainPairView.as_view(), name='token_obtain_pair'),
|
||||
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
||||
path('token/verify/', TokenVerifyView.as_view(), name='token_verify'),
|
||||
]
|
||||
Reference in New Issue
Block a user