@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)); } }
byte[] password = payload.passwordInBytes(); byte[] passwordBytes = password != null ? password : EmptyArrays.EMPTY_BYTES; if (variableHeader.hasPassword()) {
@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); }
byte[] password = payload.passwordInBytes(); byte[] passwordBytes = password != null ? password : EmptyArrays.EMPTY_BYTES; if (variableHeader.hasPassword()) {
String password = msg.payload().passwordInBytes() == null ? null : new String(msg.payload().passwordInBytes(), CharsetUtil.UTF_8); if (!authService.checkValid(username, password)) { MqttConnAckMessage connAckMessage = (MqttConnAckMessage) MqttMessageFactory.newMessage(
/** * 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()); }