Search in sources :

Example 1 with FREventSubscription

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);
    }
}
Also used : FREventNotification(com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventNotification) FREventSubscription(com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventSubscription)

Example 2 with FREventSubscription

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();
}
Also used : ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Collection(java.util.Collection) URI(java.net.URI) FREventSubscription(com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventSubscription)

Example 3 with FREventSubscription

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);
}
Also used : HttpResponse(kong.unirest.HttpResponse) FREventSubscription(com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventSubscription) SpringSecForTest(com.forgerock.openbanking.integration.test.support.SpringSecForTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with FREventSubscription

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));
    }
}
Also used : OBErrorResponseException(com.forgerock.openbanking.exceptions.OBErrorResponseException) FREventSubscription(com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventSubscription)

Example 5 with FREventSubscription

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));
}
Also used : Tpp(com.forgerock.openbanking.model.Tpp) OBErrorResponseException(com.forgerock.openbanking.exceptions.OBErrorResponseException) FREventSubscription(com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventSubscription)

Aggregations

FREventSubscription (com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventSubscription)9 SpringSecForTest (com.forgerock.openbanking.integration.test.support.SpringSecForTest)4 Test (org.junit.Test)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 FREventNotification (com.forgerock.openbanking.common.model.openbanking.persistence.event.FREventNotification)2 OBErrorResponseException (com.forgerock.openbanking.exceptions.OBErrorResponseException)2 Tpp (com.forgerock.openbanking.model.Tpp)2 HttpResponse (kong.unirest.HttpResponse)2 CallBackCounterEntry (com.forgerock.openbanking.analytics.model.entries.callback.CallBackCounterEntry)1 CallBackResponseStatus (com.forgerock.openbanking.analytics.model.entries.callback.CallBackResponseStatus)1 CallBackCountersKPIService (com.forgerock.openbanking.analytics.services.CallBackCountersKPIService)1 AggregatedPollingService (com.forgerock.openbanking.aspsp.rs.simulator.event.store.AggregatedPollingService)1 CallbackUrlsService (com.forgerock.openbanking.aspsp.rs.simulator.event.store.CallbackUrlsService)1 EventSubscriptionService (com.forgerock.openbanking.aspsp.rs.simulator.event.store.EventSubscriptionService)1 FREventSubscriptionData (com.forgerock.openbanking.common.model.openbanking.domain.event.FREventSubscriptionData)1 FRCallbackUrl (com.forgerock.openbanking.common.model.openbanking.persistence.event.FRCallbackUrl)1 EventSubject (com.forgerock.openbanking.common.services.notification.EventSubject)1 EventType (com.forgerock.openbanking.common.services.notification.EventType)1 URI (java.net.URI)1 Collection (java.util.Collection)1