use of net.petafuel.styx.core.xs2a.exceptions.OAuthTokenExpiredException in project styx by petafuel.
the class PreAuthAccessFilter method filter.
/**
* Supressing java:S3776 -> need to rework logic to reduce code complexity
*
* @param containerRequestContext
*/
@Override
@SuppressWarnings("java:S3776")
public void filter(ContainerRequestContext containerRequestContext) {
XS2AStandard xs2AStandard = (XS2AStandard) containerRequestContext.getProperty(XS2AStandard.class.getName());
IOParser ioParser = new IOParser(xs2AStandard.getAspsp());
ImplementerOption ioPreAuthRequired = ioParser.get("IO6");
if (ioPreAuthRequired != null && ioPreAuthRequired.getOptions().get(IOParser.Option.REQUIRED)) {
LOG.info("ASPSP bic={} requires pre-auth", xs2AStandard.getAspsp().getBic());
// preauth is available and required for this bank -> check if preauth id is present
String preAuthIdString = containerRequestContext.getHeaderString(PRE_AUTH_ID);
if (preAuthIdString == null || "".equals(preAuthIdString)) {
throw new StyxException(new ResponseEntity("The requested aspsps requires a pre-step authorisation, preAuthId Header is missing", ResponseConstant.STYX_PREAUTH_HEADER_REQUIRED, ResponseCategory.ERROR, ResponseOrigin.CLIENT));
}
try {
UUID preAuthId = UUID.fromString(preAuthIdString);
OAuthSession oAuthSession = PersistentOAuthSession.getById(preAuthId);
LOG.info("Loaded state={} oauth_session", oAuthSession.getState());
STYX03.setPreauthId(preAuthId);
if (oAuthSession.getAccessToken() == null || oAuthSession.getAccessTokenExpiresAt() == null) {
throw new PersistenceEmptyResultSetException("The access_token data should be set");
}
if (oAuthSession.getAccessTokenExpiresAt().before(new Date())) {
if (oAuthSession.getRefreshTokenExpiresAt().after(new Date())) {
oAuthSession = refreshToken(oAuthSession);
} else {
throw new OAuthTokenExpiredException(OAuthTokenExpiredException.MESSAGE);
}
}
// Add the Authorization: <type> <credentials> header to the request context so we can use it later on demand
Map<String, String> additionalHeaders = new HashMap<>();
additionalHeaders.put(XS2AHeader.AUTHORIZATION, oAuthSession.getTokenType() + " " + oAuthSession.getAccessToken());
containerRequestContext.setProperty(PreAuthAccessFilter.class.getName(), additionalHeaders);
LOG.info("Successfully attached pre-auth from oAuthSessionState={}", oAuthSession.getState());
} catch (PersistenceEmptyResultSetException noOauthSessionFound) {
throw new StyxException(new ResponseEntity("There was no valid pre-step authorisation found for the specified preAuthId", ResponseConstant.STYX_PREAUTH_NOT_AVAILABLE, ResponseCategory.ERROR, ResponseOrigin.CLIENT));
} catch (OAuthTokenExpiredException tokenExpired) {
throw new StyxException(new ResponseEntity(tokenExpired.getMessage(), ResponseConstant.STYX_PREAUTH_EXPIRED, ResponseCategory.ERROR, ResponseOrigin.CLIENT));
}
}
}
use of net.petafuel.styx.core.xs2a.exceptions.OAuthTokenExpiredException in project styx by petafuel.
the class PreAuthAccessFilter method refreshToken.
private OAuthSession refreshToken(OAuthSession oAuthSession) throws OAuthTokenExpiredException {
String state = oAuthSession.getState();
RefreshTokenRequest request = new RefreshTokenRequest(oAuthSession.getRefreshToken());
OAuthService service = new OAuthService();
try {
oAuthSession = service.tokenRequest(oAuthSession.getTokenEndpoint(), request);
oAuthSession.setState(state);
PersistentOAuthSession.update(oAuthSession);
return oAuthSession;
} catch (BankRequestFailedException expiredToken) {
throw new OAuthTokenExpiredException(OAuthTokenExpiredException.MESSAGE);
}
}
use of net.petafuel.styx.core.xs2a.exceptions.OAuthTokenExpiredException in project styx by petafuel.
the class OAuthService method refreshToken.
public static OAuthSession refreshToken(OAuthSession oAuthSession) throws OAuthTokenExpiredException {
String state = oAuthSession.getState();
RefreshTokenRequest request = new RefreshTokenRequest(oAuthSession.getRefreshToken());
OAuthService service = new OAuthService();
try {
oAuthSession = service.tokenRequest(oAuthSession.getTokenEndpoint(), request);
oAuthSession.setState(state);
PersistentOAuthSession.update(oAuthSession);
return oAuthSession;
} catch (BankRequestFailedException expiredToken) {
throw new OAuthTokenExpiredException(OAuthTokenExpiredException.MESSAGE);
}
}
Aggregations