Improve subscribe endpoint in CommandsToServerController: add error logging for missing or invalid tokens

This commit is contained in:
akastijn 2026-08-02 17:30:28 +02:00
parent 8849612138
commit d3389159b6

View File

@ -24,17 +24,19 @@ public class CommandsToServerController {
private String validToken;
@GetMapping(path = "/subscribe/{server}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter subscribe(@RequestHeader(value = "X-Altitude-Token") String token,
public SseEmitter subscribe(@RequestHeader(value = "X-Altitude-Token", required = false) String token,
HttpServletResponse response, @PathVariable String server) {
if (validToken == null || validToken.equals("invalid-token")) {
log.error("Invalid token in config");
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
}
if (token == null) {
log.error("No token provided when trying to subscribe to {}", server);
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
}
if (!token.equals(validToken)) {
log.error("Invalid token provided when trying to subscribe to {}", server);
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
}