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,42 @@ 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+ if (datetime .now ().minute - last_update .minute ) < 0 :
227+ return redirect (
228+ "https://procollab.ru/auth/reset_password/" ,
229+ status = status .HTTP_400_BAD_REQUEST ,
230+ message = "You can't change your password so often" ,
214231 )
215232
216233 user .set_password (serializer .data ["new_password" ])
217234 user .save ()
218235
236+ RefreshToken (refresh_token ).blacklist ()
219237 return redirect (
220238 "https://procollab.ru/auth/reset_password/" ,
221239 status = status .HTTP_200_OK ,
@@ -225,12 +243,12 @@ def update(self, request, *args, **kwargs):
225243 except jwt .ExpiredSignatureError :
226244 return redirect (
227245 "https://procollab.ru/auth/reset_password/" ,
228- status = status .HTTP_200_OK ,
246+ status = status .HTTP_400_BAD_REQUEST ,
229247 message = "Activate Expired" ,
230248 )
231249 except jwt .DecodeError :
232250 return redirect (
233251 "https://procollab.ru/auth/reset_password/" ,
234- status = status .HTTP_200_OK ,
252+ status = status .HTTP_400_BAD_REQUEST ,
235253 message = "Decode error" ,
236254 )
0 commit comments