use of com.forgerock.openbanking.common.error.exception.oauth2.OAuth2InvalidClientException in project openbanking-aspsp by OpenBankingToolkit.
the class DataApiController method generateData.
@Override
public ResponseEntity generateData(@ApiParam(value = "PSU User session") @CookieValue(value = "obri-session", required = true) String obriSession, @ApiParam(value = "The access token") @RequestHeader(name = HttpHeaders.AUTHORIZATION, required = true) String authorization, @ApiParam(value = "Data profile", required = false) @RequestParam(name = "profile", required = false) String profile, Principal principal) throws OBErrorException, OAuth2InvalidClientException, OAuth2BearerTokenUsageInvalidTokenException {
try {
log.debug("generateData() called");
String tppName = psd2WithSessionApiHelperService.getTppName(principal);
String psuName = psd2WithSessionApiHelperService.getPsuNameFromSession(obriSession);
verifyAccessTokenAndVerifyTppIdentity(authorization, tppName);
log.info("generateUserData() called with session for psu '{}' by tpp '{}'", psuName, tppName);
final String defaultProfile = profile != null ? profile : dataConfig.getDefaultProfile();
Optional<DataConfigurationProperties.DataTemplateProfile> any = dataConfig.getProfiles().stream().filter(t -> t.getId().equals(defaultProfile)).findAny();
if (!any.isPresent()) {
throw new OBErrorException(OBRIErrorType.DATA_INVALID_REQUEST, "Profile '" + profile + "' doesn't exist.");
}
if (!userDataService.deleteUserData(psuName)) {
psuCounterEntryKPIService.pushPsuCounterEntry(PsuCounterEntry.builder().count(1l).day(DateTime.now()).build());
}
return ResponseEntity.status(HttpStatus.CREATED).body(userDataService.generateUserData(psuName, defaultProfile));
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.BAD_REQUEST) {
log.debug("TPP bad request: {}", e.getResponseBodyAsString(), e);
throw new OBErrorException(OBRIErrorType.DATA_INVALID_REQUEST, e.getResponseBodyAsString());
} else {
log.error("Internal server: {}", e.getResponseBodyAsString(), e);
throw new OBErrorException(OBRIErrorType.SERVER_ERROR);
}
}
}
use of com.forgerock.openbanking.common.error.exception.oauth2.OAuth2InvalidClientException in project openbanking-aspsp by OpenBankingToolkit.
the class DataApiHelperService method getTppName.
/**
* getTppName
* @param principal - A principal representing the TPP that made a request to the service
* @return A <code>String</code> containing the tppName
*/
public String getTppName(Principal principal) throws OAuth2InvalidClientException {
String tppName = null;
Authentication authentication = (Authentication) principal;
if (authentication != null) {
Object authPrincipal = authentication.getPrincipal();
if (authPrincipal != null) {
UserDetails requestApiClient = (UserDetails) authentication.getPrincipal();
if (requestApiClient != null) {
tppName = requestApiClient.getUsername();
log.debug("getTppName() tppName is '{}'", tppName);
return tppName;
} else {
log.info("getTppName() Unable to obtain UserDetails from Principal '{}'", principal.getName());
}
} else {
log.info("getTppName() Unable to obtain authPrincipal from Authentication '{}'", authentication);
}
} else {
log.info("getTppName() Principal is not of type Authentication; '{}'", principal);
}
throw new OAuth2InvalidClientException("Could not obtain tppId from principal " + principal.toString());
}
use of com.forgerock.openbanking.common.error.exception.oauth2.OAuth2InvalidClientException in project openbanking-aspsp by OpenBankingToolkit.
the class TppRegistrationServiceTest method getTpp_throwsTppNotFound.
@Test
public void getTpp_throwsTppNotFound() throws OAuth2InvalidClientException {
// Given
String clientId = "clientId";
when(tppStoreService.findByClientId(clientId)).thenReturn(Optional.empty());
// When
Exception e = catchThrowableOfType(() -> tppRegistrationService.getTpp(clientId), Exception.class);
// Then
assertThat(e).isNotNull();
assertThat(e).isInstanceOf(OAuth2InvalidClientException.class);
}
use of com.forgerock.openbanking.common.error.exception.oauth2.OAuth2InvalidClientException in project openbanking-aspsp by OpenBankingToolkit.
the class TppRegistrationServiceTest method getTpp_throwsClientIdIsNull.
@Test
public void getTpp_throwsClientIdIsNull() throws OAuth2InvalidClientException {
// Given
String clientId = null;
when(tppStoreService.findByClientId(clientId)).thenReturn(Optional.empty());
// When
Exception e = catchThrowableOfType(() -> tppRegistrationService.getTpp(clientId), Exception.class);
// Then
assertThat(e).isNotNull();
assertThat(e).isInstanceOf(OAuth2InvalidClientException.class);
}
Aggregations