diff --git a/cameleer3-server-app/src/main/java/com/cameleer3/server/app/controller/AgentRegistrationController.java b/cameleer3-server-app/src/main/java/com/cameleer3/server/app/controller/AgentRegistrationController.java index ffe863dd..53dea12c 100644 --- a/cameleer3-server-app/src/main/java/com/cameleer3/server/app/controller/AgentRegistrationController.java +++ b/cameleer3-server-app/src/main/java/com/cameleer3/server/app/controller/AgentRegistrationController.java @@ -173,17 +173,19 @@ public class AgentRegistrationController { return ResponseEntity.status(401).build(); } - // Verify agent exists - AgentInfo agent = registryService.findById(agentId); - if (agent == null) { - return ResponseEntity.notFound().build(); - } - - // Preserve roles from refresh token + // Preserve roles and application from refresh token List roles = result.roles().isEmpty() ? List.of("AGENT") : result.roles(); - String newAccessToken = jwtService.createAccessToken(agentId, agent.applicationId(), roles); - String newRefreshToken = jwtService.createRefreshToken(agentId, agent.applicationId(), roles); + String application = result.application() != null ? result.application() : "default"; + + // Try to get application from registry if available (agent may not be registered after server restart) + AgentInfo agent = registryService.findById(agentId); + if (agent != null) { + application = agent.applicationId(); + } + + String newAccessToken = jwtService.createAccessToken(agentId, application, roles); + String newRefreshToken = jwtService.createRefreshToken(agentId, application, roles); auditService.log(agentId, "agent_token_refresh", AuditCategory.AUTH, agentId, null, AuditResult.SUCCESS, httpRequest);