use of net.petafuel.styx.core.xs2a.oauth.OAuthService 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.oauth.OAuthService in project styx by petafuel.
the class RefreshTokenTest method refreshTokenTest.
@Tag("integration")
@Test
public void refreshTokenTest() throws BankRequestFailedException {
String preAuthId = "cfa0cfd3-d4db-47c6-ad45-addececcfb02";
OAuthSession session = PersistentOAuthSession.getById(UUID.fromString(preAuthId));
if (session.getAccessTokenExpiresAt().before(new Date()) && session.getRefreshTokenExpiresAt().after(new Date())) {
System.out.println("Token has expired");
RefreshTokenRequest request = new RefreshTokenRequest(session.getRefreshToken());
OAuthService service = new OAuthService();
OAuthSession refreshed = service.tokenRequest(session.getTokenEndpoint(), request);
refreshed.setState(session.getState());
PersistentOAuthSession.update(refreshed);
Assert.assertNotEquals(session.getAccessToken(), refreshed.getAccessToken());
System.out.println("Token is refreshed");
}
}
use of net.petafuel.styx.core.xs2a.oauth.OAuthService in project styx by petafuel.
the class OAuthCallbackProcessor method handleSuccessfulOAuth2.
/**
* This will retrieve and save the access_token an other oauth related data from an ASPSP into the styx system
*
* @param code oauth query param
* @param state oauth query param
* @param path redirect path
* @return whether we were able to retrieve the access_token successfully
*/
private static boolean handleSuccessfulOAuth2(String code, String state, String path) {
OAuthService service = new OAuthService();
try {
OAuthSession stored = PersistentOAuthSession.getByState(state);
AuthorizationCodeRequest request = new AuthorizationCodeRequest(code, stored.getCodeVerifier());
request.setRedirectUri(request.getRedirectUri() + path);
request.setJsonBody(false);
OAuthSession authorized = service.tokenRequest(stored.getTokenEndpoint(), request);
authorized.setState(state);
PersistentOAuthSession.update(authorized);
LOG.info("Successfully received callback from ASPSP for OAuthSession state={}", stored.getState());
return true;
} catch (Exception e) {
LOG.error(e);
return false;
}
}
Aggregations