|
|
@@ -2,16 +2,18 @@ package org.jetlinks.community.network.mqtt.auth;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.hswebframework.web.exception.BusinessException;
|
|
|
+import org.jetlinks.core.ProtocolSupport;
|
|
|
import org.jetlinks.core.Value;
|
|
|
-import org.jetlinks.core.device.AuthenticationRequest;
|
|
|
-import org.jetlinks.core.device.AuthenticationResponse;
|
|
|
-import org.jetlinks.core.device.DeviceOperator;
|
|
|
-import org.jetlinks.core.device.MqttAuthenticationRequest;
|
|
|
+import org.jetlinks.core.device.*;
|
|
|
+import org.jetlinks.core.exception.DeviceOperationException;
|
|
|
+import org.jetlinks.core.message.codec.DefaultTransport;
|
|
|
+import org.jetlinks.core.message.codec.Transport;
|
|
|
import org.jetlinks.supports.official.JetLinksAuthenticator;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
-
|
|
|
import javax.annotation.Nonnull;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import static org.jetlinks.core.enums.ErrorCode.UNSUPPORTED_MESSAGE;
|
|
|
|
|
|
/**
|
|
|
* @author lifang
|
|
|
@@ -25,40 +27,37 @@ public class MqttDefaultAuth extends JetLinksAuthenticator {
|
|
|
public Mono<AuthenticationResponse> authenticate(@Nonnull AuthenticationRequest request, @Nonnull DeviceOperator deviceOperation) {
|
|
|
if (request instanceof MqttAuthenticationRequest) {
|
|
|
MqttAuthenticationRequest mqtt = ((MqttAuthenticationRequest) request);
|
|
|
- // secureId|timestamp
|
|
|
+ Transport transport = request.getTransport();
|
|
|
+ if(!transport.equals(DefaultTransport.MQTT) &&!transport.equals(DefaultTransport.MQTT_TLS)){
|
|
|
+ //非mqtt协议
|
|
|
+ return Mono.just(AuthenticationResponse.error(400, "设备不支持的连接协议类型:" + transport.getName()));
|
|
|
+ }
|
|
|
String username = mqtt.getUsername();
|
|
|
- // md5(secureId|timestamp|secureKey)
|
|
|
String password = mqtt.getPassword();
|
|
|
- String requestSecureId;
|
|
|
+
|
|
|
try {
|
|
|
-// String[] arr = username.split("[|]");
|
|
|
-// if (arr.length <= 1) {
|
|
|
-// return Mono.just(AuthenticationResponse.error(401, "用户名格式错误"));
|
|
|
-// }
|
|
|
-// requestSecureId = arr[0];
|
|
|
-// long time = Long.parseLong(arr[1]);
|
|
|
- //和设备时间差大于5分钟则认为无效
|
|
|
-// if (Math.abs(System.currentTimeMillis() - time) > TimeUnit.MINUTES.toMillis(5)) {
|
|
|
-// return Mono.just(AuthenticationResponse.error(401, "设备时间不同步"));
|
|
|
-// }
|
|
|
return deviceOperation
|
|
|
- .getConfigs("secureId", "secureKey")
|
|
|
- .map(conf -> {
|
|
|
- String secureId = conf.getValue("secureId").map(Value::asString).orElse(null);
|
|
|
+ .getProduct()
|
|
|
+ .flatMap(deviceProductOperator -> deviceProductOperator.getProtocol()
|
|
|
+ .flatMap(protocolSupport -> protocolSupport.authenticate(request,deviceOperation)))
|
|
|
+ .switchIfEmpty(Mono.error(()->new DeviceOperationException(UNSUPPORTED_MESSAGE)))
|
|
|
+ .flatMap(ignore->deviceOperation
|
|
|
+ .getConfigs("secureId", "secureKey")
|
|
|
+ .map(conf -> {
|
|
|
+ String secureId = conf.getValue("secureId").map(Value::asString).orElse(null);
|
|
|
|
|
|
- String secureKey = conf
|
|
|
- .getValue("secureKey")
|
|
|
- .map(Value::asString)
|
|
|
- .orElse(null);
|
|
|
- //签名
|
|
|
- String digest = DigestUtils.md5Hex(username + "|" + secureKey);
|
|
|
- if ((StrUtil.isEmpty(secureId)||username.equals(secureId))
|
|
|
- && (StrUtil.isEmpty(secureKey)||password.equals(secureKey))) {
|
|
|
- return AuthenticationResponse.success(deviceOperation.getDeviceId());
|
|
|
- } else {
|
|
|
- return AuthenticationResponse.error(401, "密钥错误");
|
|
|
- }
|
|
|
- });
|
|
|
+ String secureKey = conf
|
|
|
+ .getValue("secureKey")
|
|
|
+ .map(Value::asString)
|
|
|
+ .orElse(null);
|
|
|
+ //签名
|
|
|
+ if ((StrUtil.isEmpty(secureId)||username.equals(secureId))
|
|
|
+ && (StrUtil.isEmpty(secureKey)||password.equals(secureKey))) {
|
|
|
+ return AuthenticationResponse.success(deviceOperation.getDeviceId());
|
|
|
+ } else {
|
|
|
+ return AuthenticationResponse.error(401, "密钥错误");
|
|
|
+ }
|
|
|
+ }));
|
|
|
} catch (NumberFormatException e) {
|
|
|
return Mono.just(AuthenticationResponse.error(401, "用户名格式错误"));
|
|
|
}
|