Search in sources :

Example 1 with OAuthService

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);
    }
}
Also used : RefreshTokenRequest(net.petafuel.styx.core.xs2a.oauth.http.RefreshTokenRequest) OAuthService(net.petafuel.styx.core.xs2a.oauth.OAuthService) OAuthTokenExpiredException(net.petafuel.styx.core.xs2a.exceptions.OAuthTokenExpiredException) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)

Example 2 with OAuthService

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");
    }
}
Also used : RefreshTokenRequest(net.petafuel.styx.core.xs2a.oauth.http.RefreshTokenRequest) OAuthService(net.petafuel.styx.core.xs2a.oauth.OAuthService) OAuthSession(net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession) PersistentOAuthSession(net.petafuel.styx.core.persistence.layers.PersistentOAuthSession) Date(java.util.Date) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 3 with OAuthService

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;
    }
}
Also used : AuthorizationCodeRequest(net.petafuel.styx.core.xs2a.oauth.http.AuthorizationCodeRequest) OAuthService(net.petafuel.styx.core.xs2a.oauth.OAuthService) PersistentOAuthSession(net.petafuel.styx.core.persistence.layers.PersistentOAuthSession) OAuthSession(net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession) BankNotFoundException(net.petafuel.styx.core.banklookup.exceptions.BankNotFoundException) PersistenceEmptyResultSetException(net.petafuel.styx.core.persistence.PersistenceEmptyResultSetException) BankLookupFailedException(net.petafuel.styx.core.banklookup.exceptions.BankLookupFailedException)

Aggregations

OAuthService (net.petafuel.styx.core.xs2a.oauth.OAuthService)3 PersistentOAuthSession (net.petafuel.styx.core.persistence.layers.PersistentOAuthSession)2 OAuthSession (net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession)2 RefreshTokenRequest (net.petafuel.styx.core.xs2a.oauth.http.RefreshTokenRequest)2 Date (java.util.Date)1 BankLookupFailedException (net.petafuel.styx.core.banklookup.exceptions.BankLookupFailedException)1 BankNotFoundException (net.petafuel.styx.core.banklookup.exceptions.BankNotFoundException)1 PersistenceEmptyResultSetException (net.petafuel.styx.core.persistence.PersistenceEmptyResultSetException)1 BankRequestFailedException (net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)1 OAuthTokenExpiredException (net.petafuel.styx.core.xs2a.exceptions.OAuthTokenExpiredException)1 AuthorizationCodeRequest (net.petafuel.styx.core.xs2a.oauth.http.AuthorizationCodeRequest)1 Tag (org.junit.jupiter.api.Tag)1 Test (org.junit.jupiter.api.Test)1