From 343b6c803847876ef4cf8177b7128192d6126217 Mon Sep 17 00:00:00 2001 From: "Kristen.Herum" Date: Wed, 18 Dec 2024 11:29:47 +0100 Subject: [PATCH] Refactor attribute access for OAuth2 token handling #deploy-idporten-frontend Updated methods to use `getAttributes()` instead of `getAttribute()` for accessing "pid" and "exp" fields in OAuth2 tokens. Ensures better compatibility and prevents potential type mismatch issues. --- .../libs/reactivesecurity/action/GetAuthenticatedToken.java | 5 +++-- .../libs/reactivesecurity/action/GetAuthenticatedUserId.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/libs/reactive-security/src/main/java/no/nav/testnav/libs/reactivesecurity/action/GetAuthenticatedToken.java b/libs/reactive-security/src/main/java/no/nav/testnav/libs/reactivesecurity/action/GetAuthenticatedToken.java index 47e94a16d70..0a5d4e957fd 100644 --- a/libs/reactive-security/src/main/java/no/nav/testnav/libs/reactivesecurity/action/GetAuthenticatedToken.java +++ b/libs/reactive-security/src/main/java/no/nav/testnav/libs/reactivesecurity/action/GetAuthenticatedToken.java @@ -12,6 +12,7 @@ import org.springframework.web.server.ResponseStatusException; import reactor.core.publisher.Mono; +import java.time.Instant; import java.util.concurrent.Callable; @Component @@ -32,9 +33,9 @@ public Mono call() { try { sink.next(Token.builder() .clientCredentials(false) - .userId(oauth2.getPrincipal().getAttribute("pid")) + .userId(oauth2.getPrincipal().getAttributes().get("pid").toString()) .accessTokenValue(new ObjectMapper().writeValueAsString(oauth2)) - .expiresAt(oauth2.getPrincipal().getAttribute("exp")) + .expiresAt((Instant) oauth2.getPrincipal().getAttributes().get("exp")) .build()); } catch (JsonProcessingException e) { sink.error(new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Feilet å konvertere token to string", e)); diff --git a/libs/reactive-security/src/main/java/no/nav/testnav/libs/reactivesecurity/action/GetAuthenticatedUserId.java b/libs/reactive-security/src/main/java/no/nav/testnav/libs/reactivesecurity/action/GetAuthenticatedUserId.java index e5b514f1fb3..69b46d0c7d2 100644 --- a/libs/reactive-security/src/main/java/no/nav/testnav/libs/reactivesecurity/action/GetAuthenticatedUserId.java +++ b/libs/reactive-security/src/main/java/no/nav/testnav/libs/reactivesecurity/action/GetAuthenticatedUserId.java @@ -38,7 +38,7 @@ private Mono getTokenAttribute(String attribute) { jwtAuthenticationToken.getTokenAttributes().get(attribute).toString(); case OAuth2AuthenticationToken oauth2AuthenticationToken -> - oauth2AuthenticationToken.getPrincipal().getAttribute("pid"); + oauth2AuthenticationToken.getPrincipal().getAttributes().get("pid").toString(); default -> ""; };