|
|
|
@ -19,6 +19,7 @@ import org.springframework.beans.factory.BeanCreationNotAllowedException; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
import org.springframework.security.authentication.BadCredentialsException; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.web.socket.CloseStatus; |
|
|
|
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.UserId; |
|
|
|
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.config.WebSocketConfiguration; |
|
|
|
import org.thingsboard.server.dao.tenant.TbTenantProfileCache; |
|
|
|
@ -55,6 +57,7 @@ import org.thingsboard.server.service.ws.telemetry.cmd.TelemetryCmdsWrapper; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.security.InvalidParameterException; |
|
|
|
import java.util.EnumSet; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.Queue; |
|
|
|
import java.util.Set; |
|
|
|
@ -76,6 +79,8 @@ import static org.thingsboard.server.service.ws.DefaultWebSocketService.NUMBER_O |
|
|
|
@RequiredArgsConstructor |
|
|
|
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, String> externalSessionMap = new ConcurrentHashMap<>(); |
|
|
|
|
|
|
|
@ -181,7 +186,7 @@ public class TbWebSocketHandler extends TextWebSocketHandler implements WebSocke |
|
|
|
log.trace("{} Authenticating session", sessionRef); |
|
|
|
SecurityUser securityCtx; |
|
|
|
try { |
|
|
|
securityCtx = authenticationProvider.authenticate(authCmd.getToken()); |
|
|
|
securityCtx = authenticate(authCmd.getToken()); |
|
|
|
} catch (Exception e) { |
|
|
|
close(sessionRef, CloseStatus.BAD_DATA.withReason(e.getMessage())); |
|
|
|
return; |
|
|
|
@ -317,7 +322,7 @@ public class TbWebSocketHandler extends TextWebSocketHandler implements WebSocke |
|
|
|
SecurityUser securityCtx = null; |
|
|
|
String token = StringUtils.substringAfter(session.getUri().getQuery(), "token="); |
|
|
|
if (StringUtils.isNotEmpty(token)) { |
|
|
|
securityCtx = authenticationProvider.authenticate(token); |
|
|
|
securityCtx = authenticate(token); |
|
|
|
} |
|
|
|
return WebSocketSessionRef.builder() |
|
|
|
.sessionId(UUID.randomUUID().toString()) |
|
|
|
@ -328,6 +333,14 @@ public class TbWebSocketHandler extends TextWebSocketHandler implements WebSocke |
|
|
|
.build(); |
|
|
|
} |
|
|
|
|
|
|
|
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) { |
|
|
|
SessionMetaData sessionMd = internalSessionMap.get(internalSessionId); |
|
|
|
if (sessionMd == null) { |
|
|
|
|