-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor token attribute retrieval in reactive security. #deploy-idpo…
…rten-frontend Replaced direct Jwt token access with a context-based approach to support both Jwt and OAuth2 authentication tokens. Introduced a new `Oauth2Resolver` utility for secure handling of tokens and expiration checks. This improves flexibility and enhances token handling consistency.
- Loading branch information
Showing
2 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ve-security/src/main/java/no/nav/testnav/libs/reactivesecurity/action/Oauth2Resolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package no.nav.testnav.libs.reactivesecurity.action; | ||
|
||
import lombok.experimental.UtilityClass; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.authentication.CredentialsExpiredException; | ||
import org.springframework.security.core.context.ReactiveSecurityContextHolder; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.time.Instant; | ||
import java.time.ZonedDateTime; | ||
|
||
@Slf4j | ||
@UtilityClass | ||
public class Oauth2Resolver { | ||
|
||
public static Mono<OAuth2AuthenticationToken> getOauth2AuthenticationToken() { | ||
return ReactiveSecurityContextHolder | ||
.getContext() | ||
.switchIfEmpty(Mono.error(new JwtResolverException("ReactiveSecurityContext is empty"))) | ||
.doOnNext(context -> log.info("context.authentication {}", context.getAuthentication())) | ||
.map(SecurityContext::getAuthentication) | ||
.map(OAuth2AuthenticationToken.class::cast) | ||
.doOnError(throwable -> log.warn("Klarte ikke hente Jwt Auth Token", throwable)) | ||
.doOnSuccess(jwtAuthenticationToken -> { | ||
Jwt credentials = (Jwt) jwtAuthenticationToken.getCredentials(); | ||
Instant expiresAt = credentials.getExpiresAt(); | ||
if (expiresAt == null || expiresAt.isBefore(ZonedDateTime.now().toInstant().plusSeconds(120))) { | ||
throw new CredentialsExpiredException("Jwt er utløpt eller utløper innen kort tid"); | ||
} | ||
}); | ||
} | ||
} |