Skip to content

Commit 8f9f4c2

Browse files
committed
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.
1 parent 12bd363 commit 8f9f4c2

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

libs/reactive-security/src/main/java/no/nav/testnav/libs/reactivesecurity/action/GetAuthenticatedToken.java

+7-13
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,13 @@ public Mono<Token> call() {
2828
.call()
2929
.flatMap(serverType -> switch (serverType) {
3030
case TOKEN_X -> getJwtAuthenticationToken()
31-
.map(OAuth2AuthenticationToken.class::cast)
32-
.handle((oauth2, sink) -> {
33-
try {
34-
sink.next(Token.builder()
35-
.clientCredentials(false)
36-
.userId(oauth2.getPrincipal().getAttributes().get("pid").toString())
37-
.accessTokenValue(new ObjectMapper().writeValueAsString(oauth2))
38-
.expiresAt((Instant) oauth2.getPrincipal().getAttributes().get("exp"))
39-
.build());
40-
} catch (JsonProcessingException e) {
41-
sink.error(new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Feilet å konvertere token to string", e));
42-
}
43-
});
31+
.map(JwtAuthenticationToken.class::cast)
32+
.map(jwt -> Token.builder()
33+
.clientCredentials(false)
34+
.userId(jwt.getTokenAttributes().get("pid").toString())
35+
.accessTokenValue(jwt.getToken().getTokenValue())
36+
.expiresAt(jwt.getToken().getExpiresAt())
37+
.build());
4438
case AZURE_AD -> getJwtAuthenticationToken()
4539
.map(JwtAuthenticationToken.class::cast)
4640
.map(jwt -> Token.builder()

0 commit comments

Comments
 (0)