def post(self, request): if check_blacklist_token(request): return Response(status=status.HTTP_401_UNAUTHORIZED) serializer = serializers.RevokeTokenSerializer(data=request.data) if serializer.is_valid(): refresh_token = serializer.data.get('refresh_token') try: decoded_token = jwt.decode(refresh_token, settings.SECRET_KEY)['token'] except: return Response({"message": "This is not refresh token"}, status=status.HTTP_400_BAD_REQUEST) print(decoded_token) try: to_delete_token = AccessToken.objects.get( token=str(decoded_token)) if to_delete_token.user != str(request.user.username): return Response( { "message": "Access token and refresh token do not " "match" }, status=status.HTTP_400_BAD_REQUEST) print(to_delete_token.user) to_delete_token.delete() except: return Response({'message': 'Refresh token was not found'}) else: return Response(status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_200_OK)
def post(self, request): if check_blacklist_token(request): return Response(status=status.HTTP_401_UNAUTHORIZED) serializer = serializers.RefreshTokenSerializer(data=request.data) if serializer.is_valid(): token = serializer.data.get('token') decoded_token = jwt.decode(token, settings.SECRET_KEY) try: valid_token = AccessToken.objects.get( token=decoded_token['token']) if datetime.now(timezone.utc) < valid_token.expires: username = valid_token.user.username acc = Account.objects.get(username=username) payload = { 'uuid': str(acc.uuid), 'username': acc.username, 'time': str(datetime.now(timezone.utc)), } access_token = jwt.encode( payload, settings.SECRET_KEY).decode('utf-8') return Response({'access token': access_token}, status=status.HTTP_200_OK) else: return Response(status=status.HTTP_400_BAD_REQUEST) except: return Response({'message': 'Refresh token not found'}, status=status.HTTP_404_NOT_FOUND) else: return Response(status=status.HTTP_400_BAD_REQUEST)
def delete(self, request): if check_blacklist_token(request): return Response(status=status.HTTP_401_UNAUTHORIZED) acc = Account.objects.get(username=request.user.username) try: prof = Profile.objects.get(uuid=acc.uuid) prof.delete() except: pass acc.delete() return Response(status=status.HTTP_200_OK)
def patch(self, request): if check_blacklist_token(request): return Response(status=status.HTTP_401_UNAUTHORIZED) serializer = serializers.UpdateEmailSerializer(data=request.data) if serializer.is_valid(): email = serializer.data.get('email') acc = Account.objects.get(username=request.user.username) acc.email = email acc.save() return Response(status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): if check_blacklist_token(request): return Response(status=status.HTTP_401_UNAUTHORIZED) acc = Account.objects.get(username=request.user.username) to_email = acc.email current_site = get_current_site(request) message = render_to_string( 'email_active.html', { 'user': acc, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(acc.uuid)), 'token': email_activation_token.make_token(acc), }) mail_subject = 'Activate your blog account.' to_email = to_email email = EmailMessage(mail_subject, message, to=[to_email]) email.send() return Response(status=status.HTTP_200_OK)
def patch(self, request): if check_blacklist_token(request): return Response(status=status.HTTP_401_UNAUTHORIZED) serializer = serializers.UpdateProfileSerializer(data=request.data) if serializer.is_valid(): fullname = serializer.data.get('fullname') address = serializer.data.get('address') country = serializer.data.get('country') phone = serializer.data.get('phone') date_of_birth = serializer.data.get('date_of_birth') acc = Account.objects.get(username=request.user.username) profiles = Profile.objects.all() if str(acc.uuid) in [profile.uuid for profile in profiles]: prof = profiles.get(uuid=acc.uuid) if fullname != '': prof.fullname = fullname if address != '': prof.address = address if country != '': prof.country = country if phone != '': prof.phone = phone if date_of_birth != '': print(date_of_birth) prof.date_of_birth = date_of_birth prof.save() return Response(status=status.HTTP_200_OK) else: prof = Profile() prof.uuid = acc.uuid prof.fullname = fullname prof.address = address prof.country = country prof.phone = phone prof.date_of_birth = date_of_birth prof.save() return Response(status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): if check_blacklist_token(request): return Response(status=status.HTTP_401_UNAUTHORIZED) acc = Account.objects.get(username=request.user.username) try: prof = Profile.objects.get(uuid=acc.uuid) msg = { 'id': prof.id, 'uuid': prof.uuid, 'fullname': prof.fullname, 'address': prof.address, 'country': prof.country, 'phone': prof.phone, 'date_of_birth': prof.date_of_birth } return Response(msg, status=status.HTTP_200_OK) except: return Response({'message': 'Profile not found'}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, formar=None): if check_blacklist_token(request): return Response(status=status.HTTP_401_UNAUTHORIZED) acc = Account.objects.get(username=request.user.username) prof = '' print(acc.uuid) try: prof = Profile.objects.get(uuid=acc.uuid).to_dic() except: prof = 'null' print(prof) user_infor = { 'id': str(acc.uuid), 'username': acc.username, 'email': acc.email, 'timezone': acc.timezone, 'profile': prof } return Response(user_infor, content_type='application/json')
def get(self, request, format=None): if check_blacklist_token(request): return Response(status=status.HTTP_401_UNAUTHORIZED) return Response({'verify': True})
def wrapper(self, request, *args, **kwargs): if check_blacklist_token(request): return Response(status.HTTP_401_UNAUTHORIZED) else: return func(self, request, *args, **kwargs)