Skip to content

Commit 80b6932

Browse files
committed
Oppdatering for å hente organisasjon #deploy-profil-api
1 parent 7f3b6a0 commit 80b6932

File tree

6 files changed

+47
-40
lines changed

6 files changed

+47
-40
lines changed

Diff for: apps/profil-api/src/main/java/no/nav/registre/testnorge/profil/consumer/AzureAdProfileConsumer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.stereotype.Service;
1111
import org.springframework.web.reactive.function.client.ExchangeStrategies;
1212
import org.springframework.web.reactive.function.client.WebClient;
13+
import reactor.core.publisher.Mono;
1314
import reactor.netty.http.client.HttpClient;
1415
import reactor.netty.transport.ProxyProvider;
1516

@@ -56,10 +57,10 @@ public AzureAdProfileConsumer(
5657
this.webClient = builder.build();
5758
}
5859

59-
public Profil getProfil() {
60+
public Mono<Profil> getProfil() {
6061
return azureAdTokenService.exchange(url + "/.default")
6162
.flatMap(accessToken -> new GetProfileCommand(webClient, accessToken.getTokenValue()).call())
62-
.map(Profil::new).block();
63+
.map(Profil::new);
6364
}
6465

6566
public Optional<byte[]> getProfilImage() {

Diff for: apps/profil-api/src/main/java/no/nav/registre/testnorge/profil/consumer/PersonOrganisasjonTilgangConsumer.java

+4-12
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import no.nav.registre.testnorge.profil.consumer.command.GetPersonOrganisasjonTilgangCommand;
66
import no.nav.testnav.libs.dto.altinn3.v1.OrganisasjonDTO;
77
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
8-
import no.nav.testnav.libs.securitycore.domain.UserInfo;
9-
import no.nav.testnav.libs.servletsecurity.action.GetUserInfo;
108
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
119
import org.springframework.stereotype.Component;
1210
import org.springframework.web.reactive.function.client.WebClient;
@@ -16,34 +14,28 @@
1614
@Slf4j
1715
@Component
1816
public class PersonOrganisasjonTilgangConsumer {
17+
1918
private final WebClient webClient;
2019
private final ServerProperties serverProperties;
2120
private final TokenExchange tokenExchange;
22-
private final GetUserInfo getUserInfo;
2321

2422
public PersonOrganisasjonTilgangConsumer(
2523
Consumers consumers,
2624
TokenExchange tokenExchange,
27-
WebClient.Builder webClientBuilder,
28-
GetUserInfo getUserInfo) {
25+
WebClient.Builder webClientBuilder) {
2926

3027
serverProperties = consumers.getTestnavAltinn3TilgangService();
3128
this.tokenExchange = tokenExchange;
3229
this.webClient = webClientBuilder
3330
.baseUrl(serverProperties.getUrl())
3431
.build();
35-
this.getUserInfo = getUserInfo;
3632
}
3733

38-
public Mono<OrganisasjonDTO> getOrganisasjon(String organisasjonsnummer) {
39-
40-
var userId = getUserInfo.call()
41-
.map(UserInfo::id)
42-
.orElse(null);
34+
public Mono<OrganisasjonDTO> getOrganisasjon(String ident, String organisasjonsnummer) {
4335

4436
return Mono.from(tokenExchange.exchange(serverProperties)
4537
.flatMapMany(accessToken ->
46-
new GetPersonOrganisasjonTilgangCommand(webClient, userId, accessToken.getTokenValue()).call()))
38+
new GetPersonOrganisasjonTilgangCommand(webClient, ident, accessToken.getTokenValue()).call()))
4739
.doOnNext(organisasjon -> log.info("Mottatt organisasjon: {}", organisasjon))
4840
.filter(organisasjon -> organisasjon.getOrganisasjonsnummer().equals(organisasjonsnummer));
4941
}

Diff for: apps/profil-api/src/main/java/no/nav/registre/testnorge/profil/provider/ProfilController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ProfilController(ProfilService profilService) {
2828
@SneakyThrows
2929
@GetMapping
3030
public ResponseEntity<ProfilDTO> getProfile() {
31-
var profil = profilService.getProfile();
31+
var profil = profilService.getProfile().block();
3232
return ResponseEntity.ok().cacheControl(cacheControl).body(profil.toDTO());
3333
}
3434

Diff for: apps/profil-api/src/main/java/no/nav/registre/testnorge/profil/service/ProfilService.java

+38-23
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.springframework.security.oauth2.jwt.JwtClaimNames;
1212
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
1313
import org.springframework.stereotype.Service;
14+
import reactor.core.publisher.Mono;
1415

1516
import java.util.Optional;
1617

@@ -25,25 +26,28 @@ public class ProfilService {
2526
private final PersonOrganisasjonTilgangConsumer organisasjonTilgangConsumer;
2627
private final GetUserInfo getUserInfo;
2728

28-
public Profil getProfile() {
29+
public Mono<Profil> getProfile() {
30+
2931
if (isTokenX()) {
3032
return getUserInfo.call()
31-
.map(userInfo -> new Profil(
32-
userInfo.brukernavn(),
33-
UKJENT,
34-
UKJENT,
35-
UKJENT,
36-
userInfo.organisasjonsnummer(),
37-
BANK_ID)
38-
)
39-
.orElse(new Profil(
40-
BANK_ID,
41-
UKJENT,
42-
UKJENT,
43-
UKJENT,
44-
UKJENT,
45-
BANK_ID
46-
));
33+
.map(userInfo -> organisasjonTilgangConsumer
34+
.getOrganisasjon(getIdent(), userInfo.organisasjonsnummer())
35+
.map(organisasjon -> new Profil(
36+
userInfo.brukernavn(),
37+
UKJENT,
38+
UKJENT,
39+
organisasjon.getNavn(),
40+
userInfo.organisasjonsnummer(),
41+
BANK_ID)
42+
))
43+
.orElse(Mono.just(new Profil(
44+
BANK_ID,
45+
UKJENT,
46+
UKJENT,
47+
UKJENT,
48+
UKJENT,
49+
BANK_ID
50+
)));
4751
}
4852
return azureAdProfileConsumer.getProfil();
4953
}
@@ -52,18 +56,29 @@ public Optional<byte[]> getImage() {
5256
return isTokenX() ? Optional.empty() : azureAdProfileConsumer.getProfilImage();
5357
}
5458

55-
private JwtAuthenticationToken getJwtAuthenticationToken() {
59+
private Optional<JwtAuthenticationToken> getJwtAuthenticationToken() {
60+
5661
return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
5762
.filter(JwtAuthenticationToken.class::isInstance)
58-
.map(JwtAuthenticationToken.class::cast)
59-
.orElseThrow();
63+
.map(JwtAuthenticationToken.class::cast);
6064
}
6165

6266
private boolean isTokenX() {
6367

6468
return getJwtAuthenticationToken()
65-
.getTokenAttributes()
66-
.get(JwtClaimNames.ISS)
67-
.equals(tokenXResourceServerProperties.getIssuerUri());
69+
.map(token -> token
70+
.getTokenAttributes()
71+
.get(JwtClaimNames.ISS)
72+
.equals(tokenXResourceServerProperties.getIssuerUri()))
73+
.orElseThrow();
74+
}
75+
76+
private String getIdent() {
77+
78+
return getJwtAuthenticationToken()
79+
.map(JwtAuthenticationToken::getTokenAttributes)
80+
.map(attribs -> attribs.get("pid"))
81+
.map(ident -> (String) ident)
82+
.orElseThrow();
6883
}
6984
}

Diff for: apps/profil-api/src/main/resources/logback-spring.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</throwableConverter>
1818
</encoder>
1919
</appender>
20-
<root level="TRACE">
20+
<root level="INFO">
2121
<appender-ref ref="stdout_json"/>
2222
</root>
2323
</springProfile>

Diff for: libs/servlet-security/src/main/java/no/nav/testnav/libs/servletsecurity/action/GetUserInfo.java

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public Optional<UserInfo> call() {
3535
var jwt = JWT.decode(token);
3636
var verifier = JWT.require(Algorithm.HMAC256(secret)).build();
3737
verifier.verify(jwt);
38-
log.info("jwt ---> {}", jwt.getToken());
3938
return new UserInfo(
4039
jwt.getClaim(UserConstant.USER_CLAIM_ID).asString(),
4140
jwt.getClaim(UserConstant.USER_CLAIM_ORG).asString(),

0 commit comments

Comments
 (0)