Search in sources :

Example 1 with RefreshTokenRequest

use of net.petafuel.styx.core.xs2a.oauth.http.RefreshTokenRequest 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 RefreshTokenRequest

use of net.petafuel.styx.core.xs2a.oauth.http.RefreshTokenRequest 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 RefreshTokenRequest

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

Aggregations

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