1919from rest_framework .permissions import AllowAny , IsAuthenticated
2020from rest_framework .response import Response
2121from rest_framework .views import APIView
22- from rest_framework_simplejwt .tokens import RefreshToken
22+ from rest_framework_simplejwt .tokens import RefreshToken , TokenError
2323
2424from core .permissions import IsOwnerOrReadOnly
2525from core .utils import Email
@@ -145,13 +145,13 @@ def get(self, request):
145145 except jwt .ExpiredSignatureError :
146146 return redirect (
147147 REDIRECT_URL ,
148- status = status .HTTP_200_OK ,
148+ status = status .HTTP_400_BAD_REQUEST ,
149149 message = "Activate Expired" ,
150150 )
151151 except jwt .DecodeError :
152152 return redirect (
153153 REDIRECT_URL ,
154- status = status .HTTP_200_OK ,
154+ status = status .HTTP_400_BAD_REQUEST ,
155155 message = "Decode error" ,
156156 )
157157
@@ -198,24 +198,43 @@ class ResetPassword(UpdateAPIView):
198198 serializer_class = PasswordSerializer
199199 permission_classes = [AllowAny ]
200200
201+ def get (self , request , * args , ** kwargs ):
202+ refresh_token = request .GET .get ("refresh_token" )
203+ try :
204+ RefreshToken (refresh_token ).check_blacklist ()
205+ except TokenError :
206+ return redirect (
207+ "https://procollab.ru/auth/reset_password/" ,
208+ status = status .HTTP_400_BAD_REQUEST ,
209+ message = "Used token" ,
210+ )
211+
212+ return Response ({"message" : "Enter new password" })
213+
201214 def update (self , request , * args , ** kwargs ):
202215 serializer = self .get_serializer (data = request .data )
203216 serializer .is_valid ()
204217
205218 try :
206- token = request .GET .get ("access_token" )
207- payload = jwt .decode (jwt = token , key = settings .SECRET_KEY , algorithms = ["HS256" ])
219+ refresh_token = request .GET .get ("refresh_token" )
220+ access_token = request .GET .get ("access_token" )
221+ payload = jwt .decode (
222+ jwt = access_token , key = settings .SECRET_KEY , algorithms = ["HS256" ]
223+ )
208224 user = User .objects .get (id = payload ["user_id" ])
209225 last_update = user .datetime_updated
210- if (datetime .now ().minute - last_update .minute ) <= 10 :
211- return Response (
212- {"response" : "You can't change your password so often" },
213- status = status .HTTP_200_OK ,
226+ frequency_update = datetime .utcnow ().minute - last_update .minute
227+ if frequency_update <= 10 :
228+ return redirect (
229+ "https://procollab.ru/auth/reset_password/" ,
230+ status = status .HTTP_400_BAD_REQUEST ,
231+ message = "You can't change your password so often" ,
214232 )
215233
216234 user .set_password (serializer .data ["new_password" ])
217235 user .save ()
218236
237+ RefreshToken (refresh_token ).blacklist ()
219238 return redirect (
220239 "https://procollab.ru/auth/reset_password/" ,
221240 status = status .HTTP_200_OK ,
@@ -225,12 +244,12 @@ def update(self, request, *args, **kwargs):
225244 except jwt .ExpiredSignatureError :
226245 return redirect (
227246 "https://procollab.ru/auth/reset_password/" ,
228- status = status .HTTP_200_OK ,
247+ status = status .HTTP_400_BAD_REQUEST ,
229248 message = "Activate Expired" ,
230249 )
231250 except jwt .DecodeError :
232251 return redirect (
233252 "https://procollab.ru/auth/reset_password/" ,
234- status = status .HTTP_200_OK ,
253+ status = status .HTTP_400_BAD_REQUEST ,
235254 message = "Decode error" ,
236255 )
0 commit comments