def validated(self, serialized_data, *args, **kwargs): from rest_framework_jwt.settings import api_settings from datetime import datetime from .models import AuthTransaction from drfaddons.utils import get_client_ip from rest_framework.response import Response user = serialized_data.object.get('user') or self.request.user token = serialized_data.object.get('token') response_data = api_settings.JWT_RESPONSE_PAYLOAD_HANDLER(token, user, self.request) response = Response(response_data) if api_settings.JWT_AUTH_COOKIE: expiration = (datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA) response.set_cookie(api_settings.JWT_AUTH_COOKIE, token, expires=expiration, httponly=True) user.last_login = datetime.now() user.save() AuthTransaction(created_by=user, token=token, ip_address=get_client_ip(self.request), session=user.get_session_auth_hash()).save() return response
def login_user(user: User, request) -> (dict, int): """ This function is used to login a user. It saves the authentication in AuthTransaction model. Parameters ---------- user: django.contrib.auth.get_user_model request: HttpRequest Returns ------- tuple: data: dict status_code: int """ from django.utils import timezone from rest_framework_jwt.utils import jwt_encode_handler from drfaddons.utils import get_client_ip from .models import AuthTransaction from .auth import jwt_payload_handler token = jwt_encode_handler(jwt_payload_handler(user)) user.last_login = timezone.now() user.save() AuthTransaction(created_by=user, ip_address=get_client_ip(request), token=token, session=user.get_session_auth_hash()).save() data = {'session': user.get_session_auth_hash(), 'token': token} return data
def login_user(user: User, request) -> (dict, int): """ This function is used to login a user. It saves the authentication in AuthTransaction model. Parameters ---------- user: django.contrib.auth.get_user_model request: HttpRequest Returns ------- tuple: data: dict status_code: int """ token = jwt_encode_handler(jwt_payload_handler(user)) user.last_login = timezone.now() user.save() AuthTransaction( created_by=user, ip_address=get_client_ip(request), token=token, session=user.get_session_auth_hash(), ).save() data = {"session": user.get_session_auth_hash(), "token": token} return data
def validated(self, serialized_data, *args, **kwargs): """Validates the response""" user = serialized_data.object.get("user") or self.request.user token = serialized_data.object.get("token") response_data = api_settings.JWT_RESPONSE_PAYLOAD_HANDLER( token, user, self.request) response = Response(response_data) if api_settings.JWT_AUTH_COOKIE: expiration = datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA response.set_cookie(api_settings.JWT_AUTH_COOKIE, token, expires=expiration, httponly=True) user.last_login = datetime.now() user.save() AuthTransaction( created_by=user, token=token, ip_address=get_client_ip(self.request), session=user.get_session_auth_hash(), ).save() return response
def login_user(user: User, request) -> (dict, int): from rest_framework_jwt.utils import jwt_payload_handler from drfaddons.utils import get_client_ip from rest_framework_jwt.utils import jwt_encode_handler from .models import AuthTransaction token = jwt_encode_handler(jwt_payload_handler(user)) user.last_login = datetime.datetime.now() user.save() AuthTransaction( user=user, ip_address=get_client_ip(request), token=token, session=user.get_session_auth_hash(), ).save() data = {"session": user.get_session_auth_hash(), "token": token} status_code = status.HTTP_200_OK return data, status_code