from source import packets
from source.behaviors import roles
from source.behaviors.events import base
from source.utils.crypto.type import CipherType


class RequestKeyEvent(base.BaseEvent):
    """
    Event reacting to a machine trying to get our secret symmetric key for secure communication
    """

    def handle(self, packet: packets.RequestKeyPacket, address: tuple):
        # check if we are a master
        if not isinstance(self.manager.role, roles.MasterRole):
            return

        # create a packet containing our secret key
        packet = packets.KeyPacket(self.manager.communication.secret_key)
        # send it back to the slave
        self.manager.communication.send(packet, CipherType.RSA, address)

        # TODO(Faraphel): check if we trust the slave ?
