Files
IM-server-new/broker/src/main/java/io/moquette/imhandler/GetTokenHandler.java
T

39 lines
1.7 KiB
Java
Raw Normal View History

2019-03-16 17:04:23 +08:00
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package io.moquette.imhandler;
import cn.wildfirechat.proto.ProtoConstants;
2019-03-16 17:04:23 +08:00
import cn.wildfirechat.proto.WFCMessage;
2021-11-16 16:49:09 +08:00
import com.xiaoleilu.hutool.system.UserInfo;
2019-03-16 17:04:23 +08:00
import io.moquette.persistence.MemorySessionStore;
import io.moquette.spi.impl.Qos1PublishHandler;
import io.moquette.spi.impl.security.TokenAuthenticator;
import io.netty.buffer.ByteBuf;
2019-05-22 20:24:21 +08:00
import cn.wildfirechat.common.ErrorCode;
2019-03-16 17:04:23 +08:00
import win.liyufan.im.IMTopic;
@Handler(IMTopic.GetTokenTopic)
public class GetTokenHandler extends IMHandler<WFCMessage.GetTokenRequest> {
@Override
public ErrorCode action(ByteBuf ackPayload, String clientID, String fromUser, ProtoConstants.RequestSourceType requestSourceType, WFCMessage.GetTokenRequest request, Qos1PublishHandler.IMCallback callback) {
2019-12-17 10:11:15 +08:00
MemorySessionStore.Session session = m_sessionsStore.updateOrCreateUserSession(fromUser, clientID, request.getPlatform());
2021-11-16 16:49:09 +08:00
WFCMessage.User userInfo = m_messagesStore.getUserInfo(fromUser);
if(userInfo != null && userInfo.getType() == 1) {
return ErrorCode.ERROR_CODE_ROBOT_NO_TOKEN;
}
2019-03-16 17:04:23 +08:00
TokenAuthenticator authenticator = new TokenAuthenticator();
String strToken = authenticator.generateToken(fromUser);
String result = strToken + "|" + session.getSecret() + "|" + session.getDbSecret();
byte[] data = result.getBytes();
ackPayload.ensureWritable(data.length).writeBytes(data);
return ErrorCode.ERROR_CODE_SUCCESS;
}
}