public byte[] getPassword() { return msg.payload().password().getBytes(StandardCharsets.UTF_8); }
public String getWillTopic() { return msg.payload().willTopic(); }
public byte[] getWillMessage() { return msg.payload().willMessage().getBytes(StandardCharsets.UTF_8); } }
public String getClientID() { return msg.payload().clientIdentifier(); }
public String getUsername() { return msg.payload().userName(); }
@Override public void notifyClientConnected(final MqttConnectMessage msg) { for (final InterceptHandler handler : this.handlers.get(InterceptConnectMessage.class)) { LOG.debug("Sending MQTT CONNECT message to interceptor. CId={}, interceptorId={}", msg.payload().clientIdentifier(), handler.getID()); executor.execute(() -> handler.onConnect(new InterceptConnectMessage(msg))); } }
@Override protected void channelRead0(ChannelHandlerContext ctx, MqttConnectMessage message) { String username = message.payload().userName().trim().toLowerCase(); String token = new String(message.payload().passwordInBytes(), CharsetUtil.UTF_8); TokenValue tokenValue = holder.tokenManager.getTokenValueByToken(token); if (tokenValue == null || !tokenValue.user.email.equalsIgnoreCase(username)) { log.debug("MqttHardwareLogic token is invalid. Token '{}', '{}'", token, ctx.channel().remoteAddress()); ctx.writeAndFlush(createConnAckMessage(CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD), ctx.voidPromise()); return; } User user = tokenValue.user; Device device = tokenValue.device; DashBoard dash = tokenValue.dash; ChannelPipeline pipeline = ctx.pipeline(); HardwareStateHolder hardwareStateHolder = new HardwareStateHolder(user, tokenValue.dash, device); pipeline.replace(this, "HHArdwareMqttHandler", new MqttHardwareHandler(holder, hardwareStateHolder)); Session session = holder.sessionDao.getOrCreateSessionByUser( hardwareStateHolder.userKey, ctx.channel().eventLoop()); if (session.isSameEventLoop(ctx)) { completeLogin(ctx.channel(), session, user, dash, device, -1); } else { log.debug("Re registering hard channel. {}", ctx.channel()); ReregisterChannelUtil.reRegisterChannel(ctx, session, channelFuture -> completeLogin(channelFuture.channel(), session, user, dash, device, -1)); } }
private Session.Will createWill(MqttConnectMessage msg) { final ByteBuf willPayload = Unpooled.copiedBuffer(msg.payload().willMessageInBytes()); final String willTopic = msg.payload().willTopic(); final boolean retained = msg.variableHeader().isWillRetain(); final MqttQoS qos = MqttQoS.valueOf(msg.variableHeader().willQos()); return new Session.Will(willTopic, willPayload, qos, retained); }
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; }
@Override public void handleMessage(Message message) { MqttClient client = (MqttClient) message.getClient(); MqttConnectMessage connectMessage = (MqttConnectMessage) message.getPayload(); MqttConnectReturnCode returnCode; MqttConnAckMessage ackMessage; ChannelHandlerContext ctx = client.getCtx(); if (!isClientIdValid(connectMessage.payload().clientIdentifier())) { returnCode = MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED; } else if (!checkUsername(connectMessage.payload().userName()) || !checkPassword(connectMessage.payload().passwordInBytes())) { returnCode = MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD; } else if (!isAuthorized(connectMessage)) { returnCode = MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED; } else if (client.isConnected()) { // protocol violation and disconnect ctx.fireUserEventTriggered(new DisconnectChannelEvent(ctx.channel())); return; } else if (!isServiceAviable(connectMessage)) { returnCode = MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE; } else { client.setId(connectMessage.payload().clientIdentifier()); client.setConnected(true); client.setCleanSession(connectMessage.variableHeader().isCleanSession()); ctx.channel().attr(ChannelConfiguration.CHANNEL_IDLE_TIME_ATTRIBUTE_KEY).set(connectMessage.variableHeader().keepAliveTimeSeconds()); returnCode = MqttConnectReturnCode.CONNECTION_ACCEPTED; } ackMessage = MessageUtil.getMqttConnackMessage(connectMessage, returnCode); ctx.writeAndFlush(ackMessage); }
void processConnect(MqttConnectMessage msg) { MqttConnectPayload payload = msg.payload(); String clientId = payload.clientIdentifier(); final String username = payload.userName();
MqttConnectPayload payload = message.payload(); MqttVersion mqttVersion = MqttVersion.fromProtocolNameAndLevel(variableHeader.name(), (byte) variableHeader.version());
public byte[] getWillMessage() { return msg.payload().willMessage().getBytes(StandardCharsets.UTF_8); } }
public String getUsername() { return msg.payload().userName(); }
public byte[] getPassword() { return msg.payload().password().getBytes(StandardCharsets.UTF_8); }
@Override public void notifyClientConnected(final MqttConnectMessage msg) { for (final InterceptHandler handler : this.handlers.get(InterceptConnectMessage.class)) { LOG.debug("Sending MQTT CONNECT message to interceptor. CId={}, interceptorId={}", msg.payload().clientIdentifier(), handler.getID()); executor.execute(() -> handler.onConnect(new InterceptConnectMessage(msg))); } }
/** * Called during connection. * * @param connect */ void handleConnect(MqttConnectMessage connect, ChannelHandlerContext ctx) throws Exception { this.ctx = ctx; connectionEntry.ttl = connect.variableHeader().keepAliveTimeSeconds() * 1500L; String clientId = connect.payload().clientIdentifier(); session.getConnectionManager().connect(clientId, connect.payload().userName(), connect.payload().passwordInBytes(), connect.variableHeader().isWillFlag(), connect.payload().willMessageInBytes(), connect.payload().willTopic(), connect.variableHeader().isWillRetain(), connect.variableHeader().willQos(), connect.variableHeader().isCleanSession()); }
/** * Called during connection. * * @param connect */ void handleConnect(MqttConnectMessage connect, ChannelHandlerContext ctx) throws Exception { this.ctx = ctx; connectionEntry.ttl = connect.variableHeader().keepAliveTimeSeconds() * 1500L; String clientId = connect.payload().clientIdentifier(); session.getConnectionManager().connect(clientId, connect.payload().userName(), connect.payload().passwordInBytes(), connect.variableHeader().isWillFlag(), connect.payload().willMessageInBytes(), connect.payload().willTopic(), connect.variableHeader().isWillRetain(), connect.variableHeader().willQos(), connect.variableHeader().isCleanSession()); }
@Test public void testConnectMessageForMqtt311() throws Exception { final MqttConnectMessage message = createConnectMessage(MqttVersion.MQTT_3_1_1); ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message); final List<Object> out = new LinkedList<>(); mqttDecoder.decode(ctx, byteBuf, out); assertEquals("Expected one object bout got " + out.size(), 1, out.size()); final MqttConnectMessage decodedMessage = (MqttConnectMessage) out.get(0); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateConnectVariableHeader(message.variableHeader(), decodedMessage.variableHeader()); validateConnectPayload(message.payload(), decodedMessage.payload()); }
@Test public void testConnectMessageForMqtt31() throws Exception { final MqttConnectMessage message = createConnectMessage(MqttVersion.MQTT_3_1); ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message); final List<Object> out = new LinkedList<>(); mqttDecoder.decode(ctx, byteBuf, out); assertEquals("Expected one object bout got " + out.size(), 1, out.size()); final MqttConnectMessage decodedMessage = (MqttConnectMessage) out.get(0); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateConnectVariableHeader(message.variableHeader(), decodedMessage.variableHeader()); validateConnectPayload(message.payload(), decodedMessage.payload()); }