|
|
@ -19,6 +19,7 @@ import org.springframework.beans.factory.BeanCreationNotAllowedException; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.context.annotation.Lazy; |
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
|
|
import org.springframework.security.authentication.BadCredentialsException; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.web.socket.CloseStatus; |
|
|
import org.springframework.web.socket.CloseStatus; |
|
|
import org.springframework.web.socket.PongMessage; |
|
|
import org.springframework.web.socket.PongMessage; |
|
|
@ -34,6 +35,7 @@ import org.thingsboard.server.common.data.id.CustomerId; |
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
import org.thingsboard.server.common.data.id.UserId; |
|
|
import org.thingsboard.server.common.data.id.UserId; |
|
|
import org.thingsboard.server.common.data.limit.LimitedApi; |
|
|
import org.thingsboard.server.common.data.limit.LimitedApi; |
|
|
|
|
|
import org.thingsboard.server.common.data.security.Authority; |
|
|
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; |
|
|
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; |
|
|
import org.thingsboard.server.config.WebSocketConfiguration; |
|
|
import org.thingsboard.server.config.WebSocketConfiguration; |
|
|
import org.thingsboard.server.dao.tenant.TbTenantProfileCache; |
|
|
import org.thingsboard.server.dao.tenant.TbTenantProfileCache; |
|
|
@ -56,6 +58,7 @@ import org.thingsboard.server.service.ws.telemetry.cmd.TelemetryCmdsWrapper; |
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.security.InvalidParameterException; |
|
|
import java.security.InvalidParameterException; |
|
|
|
|
|
import java.util.EnumSet; |
|
|
import java.util.Optional; |
|
|
import java.util.Optional; |
|
|
import java.util.Queue; |
|
|
import java.util.Queue; |
|
|
import java.util.Set; |
|
|
import java.util.Set; |
|
|
@ -77,6 +80,8 @@ import static org.thingsboard.server.service.ws.DefaultWebSocketService.NUMBER_O |
|
|
@RequiredArgsConstructor |
|
|
@RequiredArgsConstructor |
|
|
public class TbWebSocketHandler extends TextWebSocketHandler implements WebSocketMsgEndpoint { |
|
|
public class TbWebSocketHandler extends TextWebSocketHandler implements WebSocketMsgEndpoint { |
|
|
|
|
|
|
|
|
|
|
|
private static final Set<Authority> USER_AUTHORITIES = EnumSet.of(Authority.SYS_ADMIN, Authority.TENANT_ADMIN, Authority.CUSTOMER_USER); |
|
|
|
|
|
|
|
|
private final ConcurrentMap<String, SessionMetaData> internalSessionMap = new ConcurrentHashMap<>(); |
|
|
private final ConcurrentMap<String, SessionMetaData> internalSessionMap = new ConcurrentHashMap<>(); |
|
|
private final ConcurrentMap<String, String> externalSessionMap = new ConcurrentHashMap<>(); |
|
|
private final ConcurrentMap<String, String> externalSessionMap = new ConcurrentHashMap<>(); |
|
|
|
|
|
|
|
|
@ -187,7 +192,7 @@ public class TbWebSocketHandler extends TextWebSocketHandler implements WebSocke |
|
|
if (StringUtils.isNotEmpty(authCmd.getApiKey())) { |
|
|
if (StringUtils.isNotEmpty(authCmd.getApiKey())) { |
|
|
securityCtx = apiKeyAuthenticationProvider.authenticate(authCmd.getApiKey()); |
|
|
securityCtx = apiKeyAuthenticationProvider.authenticate(authCmd.getApiKey()); |
|
|
} else { |
|
|
} else { |
|
|
securityCtx = authenticationProvider.authenticate(authCmd.getToken()); |
|
|
securityCtx = authenticate(authCmd.getToken()); |
|
|
} |
|
|
} |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
close(sessionRef, CloseStatus.BAD_DATA.withReason(e.getMessage())); |
|
|
close(sessionRef, CloseStatus.BAD_DATA.withReason(e.getMessage())); |
|
|
@ -326,7 +331,7 @@ public class TbWebSocketHandler extends TextWebSocketHandler implements WebSocke |
|
|
if (query != null) { |
|
|
if (query != null) { |
|
|
String token = extractQueryParam(query, "token"); |
|
|
String token = extractQueryParam(query, "token"); |
|
|
if (StringUtils.isNotEmpty(token)) { |
|
|
if (StringUtils.isNotEmpty(token)) { |
|
|
securityCtx = authenticationProvider.authenticate(token); |
|
|
securityCtx = authenticate(token); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return WebSocketSessionRef.builder() |
|
|
return WebSocketSessionRef.builder() |
|
|
@ -347,6 +352,14 @@ public class TbWebSocketHandler extends TextWebSocketHandler implements WebSocke |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private SecurityUser authenticate(String token) { |
|
|
|
|
|
SecurityUser securityUser = authenticationProvider.authenticate(token); |
|
|
|
|
|
if (!USER_AUTHORITIES.contains(securityUser.getAuthority())) { |
|
|
|
|
|
throw new BadCredentialsException("Token is invalid"); |
|
|
|
|
|
} |
|
|
|
|
|
return securityUser; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private SessionMetaData getSessionMd(String internalSessionId) { |
|
|
private SessionMetaData getSessionMd(String internalSessionId) { |
|
|
SessionMetaData sessionMd = internalSessionMap.get(internalSessionId); |
|
|
SessionMetaData sessionMd = internalSessionMap.get(internalSessionId); |
|
|
if (sessionMd == null) { |
|
|
if (sessionMd == null) { |
|
|
|