2026-03-11 20:38:28 +01:00
|
|
|
package com.cameleer3.server.app;
|
|
|
|
|
|
|
|
|
|
import com.cameleer3.server.core.agent.AgentRegistryService;
|
|
|
|
|
import com.cameleer3.server.core.security.JwtService;
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test utility for creating JWT-authenticated requests in integration tests.
|
|
|
|
|
* <p>
|
|
|
|
|
* Registers a test agent and issues a JWT access token that can be used
|
|
|
|
|
* to authenticate against protected endpoints.
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
public class TestSecurityHelper {
|
|
|
|
|
|
|
|
|
|
private final JwtService jwtService;
|
|
|
|
|
private final AgentRegistryService agentRegistryService;
|
|
|
|
|
|
|
|
|
|
public TestSecurityHelper(JwtService jwtService, AgentRegistryService agentRegistryService) {
|
|
|
|
|
this.jwtService = jwtService;
|
|
|
|
|
this.agentRegistryService = agentRegistryService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-16 20:03:38 +01:00
|
|
|
* Registers a test agent and returns a valid JWT access token with AGENT role.
|
2026-03-11 20:38:28 +01:00
|
|
|
*/
|
|
|
|
|
public String registerTestAgent(String agentId) {
|
|
|
|
|
agentRegistryService.register(agentId, "test", "test-group", "1.0", List.of(), Map.of());
|
2026-03-16 20:03:38 +01:00
|
|
|
return jwtService.createAccessToken(agentId, "test-group", List.of("AGENT"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a valid JWT access token with the given roles (no agent registration).
|
|
|
|
|
*/
|
2026-03-24 08:48:12 +01:00
|
|
|
public String createToken(String subject, String application, List<String> roles) {
|
|
|
|
|
return jwtService.createAccessToken(subject, application, roles);
|
2026-03-16 20:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a valid JWT access token with OPERATOR role.
|
|
|
|
|
*/
|
|
|
|
|
public String operatorToken() {
|
2026-03-16 21:01:57 +01:00
|
|
|
// Subject must start with "user:" for JwtAuthenticationFilter to treat it as a UI user token
|
|
|
|
|
return jwtService.createAccessToken("user:test-operator", "user", List.of("OPERATOR"));
|
2026-03-16 20:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a valid JWT access token with ADMIN role.
|
|
|
|
|
*/
|
|
|
|
|
public String adminToken() {
|
2026-03-16 21:01:57 +01:00
|
|
|
return jwtService.createAccessToken("user:test-admin", "user", List.of("ADMIN"));
|
2026-03-16 20:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a valid JWT access token with VIEWER role.
|
|
|
|
|
*/
|
|
|
|
|
public String viewerToken() {
|
2026-03-16 21:01:57 +01:00
|
|
|
return jwtService.createAccessToken("user:test-viewer", "user", List.of("VIEWER"));
|
2026-03-11 20:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns HttpHeaders with JWT Bearer authorization, protocol version, and JSON content type.
|
|
|
|
|
*/
|
|
|
|
|
public HttpHeaders authHeaders(String jwt) {
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
headers.set("Authorization", "Bearer " + jwt);
|
|
|
|
|
headers.set("X-Cameleer-Protocol-Version", "1");
|
|
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
return headers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns HttpHeaders with JWT Bearer authorization and protocol version (no content type).
|
|
|
|
|
*/
|
|
|
|
|
public HttpHeaders authHeadersNoBody(String jwt) {
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
headers.set("Authorization", "Bearer " + jwt);
|
|
|
|
|
headers.set("X-Cameleer-Protocol-Version", "1");
|
|
|
|
|
return headers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns HttpHeaders with bootstrap token authorization, protocol version, and JSON content type.
|
|
|
|
|
*/
|
|
|
|
|
public HttpHeaders bootstrapHeaders() {
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
headers.set("Authorization", "Bearer test-bootstrap-token");
|
|
|
|
|
headers.set("X-Cameleer-Protocol-Version", "1");
|
|
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
return headers;
|
|
|
|
|
}
|
|
|
|
|
}
|