async def coro(): s = Session(None) s.username = "******" s.password = "******" auth_plugin = FileAuthPlugin(context) ret = await auth_plugin.authenticate(session=s) self.assertFalse(ret)
async def coro(): s = Session(None) s.username = "******" auth_plugin = AnonymousAuthPlugin(context) ret = await auth_plugin.authenticate(session=s) self.assertTrue(ret)
async def init_from_connect(cls, stream: StreamAdapter, plugins_manager): """ :param stream: :param plugins_manager: :return: """ remote_address, remote_port = stream.get_peer_info() try: connect = await ConnectPacket.from_stream(stream) except NoDataException: raise MQTTException("Client closed the connection") logger.debug("< B %r", connect) await plugins_manager.fire_event(EVENT_MQTT_PACKET_RECEIVED, packet=connect) # this shouldn't be required anymore since broker generates for each client a random client_id if not provided # [MQTT-3.1.3-6] if connect.payload.client_id is None: raise MQTTException( "[[MQTT-3.1.3-3]] : Client identifier must be present") if connect.variable_header.will_flag: if connect.payload.will_topic is None or connect.payload.will_message is None: raise MQTTException( "will flag set, but will topic/message not present in payload" ) if connect.variable_header.reserved_flag: raise MQTTException( "[MQTT-3.1.2-3] CONNECT reserved flag must be set to 0") if connect.proto_name != "MQTT": raise MQTTException( '[MQTT-3.1.2-1] Incorrect protocol name: "%s"' % connect.proto_name) connack = None error_msg = None if connect.proto_level != 4: # only MQTT 3.1.1 supported error_msg = "Invalid protocol from %s: %d" % ( format_client_message(address=remote_address, port=remote_port), connect.proto_level, ) connack = ConnackPacket.build(0, UNACCEPTABLE_PROTOCOL_VERSION ) # [MQTT-3.2.2-4] session_parent=0 elif not connect.username_flag and connect.password_flag: connack = ConnackPacket.build( 0, BAD_USERNAME_PASSWORD) # [MQTT-3.1.2-22] elif connect.username_flag and not connect.password_flag: connack = ConnackPacket.build( 0, BAD_USERNAME_PASSWORD) # [MQTT-3.1.2-22] elif connect.username_flag and connect.username is None: error_msg = "Invalid username from %s" % (format_client_message( address=remote_address, port=remote_port)) connack = ConnackPacket.build( 0, BAD_USERNAME_PASSWORD) # [MQTT-3.2.2-4] session_parent=0 elif connect.password_flag and connect.password is None: error_msg = "Invalid password %s" % (format_client_message( address=remote_address, port=remote_port)) connack = ConnackPacket.build( 0, BAD_USERNAME_PASSWORD) # [MQTT-3.2.2-4] session_parent=0 elif connect.clean_session_flag is False and ( connect.payload.client_id_is_random): error_msg = ( "[MQTT-3.1.3-8] [MQTT-3.1.3-9] %s: No client Id provided (cleansession=0)" % (format_client_message(address=remote_address, port=remote_port))) connack = ConnackPacket.build(0, IDENTIFIER_REJECTED) if connack is not None: logger.debug("B > %r", connack) await plugins_manager.fire_event(EVENT_MQTT_PACKET_SENT, packet=connack) await connack.to_stream(stream) await stream.close() raise MQTTException(error_msg) incoming_session = Session(plugins_manager) incoming_session.client_id = connect.client_id incoming_session.clean_session = connect.clean_session_flag incoming_session.will_flag = connect.will_flag incoming_session.will_retain = connect.will_retain_flag incoming_session.will_qos = connect.will_qos incoming_session.will_topic = connect.will_topic incoming_session.will_message = connect.will_message incoming_session.username = connect.username incoming_session.password = connect.password if connect.keep_alive > 0: incoming_session.keep_alive = connect.keep_alive else: incoming_session.keep_alive = 0 handler = cls(plugins_manager) return handler, incoming_session