Ejemplo n.º 1
0
 def test_encode_auth_token(self):
     user = User(email='*****@*****.**',
                 password='******',
                 registered_on=datetime.datetime.utcnow(),
                 public_id=str(uuid.uuid4()))
     db.session.add(user)
     db.session.commit()
     auth_token = User.encode_auth_token(user.id)
     self.assertTrue(isinstance(auth_token, bytes))
Ejemplo n.º 2
0
    def login_user(data):
        try:
            # fetch the user data
            user = User.query.filter_by(email=data.get('email')).first()
            if user and user.check_password(data.get('password')):
                auth_token = User.encode_auth_token(user.id)
                if auth_token:
                    response_object = {
                        'status': 'success',
                        'message': 'Successfully logged in.',
                        'authorization': auth_token.decode()
                    }
                    return response_object, 200
            else:
                response_object = {
                    'status': 'fail',
                    'message': 'email or password does not match.'
                }
                return response_object, 401

        except Exception as e:
            print(e)
            response_object = {
                'status': 'fail',
                'message': 'Try again'
            }
            return response_object, 500
Ejemplo n.º 3
0
 def get_logged_in_user(new_request):
     # get the auth token
     auth_token = new_request.headers.get('Authorization').split(" ")[1]
     if auth_token:
         resp = User.decode_auth_token(auth_token)
         if not isinstance(resp, str):
             user = User.query.filter_by(id=resp).first()
             response_object = {
                 'status': 'success',
                 'data': {
                     'user_id': user.id,
                     'public_id': user.public_id,
                     'email': user.email,
                     'admin': user.admin,
                     'registered_on': str(user.registered_on)
                 }
             }
             return response_object, 200
         response_object = {
             'status': 'fail',
             'message': resp
         }
         return response_object, 401
     else:
         response_object = {
             'status': 'fail',
             'message': 'Provide a valid auth token.'
         }
         return response_object, 401
Ejemplo n.º 4
0
def save_new_user(data):
    user = User.query.filter_by(email=data['email']).first()
    if not user:
        new_user = User(public_id=str(uuid.uuid4()),
                        email=data['email'],
                        username=data['username'],
                        password=data['password'],
                        registered_on=datetime.datetime.utcnow())
        save_changes(new_user)
        return generate_token(new_user)
    else:
        response_object = {
            'status': 'fail',
            'message': 'User already exists. Please Log in.',
        }
        return response_object, 409
Ejemplo n.º 5
0
def generate_token(user):
    try:
        # generate the auth token
        auth_token = User.encode_auth_token(user.id)
        response_object = {
            'status': 'success',
            'message': 'Successfully registered.',
            'authorization': auth_token.decode()
        }
        return response_object, 201
    except Exception as e:
        response_object = {
            'status': 'fail',
            'message': 'Some error occurred. Please try again.'
        }
        return response_object, 401
Ejemplo n.º 6
0
 def logout_user(data):
     if data:
         auth_token = data.split(" ")[1]
     else:
         auth_token = ''
     if auth_token:
         resp = User.decode_auth_token(auth_token)
         if not isinstance(resp, str):
             # mark the token as blacklisted
             return save_token(token=auth_token)
         else:
             response_object = {
                 'status': 'fail',
                 'message': resp
             }
             return response_object, 401
     else:
         response_object = {
             'status': 'fail',
             'message': 'Provide a valid auth token.'
         }
         return response_object, 403