Skip to content

Commit

Permalink
feat: [SRTP-230] extract subject from access token (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaconsalvi authored Jan 31, 2025
1 parent 0d9f5bb commit fbac964
Show file tree
Hide file tree
Showing 28 changed files with 523 additions and 369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ActivationDtoMapper {

public ActivationDto toActivationDto(Payer payer) {
return new ActivationDto().id(payer.activationID().getId())
.payer(new PayerDto().fiscalCode(payer.fiscalCode()).rtpSpId(payer.rtpSpId()))
.payer(new PayerDto().fiscalCode(payer.fiscalCode()).rtpSpId(payer.serviceProviderDebtor()))
.effectiveActivationDate(LocalDateTime.ofInstant(payer.effectiveActivationDate(), ZoneOffset.UTC));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,23 @@ public Rtp toRtp(CreateRtpDto createRtpDto) {
.savingDateTime(LocalDateTime.now())
.payerName(createRtpDto.getPayer().getName())
.payerId(createRtpDto.getPayer().getPayerId()).payeeName(createRtpDto.getPayee().getName())
.payeeId(createRtpDto.getPayee().getPayeeId()).rtpSpId("rtpSpId").iban("iban")
.payeeId(createRtpDto.getPayee().getPayeeId()).serviceProviderDebtor("serviceProviderDebtor").iban("iban")
.subject(createRtpDto.getPaymentNotice().getSubject())
.payTrxRef(createRtpDto.getPayee().getPayTrxRef()).flgConf("flgConf").build();
}

public Rtp toRtpWithServiceProviderCreditor(CreateRtpDto createRtpDto, String tokenSub) {
return Rtp.builder().noticeNumber(createRtpDto.getPaymentNotice().getNoticeNumber())
.amount(createRtpDto.getPaymentNotice().getAmount()).resourceID(ResourceID.createNew())
.description(createRtpDto.getPaymentNotice().getDescription())
.expiryDate(createRtpDto.getPaymentNotice().getExpiryDate())
.savingDateTime(LocalDateTime.now())
.payerName(createRtpDto.getPayer().getName())
.payerId(createRtpDto.getPayer().getPayerId()).payeeName(createRtpDto.getPayee().getName())
.payeeId(createRtpDto.getPayee().getPayeeId()).serviceProviderDebtor("serviceProviderDebtor").iban("iban")
.subject(createRtpDto.getPaymentNotice().getSubject())
.serviceProviderCreditor(tokenSub)
.payTrxRef(createRtpDto.getPayee().getPayTrxRef()).flgConf("flgConf").build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import it.gov.pagopa.rtp.activator.domain.errors.PayerNotActivatedException;
import it.gov.pagopa.rtp.activator.model.generated.send.CreateRtpDto;
import it.gov.pagopa.rtp.activator.service.rtp.SendRTPService;
import it.gov.pagopa.rtp.activator.utils.TokenInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -18,27 +19,27 @@
@Slf4j
public class SendAPIControllerImpl implements RtpsApi {

private final SendRTPService sendRTPService;
private final SendRTPService sendRTPService;

private final RtpDtoMapper rtpDtoMapper;
private final RtpDtoMapper rtpDtoMapper;

public SendAPIControllerImpl(SendRTPService sendRTPService, RtpDtoMapper rtpDtoMapper) {
this.sendRTPService = sendRTPService;
this.rtpDtoMapper = rtpDtoMapper;
}
public SendAPIControllerImpl(SendRTPService sendRTPService, RtpDtoMapper rtpDtoMapper) {
this.sendRTPService = sendRTPService;
this.rtpDtoMapper = rtpDtoMapper;
}

@Override
@PreAuthorize("hasRole('write_rtp_send')")
public Mono<ResponseEntity<Void>> createRtp(Mono<CreateRtpDto> createRtpDto,
String version, ServerWebExchange exchange) {
log.info("Received request to create RTP");
return createRtpDto
.map(rtpDtoMapper::toRtp)
.flatMap(sendRTPService::send)
.thenReturn(new ResponseEntity<Void>(HttpStatus.CREATED))
.onErrorReturn(PayerNotActivatedException.class, ResponseEntity.unprocessableEntity().build())
.doOnError(a -> log.error("Error creating RTP {}", a.getMessage()));
}
@Override
@PreAuthorize("hasRole('write_rtp_send')")
public Mono<ResponseEntity<Void>> createRtp(Mono<CreateRtpDto> createRtpDto,
String version, ServerWebExchange exchange) {
log.info("Received request to create RTP");
return createRtpDto
.flatMap(rtpDto -> TokenInfo.getTokenSubject()
.map(sub -> rtpDtoMapper.toRtpWithServiceProviderCreditor(rtpDto, sub)))
.flatMap(sendRTPService::send)
.thenReturn(new ResponseEntity<Void>(HttpStatus.CREATED))
.onErrorReturn(PayerNotActivatedException.class, ResponseEntity.unprocessableEntity().build())
.doOnError(a -> log.error("Error creating RTP {}", a.getMessage()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import java.time.Instant;

public record Payer(ActivationID activationID, String rtpSpId, String fiscalCode, Instant effectiveActivationDate) {
public record Payer(ActivationID activationID, String serviceProviderDebtor, String fiscalCode, Instant effectiveActivationDate) {
}
10 changes: 6 additions & 4 deletions src/main/java/it/gov/pagopa/rtp/activator/domain/rtp/Rtp.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
@Builder
public record Rtp(String noticeNumber, BigDecimal amount, String description, LocalDate expiryDate,
String payerId, String payerName, String payeeName, String payeeId, ResourceID resourceID,
String subject, LocalDateTime savingDateTime, String rtpSpId, String iban,
String payTrxRef, String flgConf, RtpStatus status) {
String subject, LocalDateTime savingDateTime, String serviceProviderDebtor, String iban,
String payTrxRef, String flgConf, RtpStatus status, String serviceProviderCreditor) {

public Rtp toRtpWithActivationInfo(String rtpSpId) {
return Rtp.builder()
.rtpSpId(rtpSpId)
.serviceProviderDebtor(rtpSpId)
.iban(this.iban())
.payTrxRef(this.payTrxRef())
.flgConf(this.flgConf())
Expand All @@ -27,14 +27,15 @@ public Rtp toRtpWithActivationInfo(String rtpSpId) {
.expiryDate(this.expiryDate())
.resourceID(this.resourceID())
.subject(this.subject())
.serviceProviderCreditor(this.serviceProviderCreditor())
.savingDateTime(this.savingDateTime())
.status(RtpStatus.CREATED)
.build();
}

public Rtp toRtpSent(Rtp rtp) {
return Rtp.builder()
.rtpSpId(rtp.rtpSpId())
.serviceProviderDebtor(rtp.serviceProviderDebtor())
.iban(rtp.iban())
.payTrxRef(rtp.payTrxRef())
.flgConf(rtp.flgConf())
Expand All @@ -48,6 +49,7 @@ public Rtp toRtpSent(Rtp rtp) {
.expiryDate(rtp.expiryDate())
.resourceID(rtp.resourceID())
.subject(this.subject())
.serviceProviderCreditor(this.serviceProviderCreditor())
.savingDateTime(rtp.savingDateTime())
.status(RtpStatus.SENT)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class ActivationEntity {
@Id
private String id;
private String rtpSpId;
private String serviceProviderDebtor;
private Instant effectiveActivationDate;

private String fiscalCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public Payer toDomain(ActivationEntity activationEntity) {
ActivationID activationID = new ActivationID(
UUID.fromString(activationEntity.getId()));
return new Payer(activationID,
activationEntity.getRtpSpId(), activationEntity.getFiscalCode(),
activationEntity.getServiceProviderDebtor(), activationEntity.getFiscalCode(),
activationEntity.getEffectiveActivationDate());
}

public ActivationEntity toDbEntity(Payer payer) {
return ActivationEntity.builder().id(payer.activationID().getId().toString())
.fiscalCode(payer.fiscalCode())
.rtpSpId(payer.rtpSpId())
.serviceProviderDebtor(payer.serviceProviderDebtor())
.effectiveActivationDate(payer.effectiveActivationDate())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ public class RtpEntity {
private String payeeId;
private String subject;
private Instant savingDateTime;
private String rtpSpId;
private String serviceProviderDebtor;
private String iban;
private String payTrxRef;
private String flgConf;
private String status;
private String serviceProviderCreditor;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ public Rtp toDomain(RtpEntity rtpEntity) {
.payeeId(rtpEntity.getPayeeId())
.resourceID(new ResourceID(rtpEntity.getResourceID()))
.savingDateTime(LocalDateTime.ofInstant(rtpEntity.getSavingDateTime(), ZoneOffset.UTC))
.rtpSpId(rtpEntity.getRtpSpId())
.serviceProviderDebtor(rtpEntity.getServiceProviderDebtor())
.iban(rtpEntity.getIban())
.payTrxRef(rtpEntity.getPayTrxRef())
.flgConf(rtpEntity.getFlgConf())
.subject(rtpEntity.getSubject())
.status(RtpStatus.valueOf(rtpEntity.getStatus()))
.serviceProviderCreditor(rtpEntity.getServiceProviderCreditor())
.build();
}

Expand All @@ -44,12 +45,13 @@ public RtpEntity toDbEntity(Rtp rtp) {
.payeeId(rtp.payeeId())
.resourceID(rtp.resourceID().getId())
.savingDateTime(rtp.savingDateTime().toInstant(ZoneOffset.UTC))
.rtpSpId(rtp.rtpSpId())
.serviceProviderDebtor(rtp.serviceProviderDebtor())
.iban(rtp.iban())
.payTrxRef(rtp.payTrxRef())
.flgConf(rtp.flgConf())
.subject(rtp.subject())
.status(rtp.status().name())
.serviceProviderCreditor(rtp.serviceProviderCreditor())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public ActivationPayerServiceImpl(ActivationDBRepository activationDBRepository)
}

@Override
public Mono<Payer> activatePayer(String rtpSpId, String fiscalCode) {
public Mono<Payer> activatePayer(String serviceProviderDebtor, String fiscalCode) {

ActivationID activationID = ActivationID.createNew();
Payer payer = new Payer(activationID, rtpSpId, fiscalCode, Instant.now());
Payer payer = new Payer(activationID, serviceProviderDebtor, fiscalCode, Instant.now());

return activationDBRepository.findByFiscalCode(fiscalCode)
.flatMap(existingEntity -> Mono.<Payer>error(new PayerAlreadyExists()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Mono<Rtp> send(Rtp rtp) {
.onErrorMap(Throwable::getCause)
.map(response -> rtpToSend)
.defaultIfEmpty(rtpToSend)
.doOnSuccess(rtpSent -> log.info("RTP sent to {} with id: {}", rtpSent.rtpSpId(),
.doOnSuccess(rtpSent -> log.info("RTP sent to {} with id: {}", rtpSent.serviceProviderDebtor(),
rtpSent.resourceID().getId()))
)
.map(rtp::toRtpSent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public SepaRequestToPayRequestResourceDto toEpcRequestToPay(Rtp rtp) {
.id(party38ChoiceEPC25922V30DS02Dto);

var dbtFinancialInstitutionIdentification18EPC25922V30DS02Dto = new FinancialInstitutionIdentification18EPC25922V30DS02Dto();
dbtFinancialInstitutionIdentification18EPC25922V30DS02Dto.setBICFI(rtp.rtpSpId());
dbtFinancialInstitutionIdentification18EPC25922V30DS02Dto.setBICFI(rtp.serviceProviderDebtor());

var dbtBranchAndFinancialInstitutionIdentification6EPC25922V30DS02Dto = new BranchAndFinancialInstitutionIdentification6EPC25922V30DS02Dto()
.finInstnId(dbtFinancialInstitutionIdentification18EPC25922V30DS02Dto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,49 @@
@Configuration
public class MongoTraceConfiguration {

/**
* Creates a trace interceptor bean for MongoDB operations.
* This interceptor is responsible for creating and managing trace spans
* for MongoDB database operations.
*
* @param tracer OpenTelemetry tracer for creating spans
* @param mongoTemplate Template for MongoDB operations
* @return A new instance of ReactiveMongoTraceInterceptor
*/
@Bean
public ReactiveMongoTraceInterceptor mongoTraceInterceptor(
Tracer tracer,
ReactiveMongoTemplate mongoTemplate) {
return new ReactiveMongoTraceInterceptor(tracer, mongoTemplate);
}
/**
* Creates a trace interceptor bean for MongoDB operations.
* This interceptor is responsible for creating and managing trace spans
* for MongoDB database operations.
*
* @param tracer OpenTelemetry tracer for creating spans
* @param mongoTemplate Template for MongoDB operations
* @return A new instance of ReactiveMongoTraceInterceptor
*/
@Bean
public ReactiveMongoTraceInterceptor mongoTraceInterceptor(
Tracer tracer,
ReactiveMongoTemplate mongoTemplate) {
return new ReactiveMongoTraceInterceptor(tracer, mongoTemplate);
}

/**
* Creates a bean post processor that adds tracing capabilities to MongoDB repositories.
* This processor automatically creates proxies for all ReactiveMongoRepository instances,
* adding tracing interceptors to track database operations.
*
* @param tracer OpenTelemetry tracer for creating spans
* @param mongoTemplate Template for MongoDB operations
* @return BeanPostProcessor that creates proxies for repository beans
*/
@Bean
public BeanPostProcessor mongoRepositoryProxyPostProcessor(
Tracer tracer, ReactiveMongoTemplate mongoTemplate) {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
// Only create proxies for ReactiveMongoRepository implementations
if (bean instanceof ReactiveMongoRepository) {
// Create and configure the proxy with tracing interceptor
ProxyFactory proxyFactory = new ProxyFactory(bean);
proxyFactory.addAdvice(new ReactiveMongoTraceInterceptor(tracer, mongoTemplate));
return proxyFactory.getProxy();
}
return bean;
}
};
}
/**
* Creates a bean post processor that adds tracing capabilities to MongoDB
* repositories.
* This processor automatically creates proxies for all ReactiveMongoRepository
* instances,
* adding tracing interceptors to track database operations.
*
* @param tracer OpenTelemetry tracer for creating spans
* @param mongoTemplate Template for MongoDB operations
* @return BeanPostProcessor that creates proxies for repository beans
*/
@Bean
public BeanPostProcessor mongoRepositoryProxyPostProcessor(
Tracer tracer, ReactiveMongoTemplate mongoTemplate) {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
// Only create proxies for ReactiveMongoRepository implementations
if (bean instanceof ReactiveMongoRepository) {
// Create and configure the proxy with tracing interceptor
ProxyFactory proxyFactory = new ProxyFactory(bean);
proxyFactory.addAdvice(new ReactiveMongoTraceInterceptor(tracer, mongoTemplate));
return proxyFactory.getProxy();
}
return bean;
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
@Configuration
public class OpenTelemetryConfig {

/**
* Creates and configures the OpenTelemetry Tracer bean.
* This tracer is used throughout the application for creating
* and managing trace spans for MongoDB operations.
*
* @param openTelemetry The OpenTelemetry instance injected by Spring
* @return Configured Tracer instance for the RTP Activator application
*/
@Bean
public Tracer tracer(OpenTelemetry openTelemetry) {
return openTelemetry.getTracer(
"rtp-activator", // Instrumentation name
"1.0.0"// Instrumentation version
);
}
/**
* Creates and configures the OpenTelemetry Tracer bean.
* This tracer is used throughout the application for creating
* and managing trace spans for MongoDB operations.
*
* @param openTelemetry The OpenTelemetry instance injected by Spring
* @return Configured Tracer instance for the RTP Activator application
*/
@Bean
public Tracer tracer(OpenTelemetry openTelemetry) {
return openTelemetry.getTracer(
"rtp-activator", // Instrumentation name
"1.0.0"// Instrumentation version
);
}
}
Loading

0 comments on commit fbac964

Please sign in to comment.