Пример #1
0
    def perform_root(client, server, number, n):
        """
        Method to ask the server to calculate a root of a number

        :param client: Client instance, whose properties can be fetched
        :param server: Server connection that will be used to ask for the operation
        :param number: Server number on which the root will be calculated
        :param n: N of the root that needs to be computed
        :return: Result of the number's N root
        """

        signed_username = base64.b64encode(
            Cryptography.sign(Connection.get_key(),
                              client.get_username())).decode()
        server.send(
            f'OP:{client.get_username()}:{signed_username}:{n}:{number}'.
            encode())
        response = server.recv(100000).decode()
        if response == 'UNAUTHORIZED':
            print("You don't have permissions to perform this operation!")
            return None
        if response == 'INVALID':
            print("The values you inserted are not valid!")
            return None
        return float(response)
Пример #2
0
    def register_client(client, server):
        """
        Method to login a user in the server

        :param client: Client instance, whose properties can be fetched
        :param server: Server connection
        """

        username = input('Please insert the username:\n> ')
        one_time_id = input('Please insert the One Time ID:\n> ')

        signed_one_time_id = base64.b64encode(
            Cryptography.sign(Connection.get_key(), one_time_id)).decode()

        server.send(
            f'LOGIN:{username}:{one_time_id}:{signed_one_time_id}:{client.get_hostname()}'
            f':{str(client.get_port())}:{Connection.get_cert()}'.encode())
        response = server.recv(100000).decode()
        if response == 'OK':
            pin = input('Please insert your PIN:\n> ')
            client.save_pin(username, pin)
            client.set_username(username)
            print('Login successful and PIN set')
            return True
        else:
            print('Login was not successful, failed on the server')
            return False
Пример #3
0
    def list_users(client, server):
        """
        Method that accepts list requests

        :param client: Client instance, whose properties can be fetched
        :param server: Server connection
        """

        signed_username = base64.b64encode(
            Cryptography.sign(Connection.get_key(),
                              client.get_username())).decode()
        server.send(f'LIST:{client.get_username()}:{signed_username}'.encode())
        response = server.recv(100000000)
        if response[0] != 0x80:
            if response.decode() == 'UNAUTHORIZED':
                print("You don't have permissions to perform this operation!")
                return None

        user_list = pickle.loads(response)
        if len(user_list) > 0:
            print('Online users are:')
            for user in user_list:
                print('> ' + user['username'])
                client.add_user(user['username'], user['hostname'],
                                user['port'], user['cert'])
        else:
            print('No users online!')