def get(self, request): public_address = self.request.query_params.get('public_address') user_details = User.objects.get(public_address=public_address) user = authenticate(request, username=user_details.username, password="******") if user is not None: # login saves the user’s ID in the session, # using Django’s session framework. login(request, user) token_serializer = TokenSerializer( data={ # using drf jwt utility functions to generate a token "token": jwt_encode_handler(jwt_payload_handler(user)) }) token_serializer.is_valid() data_dict = { "status": 200, "data": { "token": token_serializer.data } } return Response(data_dict, status=status.HTTP_200_OK) return Response( { "status": 404, "error": "username or password incorrect" }, status=status.HTTP_404_NOT_FOUND)
def test_serialized_and_then_deserialized_data_is_valid(self): token = Token(email=TEST_EMAIL) token.save() serializer = TokenSerializer(token) content = JSONRenderer().render(serializer.data) stream = BytesIO(content) data = JSONParser().parse(stream) serializer = TokenSerializer(data=data) self.assertTrue(serializer.is_valid())
def post(self, request, *args, **kwargs): username = request.data.get("username", "") password = request.data.get("password", "") user = authenticate(request, username=username, password=password) if user is not None: # login saves the user’s ID in the session, # using Django’s session framework. login(request, user) token_serializer = TokenSerializer( data={ # using drf jwt utility functions to generate a token "token": jwt_encode_handler(jwt_payload_handler(user)) }) token_serializer.is_valid() user_serializer = UserDetailSerializer(user) if user.is_institution_administrator == True: try: related_institution = Institution.objects.get( created_by=user) institution = { "id": related_institution.pk, "institution_status": related_institution.is_institution_active } except: institution = {"status": "No institution created yet"} elif user.is_loan_officer == True: institution_staff = InstitutionStaff.objects.get(user_id=user) assigned_institution = Institution.objects.get( id=institution_staff.institution_id.pk) institution = { "id": assigned_institution.pk, "institution_status": assigned_institution.is_institution_active } elif user.is_teller == True: institution_staff = InstitutionStaff.objects.get(user_id=user) assigned_institution = Institution.objects.get( id=institution_staff.institution_id.pk) institution = { "id": assigned_institution.pk, "institution_status": assigned_institution.is_institution_active } login_data = { "user_data": user_serializer.data, "token_data": token_serializer.data, "institution": institution } return Response(login_data) return Response(status=status.HTTP_404_NOT_FOUND)
def post(self, request, *args, **kwargs): username = request.data.get("username", "") password = request.data.get("password", "") user = authenticate(request, username=username, password=password) if user is not None: # login saves the user’s ID in the session, # using Django’s session framework. login(request, user) token_serializer = TokenSerializer(data={ # using drf jwt utility functions to generate a token "token": jwt_encode_handler( jwt_payload_handler(user) )}) token_serializer.is_valid() return Response(token_serializer.data) return Response(status=status.HTTP_404_NOT_FOUND)
def post(self, request, *args, **kwargs): email = request.data.get("email", "") password = request.data.get("password", "") try: user = User.objects.get(email=email) except User.DoesNotExist: return Response(status=status.HTTP_401_UNAUTHORIZED) if user is not None and user.check_password(password): login(request, user) serializer = TokenSerializer( data={"token": jwt_encode_handler(jwt_payload_handler(user))}) serializer.is_valid() return Response(serializer.data) return Response(status=status.HTTP_401_UNAUTHORIZED)
def create(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) user = User.objects.get(username=serializer.validated_data['username']) if user is None or not user.check_password( serializer.validated_data['password']): return Response(status=status.HTTP_400_BAD_REQUEST) token, _ = Token.objects.get_or_create(user_id=user.id) return Response(TokenSerializer(token).data)
def get_response_data(self, user): if allauth_settings.EMAIL_VERIFICATION == \ allauth_settings.EmailVerificationMethod.MANDATORY: return {"detail": _("Verification e-mail sent.")} if getattr(settings, 'REST_USE_JWT', False): data = {'user': user, 'token': self.token} return JWTSerializer(data).data else: return TokenSerializer(user.auth_token).data
def post(self, request, *args, **kwargs): username = request.data.get("username", "") password = request.data.get("password", "") user = authenticate(request, username=username, password=password) if user is not None: # login saves the user’s ID in the session, # using Django’s session framework. login(request, user) token_serializer = TokenSerializer( data={ # using drf jwt utility functions to generate a token "token": jwt_encode_handler(jwt_payload_handler(user)) }) token_serializer.is_valid() if user.is_administrator == True: data_dict = { "status": 200, "data": { "token": token_serializer.data, "role": "admin" } } return Response(data_dict, status=status.HTTP_200_OK) elif user.is_store_owner == True: data_dict = { "status": 200, "data": { "token": token_serializer.data, "role": "store_owner" } } return Response(data_dict, status=status.HTTP_200_OK) return Response( { "status": 404, "error": "username or password incorrect" }, status=status.HTTP_404_NOT_FOUND)
def auth(self, request): serializer = AuthSerializer(data=request.DATA) if serializer.is_valid(): try: token = Authenticator.authenticate( email=serializer.data.get('email'), date=serializer.data.get('date'), signature=serializer.data.get('signature')) if token: return Response(TokenSerializer(token).data) else: return Response({'result': False}, status=status.HTTP_403_FORBIDDEN) except Exception as e: raise CustomAPIException(exception=e) raise CustomAPIException(detail=serializer.errors)