Skip to content

Commit

Permalink
Refactor attribute access for OAuth2 token handling #deploy-idporten-…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
krharum committed Dec 18, 2024
1 parent 97a7508 commit 343b6c8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,9 +33,9 @@ public Mono<Token> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private Mono<String> getTokenAttribute(String attribute) {
jwtAuthenticationToken.getTokenAttributes().get(attribute).toString();

case OAuth2AuthenticationToken oauth2AuthenticationToken ->
oauth2AuthenticationToken.getPrincipal().getAttribute("pid");
oauth2AuthenticationToken.getPrincipal().getAttributes().get("pid").toString();

default -> "";
};
Expand Down

0 comments on commit 343b6c8

Please sign in to comment.