示例#1
0
def register_user_device(username: str,
                         password: str,
                         email: str = None) -> Status:
    mac_address = get_mac()
    ret = net_interface.server.register_user_device(username, password,
                                                    mac_address, email)
    if isinstance(ret, str):
        logger_security.info(f"Failed to register: {ret}")
        return Status.fail(ret)
    else:
        program_state.is_authenticated_at_server.started()
        logger_security.info(f"Successfully registered")
        _save_received_token(ret)
        return Status.success("Successfully registered")
示例#2
0
def login_manual(username: str,
                 password: str,
                 allow_auto_login=True) -> Status:
    mac_address = get_mac()
    ret = net_interface.server.login_manual_user_device(
        username, password, mac_address)
    if isinstance(ret, str):
        logger_security.info(f"Failed to login manually: {ret}")
        return Status.fail(ret)
    else:
        if allow_auto_login:
            _save_received_token(ret)
        program_state.is_authenticated_at_server.started()
        logger_security.info(f"Successfully logged in manually")
        return Status.success("Successfully logged in")
示例#3
0
def login_auto() -> Status:
    token = _get_token()
    if token is not None:
        mac = get_mac()
        success = net_interface.server.login_auto(token, mac)
        if not success:
            logger_security.info(f"Failed to login automatically: Wrong token")
            return Status.fail("Failed to automatically log in.")
        else:
            program_state.is_authenticated_at_server.started()
            logger_security.info(f"Successfully logged in automatically")
            return Status.success("Successfully auto logged in")

    else:
        logger_security.info(
            f"Failed to login automatically: Token not exists")
        return Status.fail("Failed to automatically log in.")
示例#4
0
def logout() -> Status:
    net_interface.server.logout()
    net_interface.ServerCommunicator.close_connection()
    program_state.is_authenticated_at_server.stopped()
    logger_security.info(f"Successfully logged out")
    return Status.success("Successfully logged out.")
示例#5
0
def _save_received_token(token: Token) -> None:
    with open(paths.AUTHENTICATION_PATH, "w+") as file:
        file.write(token.token)
    logger_security.info("Successfully saved token")