Exemple #1
0
    def get_jwt_user(request):
        user = get_user(request)
        if user.is_authenticated:
            return user
        jwt_authentication = JSONWebTokenAuthentication()
        if jwt_authentication.get_jwt_value(request):
            jwt_value = jwt_authentication.get_jwt_value(request)
            import jwt
            try:
                payload = jwt_decode_handler(jwt_value)
            except jwt.ExpiredSignature:
                print("Signature expired.")
                msg = {
                    'jwtResponse': 'Signature has expired.'
                }
                return msg
            except jwt.DecodeError:
                print('Error decoding signature.')
                msg = {
                    'jwtResponse': 'Error decoding signature.'
                }
                return msg
            except jwt.InvalidTokenError:
                print("invalid token error")
                return exceptions.AuthenticationFailed()

            user = jwt_authentication.authenticate_credentials(payload)

            user, jwt = jwt_authentication.authenticate(request)
        return user
 def get_jwt_user(request):
     user = get_user(request)
     if user.is_authenticated:
         return user
     jwt_authentication = JSONWebTokenAuthentication()
     if jwt_authentication.get_jwt_value(request):
         user, jwt = jwt_authentication.authenticate(request)
     # print request.
     print jwt_authentication.get_jwt_value(request)
     print jwt_authentication.authenticate(request)
     return user
Exemple #3
0
 def get_serializer(self, *args, **kwargs):
     if 'data' in kwargs and 'token' not in kwargs['data']:
         authorizer = JSONWebTokenAuthentication()
         jwt_value = authorizer.get_jwt_value(self.request)
         if jwt_value is not None and len(jwt_value) > 0:
             kwargs['data']['token'] = jwt_value.decode('utf-8')
     serializer_class = self.get_serializer_class()
     kwargs['context'] = self.get_serializer_context()
     return serializer_class(*args, **kwargs)
Exemple #4
0
    def get_jwt_user(request):
        user = get_user(request)

        # prevent the generation of Token for anonymous user
        if user.is_authenticated:
            return user
        jwt_authentication = JSONWebTokenAuthentication()
        if jwt_authentication.get_jwt_value(request):
            user, jwt = jwt_authentication.authenticate(request)
        return user
 def get_jwt_user(request):
     try:
         user = get_user(request)
         if user.is_authenticated:
             return user
         jwt_authentication = JSONWebTokenAuthentication()
         if jwt_authentication.get_jwt_value(request):
             user, jwt = jwt_authentication.authenticate(request)
     except AuthenticationFailed as e:
         logger.error("Authentication failed: {}".format(e))
         return None
     return user
    def get_jwt_user(request):
        """

        :param request:
        :return:
        """
        user = get_user(request)
        if user.is_authenticated:
            return user
        jwt_authentication = JSONWebTokenAuthentication()
        if jwt_authentication.get_jwt_value(request):
            user, jwt = jwt_authentication.authenticate(request)
        return user
Exemple #7
0
 def process_request(self, request):
     if '/media' in request.path_info:
         jwt_auth = JSONWebTokenAuthentication()
         jwt_token = jwt_auth.get_jwt_value(request)
         if jwt_token:
             user_details = jwt_decode_handler(jwt_token)
             if user_details:
                 request.user = User.objects.get(
                     username=user_details['username'])
                 logger.info(
                     "[User-{0}-id-{1}] authenticated for media file {2}".format(
                         request.user.username, request.user.id, request.path_info
                     ))
Exemple #8
0
    def get_jwt_user(request):
        user = get_user(request)
        if user.is_authenticated:
            return user

        jwt_authentication = JSONWebTokenAuthentication()
        if jwt_authentication.get_jwt_value(request):
            try:
                user, jwt = jwt_authentication.authenticate(request)
                return user
            except exceptions.AuthenticationFailed as af:
                logger.warning(
                    request,
                    "get_jwt_user(): AuthenticationFailed: {}.".format(af))
                return user  # AnonymousUser
        else:
            return user  # AnonymousUser
Exemple #9
0
def otp_is_verified(request):
    """
    Helper to determine if user has verified OTP.
    """
    auth = JSONWebTokenAuthentication()
    jwt_value = auth.get_jwt_value(request)
    if jwt_value is None:
        return False

    payload = jwt_decode_handler(jwt_value)
    persistent_id = payload.get('otp_device_id')

    if persistent_id:
        device = Device.from_persistent_id(persistent_id)
        if device is not None and device.user_id != request.user.id:
            return False
        # Valid device in JWT
        return True
    return False
Exemple #10
0
 def get_jwt_user(self, request):
     user = None
     user = get_user(request)
     msg = None
     if user.is_authenticated:
         return user, msg
     jwt_authentication = JSONWebTokenAuthentication()
     token = jwt_authentication.get_jwt_value(request)
     if token:
         try:
             payload = jwt_decode_handler(token)
         except jwt.ExpiredSignature:
             msg = 'Signature has expired.'
         except jwt.DecodeError:
             msg = 'Error decoding signature.'
         except jwt.InvalidTokenError:
             msg = "Invalid Token/Credentials."
         if not msg:
             user = jwt_authentication.authenticate_credentials(payload)
     return user, msg
Exemple #11
0
def get_user_jwt(request):
    """
    Приоритет, если есть пользователь в JWT - вытаскиваем его.
    :param request:
    :return:
    """
    auth = JSONWebTokenAuthentication()
    try:
        jwt_value = auth.get_jwt_value(request)
        if jwt_value:
            user_jwt = auth.authenticate(Request(request))
            if user_jwt is not None:
                return user_jwt[0]
    except AuthenticationFailed:
        # Тут был jwt токен
        return AnonymousUser()

    user = get_user(request)
    if user.is_authenticated():
        return user
    return AnonymousUser()
Exemple #12
0
def otp_is_verified(self, request):
    """
    Helper to determine if user has verified OTP.
    :param self:
    :param request:
    :return: TRUE or FALSE
    """
    auth = JSONWebTokenAuthentication()
    jwt_value = auth.get_jwt_value(request)
    if jwt_value is None:
        return False

    payload = jwt_decode_handler(jwt_value)
    persistent_id = payload.get('otp_device_id')

    if persistent_id:
        device = Device.from_persistent_id(persistent_id)
        if (device is not None) and (device.user_id != request.user.id):
            return False
        else:
            # Valid device in JWT
            return True
    else:
        return False
Exemple #13
0
def get_uid_from_jwt(request):
    auth = JSONWebTokenAuthentication()
    jwt_value = auth.get_jwt_value(request)
    payload = api_settings.JWT_DECODE_HANDLER(jwt_value)
    return payload['user_id']
def get_jwt_value(request):
    auth = JSONWebTokenAuthentication()
    try:
        return auth.get_jwt_value(request)
    except AuthenticationFailed:
        pass
 def get_jwt_value(self, request):
     return request.query_params.get(
         'jwt') or JSONWebTokenAuthentication.get_jwt_value(self, request)