fix: agent token refresh returns 404 after server restart
All checks were successful
CI / cleanup-branch (push) Has been skipped
CI / build (push) Successful in 1m8s
CI / docker (push) Successful in 42s
CI / deploy-feature (push) Has been skipped
CI / deploy (push) Successful in 1m23s

The refresh endpoint required the agent to exist in the in-memory
registry. After server restart the registry is empty, so all refresh
attempts got 404. The refresh token itself is self-contained with
subject, application, and roles — the registry lookup is optional.

Now uses application from the JWT, falling back to registry only
if the agent happens to be registered.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
hsiegeln
2026-04-03 10:37:57 +02:00
parent bdac363e40
commit 0632f1c6a8

View File

@@ -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<String> 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);