Esempio n. 1
0
    def register_user(client_socket, parts):
        """
        Method to register users and their clients

        :param client_socket: Socket where a connection with a client is happening
        :param parts: Parts of the message that was sent by the client
        """

        username = parts[1]
        one_time_id = parts[2]
        signature = base64.b64decode(parts[3])
        hostname = parts[4]
        port = parts[5]
        client_cert = parts[6]

        valid_signature = Cryptography.verify_signature(
            base64.b64decode(client_cert).decode(), one_time_id, signature)

        if valid_signature and User.login(username, one_time_id):
            ClientsStore.save_client(username, hostname, port, client_cert)
            ClientsStore.set_online(username, hostname, port)
            Administration.__socket_map[client_socket] = username
            client_socket.send('OK'.encode())
        else:
            client_socket.send('NOTOK'.encode())
    def run(self, server, protocol, command):
        login = command['account_name']
        password = command['password']
        public_key = command['public_key']
        hashed_password = get_hash(login, password)

        session = sessionmaker(bind=self.db_engine)()
        db_user = session.query(SQLUser).filter_by(login=login).first()
        if db_user:
            if db_user.password != hashed_password:
                protocol.send_packet(ResponsePacket(402, command['id']))
                session.close()
                return
        else:
            db_user = SQLUser(login=login,
                              password=hashed_password,
                              public_key=public_key)
            session.add(db_user)
            session.commit()

        user = User()
        user.gid = db_user.gid
        user.login = db_user.login
        user.public_key = db_user.public_key
        user.protocol = protocol
        protocol.user = user

        server.logged_users.update({user.login: user})
        protocol.send_packet(ResponsePacket(202, command['id']))