Skip to content

Commit

Permalink
```
Browse files Browse the repository at this point in the history
Refactor to use Altinn3 API for user access management
#deploy-bruker-service

Replaced Person-Organisasjon-Tilgang API with Altinn3 API across the application. Deprecated `OrganisasjonDTO` and updated classes to utilize `AltinnBrukerRequest` and Altinn3 DTOs. Adjusted configuration and consumer logic to align with the new API structure for improved functionality.
```
  • Loading branch information
krharum committed Dec 20, 2024
1 parent 9a65b64 commit 12bd363
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@

import static lombok.AccessLevel.PACKAGE;

/**
* Samler alle placeholders for ulike {@code consumers.*}-konfigurasjon her, dvs. subklasser av {@code ServerProperties}.
* <br/><br/>
* Husk at Spring Boot bruker <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.typesafe-configuration-properties.relaxed-binding">relaxed binding</a>
* mellom configuration properties og field names.
*
* @see ServerProperties
*/
@Configuration
@ConfigurationProperties(prefix = "consumers")
@NoArgsConstructor(access = PACKAGE)
@Getter
@Setter(PACKAGE)
public class Consumers {

private ServerProperties testnavPersonOrganisasjonTilgangService;
private ServerProperties testnavAltinn3TilgangService;

}
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
package no.nav.testnav.apps.brukerservice.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import no.nav.testnav.apps.brukerservice.config.Consumers;
import no.nav.testnav.apps.brukerservice.consumer.command.GetOrganisasjonCommand;
import no.nav.testnav.apps.brukerservice.consumer.command.GetBrukertilgangCommand;
import no.nav.testnav.apps.brukerservice.domain.Organisasjon;
import no.nav.testnav.libs.reactivesecurity.action.GetAuthenticatedUserId;
import no.nav.testnav.libs.reactivesecurity.exchange.TokenExchange;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import org.springframework.http.MediaType;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Component
public class PersonOrganisasjonTilgangConsumer {

private final WebClient webClient;
private final ServerProperties serverProperties;
private final TokenExchange tokenExchange;
private final GetAuthenticatedUserId getAuthenticatedUserId;

public PersonOrganisasjonTilgangConsumer(
Consumers consumers,
TokenExchange tokenExchange,
ObjectMapper objectMapper,
WebClient.Builder webClientBuilder) {
serverProperties = consumers.getTestnavPersonOrganisasjonTilgangService();
WebClient.Builder webClientBuilder,
GetAuthenticatedUserId getAuthenticatedUserId) {

serverProperties = consumers.getTestnavAltinn3TilgangService();
this.tokenExchange = tokenExchange;
ExchangeStrategies jacksonStrategy = ExchangeStrategies
.builder()
.codecs(
config -> {
config.defaultCodecs()
.jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper, MediaType.APPLICATION_JSON));
config.defaultCodecs()
.jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper, MediaType.APPLICATION_JSON));
})
.build();
this.webClient = webClientBuilder
.exchangeStrategies(jacksonStrategy)
.baseUrl(serverProperties.getUrl())
.build();
this.getAuthenticatedUserId = getAuthenticatedUserId;
}

public Mono<Organisasjon> getOrganisasjon(String orgnummer) {
return tokenExchange.exchange(serverProperties)
.flatMap(accessToken -> new GetOrganisasjonCommand(webClient, orgnummer, accessToken.getTokenValue()).call())
.map(Organisasjon::new);

return Mono.from(getAuthenticatedUserId.call()
.flatMapMany(userId -> tokenExchange.exchange(serverProperties)
.flatMapMany(accessToken ->
new GetBrukertilgangCommand(webClient, userId, accessToken.getTokenValue()).call()))
.filter(org -> org.getOrganisasjonsnummer().equals(orgnummer))
.map(Organisasjon::new));
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
package no.nav.testnav.apps.brukerservice.consumer.command;

import lombok.RequiredArgsConstructor;
import no.nav.testnav.apps.brukerservice.consumer.dto.OrganisasjonDTO;
import no.nav.testnav.apps.brukerservice.consumer.dto.AltinnBrukerRequest;
import no.nav.testnav.libs.dto.altinn3.v1.OrganisasjonDTO;
import no.nav.testnav.libs.reactivecore.utils.WebClientFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.concurrent.Callable;

@RequiredArgsConstructor
public class GetOrganisasjonCommand implements Callable<Mono<OrganisasjonDTO>> {
public class GetBrukertilgangCommand implements Callable<Flux<OrganisasjonDTO>> {
private final WebClient webClient;
private final String organisasjonsnummer;
private final String ident;
private final String token;

@Override
public Mono<OrganisasjonDTO> call() {
return webClient.get()
.uri(builder -> builder.path("/api/v1/person/organisasjoner/{organisasjonsnummer}").build(organisasjonsnummer))
public Flux<OrganisasjonDTO> call() {
return webClient.post()
.uri(builder -> builder.path("/api/v1/brukertilgang").build())
.header(HttpHeaders.AUTHORIZATION, "Bearer " + token)
.bodyValue(new AltinnBrukerRequest(ident))
.retrieve()
.bodyToMono(OrganisasjonDTO.class)
.bodyToFlux(OrganisasjonDTO.class)
.doOnError(WebClientFilter::logErrorMessage)
.onErrorResume(
throwable -> throwable instanceof WebClientResponseException.NotFound,
throwable -> Mono.empty()
);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package no.nav.testnav.apps.brukerservice.consumer.dto;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class AltinnBrukerRequest {

private String ident;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package no.nav.testnav.apps.brukerservice.domain;

import no.nav.testnav.apps.brukerservice.consumer.dto.OrganisasjonDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import no.nav.testnav.libs.dto.altinn3.v1.OrganisasjonDTO;

public class Organisasjon {
private final String navn;
private final String organisasjonsnummer;
private final String organisasjonsform;

public Organisasjon(OrganisasjonDTO dto) {
this.navn = dto.navn();
this.organisasjonsnummer = dto.organisasjonsnummer();
this.organisasjonsform = dto.organisasjonsfrom();
}
@Data
@NoArgsConstructor
@AllArgsConstructor

public String getNavn() {
return navn;
}
public class Organisasjon {

public String getOrganisasjonsnummer() {
return organisasjonsnummer;
}
private String navn;
private String organisasjonsnummer;
private String organisasjonsform;

public String getOrganisasjonsform() {
return organisasjonsform;
public Organisasjon(OrganisasjonDTO dto) {
this.navn = dto.getNavn();
this.organisasjonsnummer = dto.getOrganisasjonsnummer();
this.organisasjonsform = dto.getOrganisasjonsform();
}
}
6 changes: 3 additions & 3 deletions apps/bruker-service/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ spring:


consumers:
testnav-person-organisasjon-tilgang-service:
url: http://testnav-person-organisasjon-tilgang-service-dev.dolly.svc.cluster.local
name: testnav-person-organisasjon-tilgang-service-dev
testnav-altinn3-tilgang-service:
url: http://testnav-altinn3-tilgang-service.dolly.svc.cluster.local
name: testnav-altinn3-tilgang-service
8 changes: 5 additions & 3 deletions apps/bruker-service/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ AZURE_APP_CLIENT_SECRET: ${sm://azure-app-client-secret}
CRYPTOGRAPHY_SECRET: DUMMY SUPER SECRET CRYPTOGRAPHY KEY THAT IS NOT SECURE
JWT_SECRET: DUMMY SUPER SECRET JWT KEY THAT IS NOT SECURE

TOKEN_X_CLIENT_ID: dev-gcp:dolly:testnav-bruker-service-dev

spring:
application:
name: testnav-bruker-service-dev
Expand All @@ -15,6 +17,6 @@ spring:
password:

consumers:
testnav-person-organisasjon-tilgang-service:
url: https://testnav-person-organisasjon-tilgang-service-dev.intern.dev.nav.no
name: testnav-person-organisasjon-tilgang-service-dev
testnav-altinn3-tilgang-service:
url: https://testnav-altinn3-tilgang-service.intern.dev.nav.no
name: testnav-altinn3-tilgang-service
6 changes: 3 additions & 3 deletions apps/bruker-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ springdoc:
url: /v3/api-docs

consumers:
testnav-person-organisasjon-tilgang-service:
url: http://testnav-person-organisasjon-tilgang-service.dolly.svc.cluster.local
testnav-altinn3-tilgang-service:
url: http://testnav-altinn3-proxy.dolly.svc.cluster.local
cluster: dev-gcp
namespace: dolly
name: testnav-person-organisasjon-tilgang-service
name: testnav-altinn3-proxy

management:
endpoints:
Expand Down

0 comments on commit 12bd363

Please sign in to comment.