use of com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventSubscription in project openbanking-aspsp by OpenBankingToolkit.
the class EventNotificationService method createAndSendNotification.
public void createAndSendNotification(EventSubject eventSubject, Tpp tpp, EventType eventType) throws CallbackFailedException {
// Create a notification
FREventNotification eventNotification = signedJwtEventBuilder.build(tpp, eventSubject, eventType);
if (eventNotification == null || StringUtils.isEmpty(eventNotification.getSignedJwt())) {
log.error("Failed to generate JWT for event notification due to null or empty JWT: {}. Aborting the callback to TPPs", eventNotification);
throw new CallbackFailedException("Failed to generate JWT for event notification due to null or empty JWT");
}
log.debug("Built an event notification with signed jwt: {}", eventNotification);
// Check event subscription exists
Optional<FREventSubscription> eventSubscriptions = eventSubscriptionService.findByTppId(tpp.getId()).stream().findFirst();
if (eventSubscriptions.isPresent()) {
// Event subscription exists to use this to notify
notifyToEventSubscription(tpp, eventSubscriptions.get(), eventNotification, eventSubject, eventType);
} else {
// Event subscription does not exist to try legacy callback URL if present
notifyToLegacyCallbackUrl(tpp, eventNotification, eventSubject, eventType);
}
}
use of com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventSubscription in project openbanking-aspsp by OpenBankingToolkit.
the class EventSubscriptionService method findByTppId.
public Collection<FREventSubscription> findByTppId(String tppId) {
log.debug("Read all the event subscription for {}", tppId);
ParameterizedTypeReference<Collection<FREventSubscription>> ptr = new ParameterizedTypeReference<Collection<FREventSubscription>>() {
};
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(rsStoreRoot + BASE_RESOURCE_PATH + "search/findByTppId");
builder.queryParam("tppId", tppId);
URI uri = builder.build().encode().toUri();
ResponseEntity<Collection<FREventSubscription>> entity = restTemplate.exchange(uri, HttpMethod.GET, null, ptr);
return entity.getBody();
}
use of com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventSubscription in project openbanking-aspsp by OpenBankingToolkit.
the class EventSubscriptionApiControllerIT method createCallbackUrls_urlAlreadyExistsForTpp_conflict.
@Test
public void createCallbackUrls_urlAlreadyExistsForTpp_conflict() throws Exception {
// Given
springSecForTest.mockAuthCollector.mockAuthorities(OBRIRole.ROLE_PISP);
eventSubscriptionsRepository.save(FREventSubscription.builder().tppId(tpp.getId()).eventSubscription(FREventSubscriptionData.builder().build()).build());
OBEventSubscription1 obEventSubscription1 = new OBEventSubscription1().data(new OBEventSubscription1Data().callbackUrl("http://callback").version(OBVersion.v3_0.getCanonicalVersion()));
// When
HttpResponse response = Unirest.post(BASE_URL + port + RESOURCE_URI).header(OBHeaders.X_FAPI_FINANCIAL_ID, rsConfiguration.financialId).header(OBHeaders.AUTHORIZATION, "token").header("x-ob-client-id", clientId).header(OBHeaders.CONTENT_TYPE, "application/json; charset=utf-8").body(obEventSubscription1).asObject(String.class);
// Then
assertThat(response.getStatus()).isEqualTo(HttpStatus.CONFLICT.value());
final Collection<FREventSubscription> byClientId = eventSubscriptionsRepository.findByTppId(tpp.getId());
// Should still be just 1
assertThat(byClientId.size()).isEqualTo(1);
}
use of com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventSubscription in project openbanking-aspsp by OpenBankingToolkit.
the class EventSubscriptionApiController method updateEventSubscription.
public ResponseEntity updateEventSubscription(@ApiParam(value = "EventSubscriptionId", required = true) @PathVariable("EventSubscriptionId") String eventSubscriptionId, @ApiParam(value = "Default", required = true) @Valid @RequestBody OBEventSubscriptionResponse1 obEventSubscriptionParam, @ApiParam(value = "An Authorisation Token as per https://tools.ietf.org/html/rfc6750", required = true) @RequestHeader(value = "Authorization", required = true) String authorization, @ApiParam(value = "Header containing a detached JWS signature of the body of the payload.", required = true) @RequestHeader(value = "x-jws-signature", required = false) String xJwsSignature, @ApiParam(value = "An RFC4122 UID used as a correlation id.") @RequestHeader(value = "x-fapi-interaction-id", required = false) String xFapiInteractionId, @ApiParam(value = "The PISP Client ID") @RequestHeader(value = "x-ob-client-id", required = true) String clientId, HttpServletRequest request, Principal principal) throws OBErrorResponseException {
final OBEventSubscription1 updatedSubscription = new OBEventSubscription1().data(new OBEventSubscription1Data().callbackUrl(obEventSubscriptionParam.getData().getCallbackUrl()).eventTypes(obEventSubscriptionParam.getData().getEventTypes()).version(obEventSubscriptionParam.getData().getVersion()));
final Optional<FREventSubscription> byId = eventSubscriptionsRepository.findById(eventSubscriptionId);
if (byId.isPresent()) {
FREventSubscription existingEventSubscription = byId.get();
// A TPP must not update a event-subscription on an older version, via the EventSubscriptionId for an event-subscription created in a newer version
if (eventResponseUtil.isAccessToResourceAllowedFromApiVersion(existingEventSubscription.getEventSubscription().getVersion())) {
existingEventSubscription.setEventSubscription(toFREventSubscriptionData(updatedSubscription));
eventSubscriptionsRepository.save(existingEventSubscription);
return ResponseEntity.ok(packageResponse(existingEventSubscription));
} else {
return ResponseEntity.status(HttpStatus.CONFLICT).body("The event subscription can't be update via an older API version.");
}
} else {
// PUT is only used for amending existing subscriptions
throw new OBErrorResponseException(HttpStatus.BAD_REQUEST, OBRIErrorResponseCategory.REQUEST_INVALID, OBRIErrorType.EVENT_SUBSCRIPTION_NOT_FOUND.toOBError1(eventSubscriptionId));
}
}
use of com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventSubscription in project openbanking-aspsp by OpenBankingToolkit.
the class EventSubscriptionApiController method createEventSubscription.
public ResponseEntity createEventSubscription(@ApiParam(value = "Default", required = true) @Valid @RequestBody OBEventSubscription1 obEventSubscription, @ApiParam(value = "An Authorisation Token as per https://tools.ietf.org/html/rfc6750", required = true) @RequestHeader(value = "Authorization", required = true) String authorization, @ApiParam(value = "Header containing a detached JWS signature of the body of the payload.", required = true) @RequestHeader(value = "x-jws-signature", required = false) String xJwsSignature, @ApiParam(value = "An RFC4122 UID used as a correlation id.") @RequestHeader(value = "x-fapi-interaction-id", required = false) String xFapiInteractionId, @ApiParam(value = "The PISP Client ID") @RequestHeader(value = "x-ob-client-id", required = true) String clientId, HttpServletRequest request, Principal principal) throws OBErrorResponseException {
log.debug("Create new event subscriptions: {} for client: {}", obEventSubscription, clientId);
// Get the TPP from the header
final Optional<Tpp> isTpp = Optional.ofNullable(tppRepository.findByClientId(clientId));
if (isTpp.isEmpty()) {
log.warn("No TPP found for client id '{}'", clientId);
throw new OBErrorResponseException(HttpStatus.NOT_FOUND, OBRIErrorResponseCategory.REQUEST_INVALID, OBRIErrorType.TPP_NOT_FOUND.toOBError1(clientId));
}
// Check if an event already exists for this TPP
final Collection<FREventSubscription> byClientId = eventSubscriptionsRepository.findByTppId(isTpp.get().getId());
if (!byClientId.isEmpty()) {
log.debug("An event subscription already exists for this TPP client id: '{}' for the version: {}", clientId, byClientId.stream().findFirst().get());
throw new OBErrorResponseException(HttpStatus.CONFLICT, OBRIErrorResponseCategory.REQUEST_INVALID, OBRIErrorType.EVENT_SUBSCRIPTION_ALREADY_EXISTS.toOBError1());
}
// Persist the event subscription
FREventSubscription frEventSubscription = FREventSubscription.builder().id(UUID.randomUUID().toString()).tppId(isTpp.get().getId()).eventSubscription(toFREventSubscriptionData(obEventSubscription)).build();
eventSubscriptionsRepository.save(frEventSubscription);
return ResponseEntity.status(HttpStatus.CREATED).body(packageResponse(frEventSubscription));
}
Aggregations