def acMain(ac_version): """Main function that is invoked by Assetto Corsa.""" global NOTIFICATION, LAPTIME_LABELS app = ac.newApp("AC-Ranking") ac.setSize(app, 400, 300) NOTIFICATION = ac.addLabel(app, '') ac.setPosition(NOTIFICATION, 15, 20) ac.setSize(NOTIFICATION, 190, 20) auth = read_auth() validate_token(auth['user'], auth['token']) validate_token_button = ac.addButton(app, 'Validate token') ac.setPosition(validate_token_button, 20, 40) ac.setSize(validate_token_button, 120, 20) ac.addOnClickedListener(validate_token_button, validate_token_button_func) refresh_button = ac.addButton(app, '\u21BB') ac.setPosition(refresh_button, 300, 5) ac.setSize(refresh_button, 15, 18) ac.addOnClickedListener(refresh_button, refresh_button_func) LAPTIME_LABELS = tuple(ac.addLabel(app, '#' + str(i)) for i in range(10)) for index, label in enumerate(LAPTIME_LABELS): ac.setSize(label, 120, 20) ac.setPosition(label, 200, (index * 20) + 50) get_laptimes(ac.getCarName(0), ac.getTrackName(0), ac.getTrackConfiguration(0) or None) return "ACR"
def test_validation(): """Tests the authentication methods. """ assert authentication.validate_token("mfa.ThisIsAValidToken") is True assert authentication.validate_token("foo") is False assert authentication.validate_token("") is False assert authentication.validate_token(" ") is False
def delete_user(session, deletion_token: str): """ Delete a user's account. :param session: the db session. :param deletion_token: the deletion token. :return: whether the deletion was success or not. """ # Validate deletion token valid, user_id = authentication.validate_token( deletion_token.encode(), authentication.TokenType.DELETION) if not valid: return False # Get user user = session.query(models.User).filter(models.User.id == user_id).first() # Check if the user was found if user is None: return False # Delete user session.delete(user) session.commit() return True
def verify_user(session, verification_token: str): """ Verify a user's account. :param session: the db session. :param verification_token: the verification token. :return: whether the verification was success or not. """ # Validate verification token valid, user_id = authentication.validate_token( verification_token.encode(), authentication.TokenType.VERIFICATION) if not valid: return False # Get user user = session.query(models.User).filter(models.User.id == user_id).first() # Check if the user was found if user is None: return False # Set user's account to verified user.verified = True session.commit() return True
def test_validate_token_ok_02(self) -> None: """ Test the function that validates a token, with a success case for an REFRESH token. """ # The expected result expected_result = True, 123 # Prepare the mocks configuration.REFRESH_TOKEN_VALIDITY_DAYS = 5 token = authentication.generate_token(123, authentication.TokenType.REFRESH, unittest.mock.MagicMock()) db_calls_mock.get_token.return_value = models.Token( token, datetime.date.today() + datetime.timedelta(days=configuration.REFRESH_TOKEN_VALIDITY_DAYS)) # Call the function actual_result = authentication.validate_token( token.encode(), authentication.TokenType.REFRESH, unittest.mock.MagicMock()) # Verify the result self.assertEqual(expected_result, actual_result) db_calls_mock.get_token.assert_called()
def verify_access_token(token): """ Verify if the access token is valid. :param token: the token used in the authentication. :return: whether or not the token is valid. """ valid, user_id = authentication.validate_token( token.encode(), authentication.TokenType.ACCESS) return valid
def test_validate_token_error_02(self) -> None: """ Test the function that validates a token, with an error due to missing type in payload. """ # The expected result expected_result = False, None # Call the function token_payload = {'key': 'value'} token = jwt.encode(token_payload, 'secret key', algorithm='HS256') actual_result = authentication.validate_token( token, authentication.TokenType.DELETION) # Verify the result self.assertEqual(expected_result, actual_result)
def test_validate_token_error_03(self) -> None: """ Test the function that validates a token, with an error due to different type in payload. """ # The expected result expected_result = False, None # Prepare the mocks configuration.ACCESS_TOKEN_VALIDITY_HOURS = 5 # Call the function token = authentication.generate_token(123, authentication.TokenType.ACCESS) actual_result = authentication.validate_token( token, authentication.TokenType.DELETION) # Verify the result self.assertEqual(expected_result, actual_result)
def send_change_email_new(session, change_token_old: str, new_email: str) -> (bool, bool): """ Send a 'Change Email' email to the new email address. :param session: the db session. :param change_token_old: the change token from the old email address. :param new_email: the new email. :return: a pair of booleans: the first is the success of the operation and the second is if the motif of the failure is that the new email is already at use. """ # Validate the change token from the old email address valid, user_id = authentication.validate_token( change_token_old.encode(), authentication.TokenType.CHANGE_EMAIL_OLD) if not valid: return False, False # Get the user id from the token user_id = authentication.get_token_field(change_token_old.encode(), 'user') # Get user user = session.query(models.User).filter(models.User.id == user_id).first() if user is None: return False, False # Check if the new email is valid new_email_user = session.query( models.User).filter(models.User.email == new_email).first() if new_email_user is not None: return False, True changes = {ChangeType.NEW_EMAIL.value: new_email} change_email_new_token = authentication.generate_change_token( user.id, authentication.TokenType.CHANGE_EMAIL_NEW, changes).decode() process_emails.set_language(user.language) return process_emails.send_change_email_new(new_email, change_email_new_token, user.email), True
def test_validate_token_ok_01(self) -> None: """ Test the function that validates a token, with a success case for an ACCESS token. """ # The expected result expected_result = True, 123 # Prepare the mocks configuration.ACCESS_TOKEN_VALIDITY_HOURS = 5 # Call the function token = authentication.generate_token(123, authentication.TokenType.ACCESS) actual_result = authentication.validate_token( token.encode(), authentication.TokenType.ACCESS) # Verify the result self.assertEqual(expected_result, actual_result) db_calls_mock.get_token.assert_not_called()
def change_user_settings_token(session, change_token: str): """ Change settings from a user's account. :param session: the db session. :param change_token: the change token. :return: whether the deletion was success or not. """ # Validate change token valid, user_id = authentication.validate_token( change_token.encode(), authentication.TokenType.CHANGE_EMAIL_NEW) if not valid: return False # Check for changes payload = authentication.get_token_payload(change_token) return change_user_settings(session, payload, user_id)
def test_validate_token_error_04(self) -> None: """ Test the function that validates a token, with an error due to db not finding the REFRESH token. """ # The expected result expected_result = False, None # Prepare the mocks configuration.REFRESH_TOKEN_VALIDITY_DAYS = 5 db_calls_mock.get_token.return_value = None # Call the function token = authentication.generate_token(123, authentication.TokenType.REFRESH) actual_result = authentication.validate_token( token.encode(), authentication.TokenType.REFRESH, unittest.mock.MagicMock()) # Verify the result self.assertEqual(expected_result, actual_result) db_calls_mock.get_token.assert_called()
def recover_password(session: sqlalchemy.orm.Session, recover_token: str, new_password: str): """ Change the password of the user's account. :param session: the db session. :param recover_token: the recover token. :param new_password: the new password. :return: whether the deletion was success or not. """ # Validate change token valid, user_id = authentication.validate_token( recover_token.encode(), authentication.TokenType.PASSWORD_RECOVERY) if not valid: return False # Change the password return change_user_settings(session, {ChangeType.NEW_PASSWORD.value: new_password}, user_id)
def validate_token_button_func(x, y): """Validate user's token.""" auth = read_auth() ac.setText(NOTIFICATION, 'Validating token..') validate_token(auth['user'], auth['token'])