merge: 合并 origin/master 家属端远程协助票据
管理端票据与信令授权统一走登录会话校验,保留家属端一次性票据握手分支, 冲突测试本地与管理端、家属端用例均保留。
This commit is contained in:
commit
b456743342
6 files changed
+198
-9
No files matched your search
+67
@@ -0,0 +1,67 @@
|
||||
package com.youlai.boot.client.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
||||
import com.youlai.boot.client.model.entity.ClientUser;
|
||||
import com.youlai.boot.client.service.ClientUserService;
|
||||
import com.youlai.boot.device.model.entity.SnDeviceInfo;
|
||||
import com.youlai.boot.device.service.DeviceService;
|
||||
import com.youlai.boot.framework.security.model.SecurityUserDetails;
|
||||
import com.youlai.boot.framework.security.token.TokenManager;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class ClientRemoteControlControllerTest {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void ticketsRequireActiveBoundFamilyAndExpireAfterThirtySeconds() {
|
||||
var devices = mock(DeviceService.class);
|
||||
var users = mock(ClientUserService.class);
|
||||
var tokens = mock(TokenManager.class);
|
||||
var redis = mock(StringRedisTemplate.class, RETURNS_DEEP_STUBS);
|
||||
var controller = new ClientRemoteControlController(devices, users, tokens, redis);
|
||||
var principal = new SecurityUserDetails();
|
||||
principal.setUserId(1L);
|
||||
principal.setClientType("client");
|
||||
SecurityContextHolder.getContext().setAuthentication(
|
||||
new UsernamePasswordAuthenticationToken(principal, null, List.of()));
|
||||
try {
|
||||
when(tokens.validateToken("access")).thenReturn(true);
|
||||
var user = new ClientUser();
|
||||
user.setStatus(1);
|
||||
user.setMobile("123");
|
||||
when(users.getById(1L)).thenReturn(user);
|
||||
var device = new SnDeviceInfo();
|
||||
device.setStatus(1);
|
||||
device.setSnMobile("456");
|
||||
LambdaQueryChainWrapper<SnDeviceInfo> query = mock(LambdaQueryChainWrapper.class);
|
||||
when(devices.lambdaQuery()).thenReturn(query);
|
||||
when(query.eq(any(), eq("SN-1"))).thenReturn(query);
|
||||
when(query.one()).thenReturn(device);
|
||||
controller.ticket("SN-1", "Bearer access");
|
||||
verify(redis, never()).opsForValue();
|
||||
|
||||
device.setSnMobile("123");
|
||||
device.setStatus(0);
|
||||
controller.ticket("SN-1", "Bearer access");
|
||||
verify(redis, never()).opsForValue();
|
||||
device.setStatus(1);
|
||||
principal.setClientType("admin");
|
||||
controller.ticket("SN-1", "Bearer access");
|
||||
verify(redis, never()).opsForValue();
|
||||
principal.setClientType("client");
|
||||
controller.ticket("SN-1", "Bearer access");
|
||||
verify(redis.opsForValue()).set(startsWith("webrtc:family-ticket:"),
|
||||
eq("SN-1\naccess"), eq(Duration.ofSeconds(30)));
|
||||
} finally {
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,45 @@ class RemoteControlSecurityTest {
|
||||
assertFalse(interceptor.beforeHandshake(request, response, null, new HashMap<>()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void familyTicketIsSingleUseTargetBoundAndNeverAdministrator() {
|
||||
var redis = mock(StringRedisTemplate.class, RETURNS_DEEP_STUBS);
|
||||
var tokens = mock(TokenManager.class);
|
||||
var interceptor = new SignalHandshakeInterceptor(tokens, mock(SessionService.class),
|
||||
mock(DeviceService.class), mock(DeviceSignatureVerifier.class), redis);
|
||||
String ticket = "12345678-1234-1234-1234-123456789abc";
|
||||
String key = "webrtc:family-ticket:" + ticket;
|
||||
when(redis.opsForValue().getAndDelete(key)).thenReturn("SN-1\nfamily-access", (String) null);
|
||||
when(tokens.validateToken("family-access")).thenReturn(true);
|
||||
var details = new com.youlai.boot.framework.security.model.SecurityUserDetails();
|
||||
details.setUserId(1L);
|
||||
details.setUsername("family");
|
||||
details.setClientType("client");
|
||||
var authentication = mock(org.springframework.security.core.Authentication.class);
|
||||
when(authentication.getPrincipal()).thenReturn(details);
|
||||
when(tokens.parseToken("family-access")).thenReturn(authentication);
|
||||
var request = mock(ServerHttpRequest.class);
|
||||
when(request.getHeaders()).thenReturn(new HttpHeaders());
|
||||
when(request.getURI()).thenReturn(java.net.URI.create(
|
||||
"https://example.test/ws/signal?sn=SN-1&familyTicket=" + ticket));
|
||||
var attributes = new HashMap<String, Object>();
|
||||
assertTrue(interceptor.beforeHandshake(request, mock(ServerHttpResponse.class), null, attributes));
|
||||
org.junit.jupiter.api.Assertions.assertEquals("SN-1", attributes.get(ATTR_TARGET_SN));
|
||||
org.junit.jupiter.api.Assertions.assertEquals(1L, attributes.get(ATTR_USER_ID));
|
||||
assertFalse(attributes.containsKey(SignalHandshakeInterceptor.ATTR_ADMIN));
|
||||
assertFalse(interceptor.beforeHandshake(request, mock(ServerHttpResponse.class), null, new HashMap<>()));
|
||||
|
||||
when(redis.opsForValue().getAndDelete(key)).thenReturn("SN-2\nfamily-access");
|
||||
assertFalse(interceptor.beforeHandshake(request, mock(ServerHttpResponse.class), null, new HashMap<>()));
|
||||
when(redis.opsForValue().getAndDelete(key)).thenReturn("SN-1\nfamily-access");
|
||||
details.setClientType("admin");
|
||||
assertFalse(interceptor.beforeHandshake(request, mock(ServerHttpResponse.class), null, new HashMap<>()));
|
||||
details.setClientType("client");
|
||||
when(tokens.validateRefreshToken("family-access")).thenReturn(true);
|
||||
assertFalse(interceptor.beforeHandshake(request, mock(ServerHttpResponse.class), null, new HashMap<>()));
|
||||
verify(redis.opsForValue(), never()).getAndDelete("webrtc:admin-ticket:" + ticket);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deviceHandshakeRequiresRegisteredKeyAndConsumesNonceOnce() throws Exception {
|
||||
DeviceService devices = mock(DeviceService.class);
|
||||
|
||||
Reference in new issue
Block a user