add throttle to request otp APIview
This commit is contained in:
parent
5faac7e63d
commit
224331ef5b
3 changed files with 33 additions and 1 deletions
|
|
@ -113,6 +113,13 @@ REST_FRAMEWORK = {
|
||||||
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
||||||
'PAGE_SIZE': PAGE_SIZE,
|
'PAGE_SIZE': PAGE_SIZE,
|
||||||
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
|
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
|
||||||
|
# 'DEFAULT_THROTTLE_CLASSES': [
|
||||||
|
# 'rest_framework.throttling.AnonRateThrottle',
|
||||||
|
# ],
|
||||||
|
'DEFAULT_THROTTLE_RATES': {
|
||||||
|
'otp_min': '1/min',
|
||||||
|
'otp_day': '5/day',
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SPECTACULAR_SETTINGS = {
|
SPECTACULAR_SETTINGS = {
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ from apps.users.models import User
|
||||||
from apps.users.provinces_and_cities import State
|
from apps.users.provinces_and_cities import State
|
||||||
from apps.users.serializers import PublicUserSerializer, AccountSerializer, RequestOTPSerializer, RequestOTTSerializer, \
|
from apps.users.serializers import PublicUserSerializer, AccountSerializer, RequestOTPSerializer, RequestOTTSerializer, \
|
||||||
ChangePasswordSerializer, UserInquirySerializer
|
ChangePasswordSerializer, UserInquirySerializer
|
||||||
|
from utils.throttles import RequestOTPDayRateThrottle, RequestOTPMinRateThrottle
|
||||||
|
|
||||||
UserModel = get_user_model()
|
UserModel = get_user_model()
|
||||||
|
|
||||||
|
|
@ -84,7 +85,7 @@ class RequestOTPView(generics.CreateAPIView):
|
||||||
permission_classes = []
|
permission_classes = []
|
||||||
serializer_class = RequestOTPSerializer
|
serializer_class = RequestOTPSerializer
|
||||||
required_scopes = []
|
required_scopes = []
|
||||||
|
throttle_classes = [RequestOTPMinRateThrottle, RequestOTPDayRateThrottle]
|
||||||
|
|
||||||
class RequestOTTView(generics.CreateAPIView):
|
class RequestOTTView(generics.CreateAPIView):
|
||||||
permission_classes = [IsAuthenticatedOrTokenHasScope]
|
permission_classes = [IsAuthenticatedOrTokenHasScope]
|
||||||
|
|
|
||||||
24
utils/throttles.py
Normal file
24
utils/throttles.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
from rest_framework.throttling import SimpleRateThrottle
|
||||||
|
|
||||||
|
|
||||||
|
class RequestOTPDayRateThrottle(SimpleRateThrottle):
|
||||||
|
scope = 'otp_day'
|
||||||
|
|
||||||
|
def get_cache_key(self, request, view):
|
||||||
|
if request.user and request.user.is_authenticated:
|
||||||
|
ident = request.user.pk
|
||||||
|
else:
|
||||||
|
ident = self.get_ident(request)
|
||||||
|
|
||||||
|
return self.cache_format % {
|
||||||
|
'scope': self.scope,
|
||||||
|
'ident': ident
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class RequestOTPMinRateThrottle(RequestOTPDayRateThrottle):
|
||||||
|
scope = 'otp_min'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Reference in a new issue