From 8f9f4c21b272f331b713248b509018d15dc9199d Mon Sep 17 00:00:00 2001 From: "Kristen.Herum" Date: Fri, 20 Dec 2024 11:36:30 +0100 Subject: [PATCH] Refactor token handling for TOKEN_X authentication #deploy-bruker-service Simplified and corrected the TOKEN_X token mapping logic by replacing the use of `ObjectMapper` and error handling with direct attribute access. This improves clarity and reduces potential processing errors. --- .../action/GetAuthenticatedToken.java | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 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 0a5d4e957fd..c69106248ed 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 @@ -28,19 +28,13 @@ public Mono call() { .call() .flatMap(serverType -> switch (serverType) { case TOKEN_X -> getJwtAuthenticationToken() - .map(OAuth2AuthenticationToken.class::cast) - .handle((oauth2, sink) -> { - try { - sink.next(Token.builder() - .clientCredentials(false) - .userId(oauth2.getPrincipal().getAttributes().get("pid").toString()) - .accessTokenValue(new ObjectMapper().writeValueAsString(oauth2)) - .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)); - } - }); + .map(JwtAuthenticationToken.class::cast) + .map(jwt -> Token.builder() + .clientCredentials(false) + .userId(jwt.getTokenAttributes().get("pid").toString()) + .accessTokenValue(jwt.getToken().getTokenValue()) + .expiresAt(jwt.getToken().getExpiresAt()) + .build()); case AZURE_AD -> getJwtAuthenticationToken() .map(JwtAuthenticationToken.class::cast) .map(jwt -> Token.builder()