def get_token(self, obj): request = self.context.get('request') if request: auth_login(request, obj) # 主要为了记录last_login的, 其他的作用待研究 else: print('request is None, 请在代码手动传入, 否则无法自动登录') return str(SlidingToken.for_user(request.user))
def get(self, request, format=None): #print(dir(request)) #Token.for_user(request.user) #print(self.serializer_class.get_token(request.user)) #print(SlidingToken.for_user(request.user)) return Response({'token': str(SlidingToken.for_user(request.user))})
def test_sliding_tokens_are_added_to_outstanding_list(self): token = SlidingToken.for_user(self.user) qs = OutstandingToken.objects.all() outstanding_token = qs.first() self.assertEqual(qs.count(), 1) self.assertEqual(outstanding_token.user, self.user) self.assertEqual(outstanding_token.jti, token['jti']) self.assertEqual(outstanding_token.token, str(token)) self.assertEqual(outstanding_token.created_at, token.current_time) self.assertEqual(outstanding_token.expires_at, datetime_from_epoch(token['exp']))
def post(self ,request): data=JSONParser().parse(request) users=user_password_reset_time() msg=UserExperation(data) if msg=='insert': users.user=data['email'] users.save() user=UserAccounts.objects.get(email=data['email']) a=SlidingToken.for_user(user) send_link().send_reset_link(a,data['email']) return JsonResponse({'msg':"success"}) elif msg=='can resend link': users.user=data['email'] users.save() user=UserAccounts.objects.get(email=data['email']) a=SlidingToken.for_user(user) send_link().send_reset_link(a,data['email']) return JsonResponse({'msg':"success"}) elif msg=='cannot resend link': return JsonResponse({'msg':"error1"}) else: return JsonResponse({'msg':"error"})
def request_login_email(request): serializer = RequestLoginSerializer(data=request.data) serializer.is_valid(raise_exception=True) user_data = serializer.data user, created = User.objects.get_or_create(email=user_data["email"]) token = SlidingToken.for_user(user) # send email to user send_mail( subject="Your Linkanizer Login Link", message=f"Navigate to {settings.FRONTEND_BASE_URL}/login/{token}", from_email="No Reply <*****@*****.**>", recipient_list=[user.email], ) return Response(status=status.HTTP_200_OK)
def get_token(cls, user): return SlidingToken.for_user(user)