String getUsername() { return NettyUtils.userName(channel); }
void processDisconnect(MqttMessage msg) { final String clientID = NettyUtils.clientID(channel); LOG.trace("Start DISCONNECT CId={}, channel: {}", clientID, channel); if (!connected) { LOG.info("DISCONNECT received on already closed connection, CId={}, channel: {}", clientID, channel); return; } sessionRegistry.disconnect(clientID); connected = false; channel.close().addListener(FIRE_EXCEPTION_ON_FAILURE); LOG.trace("Processed DISCONNECT CId={}, channel: {}", clientID, channel); String userName = NettyUtils.userName(channel); postOffice.dispatchDisconnection(clientID,userName); LOG.trace("dispatch disconnection: clientId={}, userName={}", clientID, userName); }
void processSubscribe(MqttSubscribeMessage msg) { final String clientID = NettyUtils.clientID(channel); if (!connected) { LOG.warn("SUBSCRIBE received on already closed connection, CId={}, channel: {}", clientID, channel); dropConnection(); return; } postOffice.subscribeClientToTopics(msg, clientID, NettyUtils.userName(channel), this); }
void handleConnectionLost() { String clientID = NettyUtils.clientID(channel); if (clientID == null || clientID.isEmpty()) { return; } LOG.info("Notifying connection lost event. CId: {}, channel: {}", clientID, channel); Session session = sessionRegistry.retrieve(clientID); if (session.hasWill()) { postOffice.fireWill(session.getWill()); } if (session.isClean()) { sessionRegistry.remove(clientID); } else { sessionRegistry.disconnect(clientID); } connected = false; //dispatch connection lost to intercept. String userName = NettyUtils.userName(channel); postOffice.dispatchConnectionLost(clientID,userName); LOG.trace("dispatch disconnection: clientId={}, userName={}", clientID, userName); }
public void unsubscribe(List<String> topics, MQTTConnection mqttConnection, int messageId) { final String clientID = mqttConnection.getClientId(); for (String t : topics) { Topic topic = new Topic(t); boolean validTopic = topic.isValid(); if (!validTopic) { // close the connection, not valid topicFilter is a protocol violation mqttConnection.dropConnection(); LOG.warn("Topic filter is not valid. CId={}, topics: {}, offending topic filter: {}", clientID, topics, topic); return; } LOG.trace("Removing subscription. CId={}, topic={}", clientID, topic); subscriptions.removeSubscription(topic, clientID); // TODO remove the subscriptions to Session // clientSession.unsubscribeFrom(topic); String username = NettyUtils.userName(mqttConnection.channel); interceptor.notifyTopicUnsubscribed(topic.toString(), clientID, username); } // ack the client mqttConnection.sendUnsubAckMessage(topics, clientID, messageId); }
private boolean login(MqttConnectMessage msg, final String clientId) { // handle user authentication if (msg.variableHeader().hasUserName()) { byte[] pwd = null; if (msg.variableHeader().hasPassword()) { pwd = msg.payload().password().getBytes(StandardCharsets.UTF_8); } else if (!brokerConfig.isAllowAnonymous()) { LOG.error("Client didn't supply any password and MQTT anonymous mode is disabled CId={}", clientId); return false; } final String login = msg.payload().userName(); if (!authenticator.checkValid(clientId, login, pwd)) { LOG.error("Authenticator has rejected the MQTT credentials CId={}, username={}", clientId, login); return false; } NettyUtils.userName(channel, login); } else if (!brokerConfig.isAllowAnonymous()) { LOG.error("Client didn't supply any credentials and MQTT anonymous mode is disabled. CId={}", clientId); return false; } return true; }
@Test public void testSubscribedToNotAuthorizedTopic() { NettyUtils.userName(channel, FAKE_USER_NAME); IAuthorizatorPolicy prohibitReadOnNewsTopic = mock(IAuthorizatorPolicy.class); when(prohibitReadOnNewsTopic.canRead(eq(new Topic(NEWS_TOPIC)), eq(FAKE_USER_NAME), eq(FAKE_CLIENT_ID))) .thenReturn(false); sut = new PostOffice(subscriptions, new MemoryRetainedRepository(), sessionRegistry, ConnectionTestUtils.NO_OBSERVERS_INTERCEPTOR, new Authorizator(prohibitReadOnNewsTopic)); connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(channel); //Exercise MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(AT_MOST_ONCE, NEWS_TOPIC) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, FAKE_CLIENT_ID, FAKE_USER_NAME, connection); // Verify MqttSubAckMessage subAckMsg = channel.readOutbound(); verifyFailureQos(subAckMsg); }
void processPublish(MqttPublishMessage msg) { final MqttQoS qos = msg.fixedHeader().qosLevel(); final String username = NettyUtils.userName(channel); final String topicName = msg.variableHeader().topicName(); final String clientId = getClientId();