Skip to content

Commit

Permalink
Fix: Ensure correct profile retrieval for TokenX users in ProfilServi…
Browse files Browse the repository at this point in the history
…ce #deploy-profil-api
  • Loading branch information
krharum committed Jan 22, 2025
1 parent 53e38ce commit d247541
Showing 1 changed file with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service
Expand All @@ -28,7 +27,23 @@ public class ProfilService {

public Profil getProfile() {
if (isTokenX()) {
return getTokenXAttributes();
return getUserInfo.call()
.map(userInfo -> new Profil(
userInfo.brukernavn(),
UKJENT,
UKJENT,
UKJENT,
userInfo.organisasjonsnummer(),
BANK_ID)
)
.orElse(new Profil(
BANK_ID,
UKJENT,
UKJENT,
UKJENT,
UKJENT,
BANK_ID
));
}
return azureAdProfileConsumer.getProfil();
}
Expand All @@ -37,40 +52,18 @@ public Optional<byte[]> getImage() {
return isTokenX() ? Optional.empty() : azureAdProfileConsumer.getProfilImage();
}

private Optional<JwtAuthenticationToken> getJwtAuthenticationToken() {
private JwtAuthenticationToken getJwtAuthenticationToken() {
return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
.filter(JwtAuthenticationToken.class::isInstance)
.map(JwtAuthenticationToken.class::cast)
.map(t -> {
log.info("JwtAuthenticationToken, attributes: {}",
t.getTokenAttributes().entrySet().stream()
.map(entry -> entry.getKey() + ":" + entry.getValue())
.collect(Collectors.joining(",")));
return t;
});
}

private boolean isTokenX() {

return getJwtAuthenticationToken()
.map(token -> token
.getTokenAttributes()
.get(JwtClaimNames.ISS)
.equals(tokenXResourceServerProperties.getIssuerUri()))
.orElseThrow();
}

private Profil getTokenXAttributes() {
private boolean isTokenX() {

return getJwtAuthenticationToken()
.map(JwtAuthenticationToken::getTokenAttributes)
.map(attribs -> new Profil(
(String) attribs.get("brukernavn"),
UKJENT,
UKJENT,
UKJENT,
(String) attribs.get("org"),
BANK_ID))
.orElseThrow();
.getTokenAttributes()
.get(JwtClaimNames.ISS)
.equals(tokenXResourceServerProperties.getIssuerUri());
}
}

0 comments on commit d247541

Please sign in to comment.