Beispiel #1
0
def signInUser():
    try:
        username = request.json['username']
        password = request.json['password']
        sign_in = SignIn(username, password)
        if sign_in.check_user():
            valid = sign_in.check_password()
        else:
            valid = "User does not exist"
        if valid == "User successfully authenticated":
            token = str(generate_token())
            db = connection()
            presentTime = str(time())
            insert_into_token_table(db, username, presentTime, token)

            message = """You just signed in, is it really you? Stay organized and on track,
                      If not! change your password immediately. For everyone minute spent in organizing,
                      an hour is earned!"""
            insert_into_recent_table(connection(), username, str(time()), "3:Signed In", message)

        else:
            token = False
        return jsonify({'valid': valid, 'token': token, 'username': username})
    except:
        return jsonify(False)
def registerUser():
    try:
        username = request.json['username']
        password = request.json['password']
        is_premium = request.json['premium']
        signup = SignUp(username, password, is_premium)
        registered = signup.add_user_after_authentication()
        token = False
        if registered:
            token = str(generate_token())
            db = connection()
            presentTime = str(time())

            message = "Welcome to Costrajectory, our beautiful chaos! You are in for an exhilarating journey with us!"
            # adding transaction to logs
            insert_into_recent_table(connection(), username, presentTime,
                                     "2:Registered Profile", message)

            # adding entry to username alias table
            alias = get_alias(username)
            add_alias(connection(), username, alias)

            insert_into_token_table(db, username, presentTime, token)
        x = jsonify({
            'username': request.json['username'],
            'registered': registered,
            'token': token
        })
        return x
    except:
        return jsonify(False)
Beispiel #3
0
 def test_utils(self):
     test_user_id = 1234
     test_phone = '972529999444'
     token = generate_token(test_user_id, test_phone)
     token_data = get_data_by_token(token)
     self.assertEqual(token_data['phone'], test_phone)
     self.assertEqual(token_data['user_id'], test_user_id)
     self.assertIn('user_id', token_data)
     self.assertIn('phone', token_data)
     self.assertIn('exp', token_data)
Beispiel #4
0
def login(request):
    '''
    POST login to system and generate JWT token
    :param request: flask request object
    '''
    try:
        data = json.loads(request.data)
        user = data.get('user')
        password = data.get('password')
        if not (user and password):
            logger.info(
                f'Login failed on {request.remote_addr}, missing credentials')
            raise InvalidCredentials(user, password)

        user_data = db.get_user_by_username(user)
        if not user_data:
            raise UserNotExists(user)

        elif not (user_data['user'] == user
                  and user_data['password'] == password):
            raise InvalidCredentials(user)

        elif not user_data['verify']:
            raise UserNotVerified()

        else:
            token = generate_token(user_data['id'], user_data['phone'])
            logger.info(f'Token for user {user} created. token: {token}')
            return token, 201

    except UserNotVerified as e:
        logger.warning(e.__str__())
        return e.__str__(), 401
    except UserNotExists as e:
        logger.warning(e.__str__())
        return e.__str__(), 404
    except InvalidCredentials as e:
        logger.warning(e.__str__())
        return e.__str__(), 401
    except Exception as e:
        logger.exception(f'Failed login from {request.remote_addr}')
        return f'Failed login {e.__str__()}', 501
Beispiel #5
0
 def __init__(self):
     self.data = '{}'
     self.remote_addr = 'Unittest'
     self.headers = {'token': generate_token(20, '972527777777')}