From 0174c86bf04d2db6369d6d82977cebd868a60887 Mon Sep 17 00:00:00 2001 From: Stephan Krusche Date: Sun, 22 Dec 2024 13:52:52 +0100 Subject: [PATCH] Avoid an issue when a token is outdated and users want to login --- .../java/de/tum/cit/aet/GeneratedByJHipster.java | 13 ------------- .../simulation/SimulationScheduleService.java | 4 ++-- .../cit/aet/web/rest/AuthenticateController.java | 12 ++++-------- .../webapp/app/core/interceptor/auth.interceptor.ts | 6 ++++++ 4 files changed, 12 insertions(+), 23 deletions(-) delete mode 100644 src/main/java/de/tum/cit/aet/GeneratedByJHipster.java diff --git a/src/main/java/de/tum/cit/aet/GeneratedByJHipster.java b/src/main/java/de/tum/cit/aet/GeneratedByJHipster.java deleted file mode 100644 index a5991385..00000000 --- a/src/main/java/de/tum/cit/aet/GeneratedByJHipster.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.tum.cit.aet; - -import jakarta.annotation.Generated; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Generated(value = "JHipster", comments = "Generated by JHipster 8.1.0") -@Retention(RetentionPolicy.SOURCE) -@Target({ ElementType.TYPE }) -public @interface GeneratedByJHipster { -} diff --git a/src/main/java/de/tum/cit/aet/service/simulation/SimulationScheduleService.java b/src/main/java/de/tum/cit/aet/service/simulation/SimulationScheduleService.java index d4e17943..d6c3b6d7 100644 --- a/src/main/java/de/tum/cit/aet/service/simulation/SimulationScheduleService.java +++ b/src/main/java/de/tum/cit/aet/service/simulation/SimulationScheduleService.java @@ -145,8 +145,8 @@ public void unsubscribeFromSchedule(String key) { */ @Scheduled(fixedRate = 1000 * 60, initialDelay = 0) void executeScheduledSimulations() { - log.info("Executing scheduled simulation runs"); - var simulationSchedules = simulationScheduleRepository.findAll(); + final var simulationSchedules = simulationScheduleRepository.findAll(); + log.info("Executing {} scheduled simulation runs", simulationSchedules.size()); simulationSchedules .stream() .filter(simulationSchedule -> simulationSchedule.getNextRun().isBefore(now())) diff --git a/src/main/java/de/tum/cit/aet/web/rest/AuthenticateController.java b/src/main/java/de/tum/cit/aet/web/rest/AuthenticateController.java index b6891ec4..ec4225f0 100644 --- a/src/main/java/de/tum/cit/aet/web/rest/AuthenticateController.java +++ b/src/main/java/de/tum/cit/aet/web/rest/AuthenticateController.java @@ -79,12 +79,8 @@ public String createToken(Authentication authentication, boolean rememberMe) { String authorities = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.joining(" ")); Instant now = Instant.now(); - Instant validity; - if (rememberMe) { - validity = now.plus(this.tokenValidityInSecondsForRememberMe, ChronoUnit.SECONDS); - } else { - validity = now.plus(this.tokenValidityInSeconds, ChronoUnit.SECONDS); - } + long amountToAdd = rememberMe ? this.tokenValidityInSecondsForRememberMe : this.tokenValidityInSeconds; + Instant validity = now.plus(amountToAdd, ChronoUnit.SECONDS); // @formatter:off JwtClaimsSet claims = JwtClaimsSet.builder() @@ -93,11 +89,11 @@ public String createToken(Authentication authentication, boolean rememberMe) { .subject(authentication.getName()) .claim(AUTHORITIES_KEY, authorities) .build(); + // @formatter:on JwsHeader jwsHeader = JwsHeader.with(JWT_ALGORITHM).build(); return this.jwtEncoder.encode(JwtEncoderParameters.from(jwsHeader, claims)).getTokenValue(); } - public record JWTToken(@JsonProperty("id_token") String idToken) { - } + public record JWTToken(@JsonProperty("id_token") String idToken) {} } diff --git a/src/main/webapp/app/core/interceptor/auth.interceptor.ts b/src/main/webapp/app/core/interceptor/auth.interceptor.ts index 5ee0a114..cd5faca6 100644 --- a/src/main/webapp/app/core/interceptor/auth.interceptor.ts +++ b/src/main/webapp/app/core/interceptor/auth.interceptor.ts @@ -14,9 +14,15 @@ export class AuthInterceptor implements HttpInterceptor { intercept(request: HttpRequest, next: HttpHandler): Observable> { const serverApiUrl = this.applicationConfigService.getEndpointFor(''); + if (!request.url || (request.url.startsWith('http') && !(serverApiUrl && request.url.startsWith(serverApiUrl)))) { return next.handle(request); } + // NOTE: do not add the token to requests that do not expect it (e.g. authenticate or forget password) + const allowedUrls = ['/authenticate', '/account/reset-password/init', '/account/reset-password/finish']; + if (allowedUrls.some(url => request.url.endsWith(url))) { + return next.handle(request); + } const token: string | null = this.stateStorageService.getAuthenticationToken(); if (token) {