Skip to content

Commit

Permalink
Refactor token handling for TOKEN_X authentication
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
krharum committed Dec 20, 2024
1 parent 12bd363 commit 8f9f4c2
Showing 1 changed file with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,13 @@ public Mono<Token> 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()
Expand Down

0 comments on commit 8f9f4c2

Please sign in to comment.