Skip to content

Commit e6d2977

Browse files
committed
Add support for retrieving and using user information
#deploy-profil-api #deploy-profil-api-test Replaced `GetAuthenticatedId` with `GetUserInfo` to extract user details and updated relevant methods to maintain compatibility. Adjusted dependency configurations and annotations, including the addition of `spring-boot-starter-web` for extended server capabilities. Corrected minor typo in log output for better clarity.
1 parent 7c46e4a commit e6d2977

File tree

6 files changed

+24
-16
lines changed

6 files changed

+24
-16
lines changed

apps/profil-api/src/main/java/no/nav/registre/testnorge/profil/ProfilApiApplicationStarter.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package no.nav.registre.testnorge.profil;
22

3+
import no.nav.testnav.libs.servletcore.config.ApplicationCoreConfig;
4+
import no.nav.testnav.libs.servletsecurity.config.SecureOAuth2ServerToServerConfiguration;
35
import org.springframework.boot.SpringApplication;
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
57
import org.springframework.context.annotation.Import;
68

7-
import no.nav.testnav.libs.servletcore.config.ApplicationCoreConfig;
8-
import no.nav.testnav.libs.servletsecurity.config.SecureOAuth2ServerToServerConfiguration;
9-
109
@SpringBootApplication
1110
@Import({
1211
ApplicationCoreConfig.class,

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import no.nav.registre.testnorge.profil.service.AzureAdTokenService;
88
import org.springframework.beans.factory.annotation.Value;
99
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
10-
import org.springframework.stereotype.Component;
10+
import org.springframework.stereotype.Service;
1111
import org.springframework.web.reactive.function.client.ExchangeStrategies;
1212
import org.springframework.web.reactive.function.client.WebClient;
1313
import reactor.netty.http.client.HttpClient;
@@ -17,8 +17,9 @@
1717
import java.util.Optional;
1818

1919
@Slf4j
20-
@Component
20+
@Service
2121
public class AzureAdProfileConsumer {
22+
2223
private final WebClient webClient;
2324
private final AzureAdTokenService azureAdTokenService;
2425

@@ -67,7 +68,7 @@ public Optional<byte[]> getProfilImage() {
6768
.flatMap(accessToken -> new GetProfileImageCommand(webClient, accessToken.getTokenValue()).call())
6869
.block());
6970
} catch (IllegalStateException e) {
70-
log.warn("Finner ikke profil bilde", e);
71+
log.warn("Finner ikke profilbilde", e);
7172
return Optional.empty();
7273
}
7374
}

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
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.servletsecurity.action.GetAuthenticatedId;
8+
import no.nav.testnav.libs.securitycore.domain.UserInfo;
9+
import no.nav.testnav.libs.servletsecurity.action.GetUserInfo;
910
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
1011
import org.springframework.stereotype.Component;
1112
import org.springframework.web.reactive.function.client.WebClient;
@@ -18,27 +19,31 @@ public class PersonOrganisasjonTilgangConsumer {
1819
private final WebClient webClient;
1920
private final ServerProperties serverProperties;
2021
private final TokenExchange tokenExchange;
21-
private final GetAuthenticatedId getAuthenticatedId;
22+
private final GetUserInfo getUserInfo;
2223

2324
public PersonOrganisasjonTilgangConsumer(
2425
Consumers consumers,
2526
TokenExchange tokenExchange,
2627
WebClient.Builder webClientBuilder,
27-
GetAuthenticatedId getAuthenticatedId) {
28+
GetUserInfo getUserInfo) {
2829

2930
serverProperties = consumers.getTestnavAltinn3TilgangService();
3031
this.tokenExchange = tokenExchange;
3132
this.webClient = webClientBuilder
3233
.baseUrl(serverProperties.getUrl())
3334
.build();
34-
this.getAuthenticatedId = getAuthenticatedId;
35+
this.getUserInfo = getUserInfo;
3536
}
3637

3738
public Mono<OrganisasjonDTO> getOrganisasjon(String organisasjonsnummer) {
3839

40+
var userId = getUserInfo.call()
41+
.map(UserInfo::id)
42+
.orElse(null);
43+
3944
return Mono.from(tokenExchange.exchange(serverProperties)
4045
.flatMapMany(accessToken ->
41-
new GetPersonOrganisasjonTilgangCommand(webClient, getAuthenticatedId.call(), accessToken.getTokenValue()).call()))
46+
new GetPersonOrganisasjonTilgangCommand(webClient, userId, accessToken.getTokenValue()).call()))
4247
.doOnNext(organisasjon -> log.info("Mottatt organisasjon: {}", organisasjon))
4348
.filter(organisasjon -> organisasjon.getOrganisasjonsnummer().equals(organisasjonsnummer));
4449
}

apps/profil-api/src/main/java/no/nav/registre/testnorge/profil/service/AzureAdTokenService.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package no.nav.registre.testnorge.profil.service;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import no.nav.testnav.libs.securitycore.command.azuread.OnBehalfOfExchangeCommand;
5+
import no.nav.testnav.libs.securitycore.domain.AccessToken;
46
import no.nav.testnav.libs.securitycore.domain.azuread.AzureClientCredential;
7+
import no.nav.testnav.libs.servletsecurity.action.GetAuthenticatedToken;
58
import org.springframework.beans.factory.annotation.Value;
69
import org.springframework.http.HttpHeaders;
710
import org.springframework.http.MediaType;
@@ -14,10 +17,6 @@
1417

1518
import java.net.URI;
1619

17-
import no.nav.testnav.libs.securitycore.command.azuread.OnBehalfOfExchangeCommand;
18-
import no.nav.testnav.libs.securitycore.domain.AccessToken;
19-
import no.nav.testnav.libs.servletsecurity.action.GetAuthenticatedToken;
20-
2120
@Slf4j
2221
@Service
2322
public class AzureAdTokenService {

libs/reactive-security/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies {
4949

5050
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
5151
implementation 'org.springframework.boot:spring-boot-starter-webflux'
52+
implementation 'org.springframework.boot:spring-boot-starter-web'
5253
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
5354
implementation 'org.springframework.boot:spring-boot-starter-security'
5455

libs/reactive-security/src/main/java/no/nav/testnav/libs/reactivesecurity/config/SecureOAuth2ServerToServerConfiguration.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import no.nav.testnav.libs.reactivesecurity.action.GetAuthenticatedResourceServerType;
44
import no.nav.testnav.libs.reactivesecurity.action.GetAuthenticatedToken;
55
import no.nav.testnav.libs.reactivesecurity.action.GetAuthenticatedUserId;
6+
import no.nav.testnav.libs.reactivesecurity.action.GetUserInfo;
67
import no.nav.testnav.libs.reactivesecurity.exchange.TokenExchange;
78
import no.nav.testnav.libs.reactivesecurity.exchange.tokenx.TokenXService;
89
import no.nav.testnav.libs.reactivesecurity.manager.JwtReactiveAuthenticationManager;
@@ -16,6 +17,7 @@
1617
import org.springframework.context.annotation.Bean;
1718
import org.springframework.context.annotation.Configuration;
1819
import org.springframework.context.annotation.Import;
20+
import org.springframework.security.oauth2.jwt.JwtDecoder;
1921

2022
import java.util.List;
2123

@@ -29,7 +31,8 @@
2931
GetAuthenticatedUserId.class,
3032
GetAuthenticatedResourceServerType.class,
3133
GetAuthenticatedToken.class,
32-
TokenXProperties.class
34+
TokenXProperties.class,
35+
GetUserInfo.class
3336
})
3437
public class SecureOAuth2ServerToServerConfiguration {
3538

0 commit comments

Comments
 (0)