Search in sources :

Example 16 with TokenResponse

use of com.okta.oidc.net.response.TokenResponse in project okta-oidc-android by okta.

the class SyncSessionClientImpl method refreshToken.

@Override
public Tokens refreshToken() throws AuthorizationException {
    try {
        RefreshTokenRequest request = refreshTokenRequest(mOktaState.getProviderConfiguration(), mOktaState.getTokenResponse());
        mCurrentRequest.set(new WeakReference<>(request));
        TokenResponse tokenResponse = request.executeRequest(mHttpClient);
        mOktaState.save(tokenResponse);
        return new Tokens(tokenResponse);
    } catch (OktaRepository.EncryptionException e) {
        throw AuthorizationException.EncryptionErrors.byEncryptionException(e);
    }
}
Also used : RefreshTokenRequest(com.okta.oidc.net.request.RefreshTokenRequest) TokenResponse(com.okta.oidc.net.response.TokenResponse) OktaRepository(com.okta.oidc.storage.OktaRepository) Tokens(com.okta.oidc.Tokens)

Example 17 with TokenResponse

use of com.okta.oidc.net.response.TokenResponse in project okta-oidc-android by okta.

the class SyncSessionClientImpl method migrateTo.

@Override
public void migrateTo(EncryptionManager manager) throws AuthorizationException {
    try {
        final ProviderConfiguration providerConfiguration = mOktaState.getProviderConfiguration();
        final TokenResponse tokenResponse = mOktaState.getTokenResponse();
        final WebRequest authorizedRequest = mOktaState.getAuthorizeRequest();
        clear();
        mOktaState.setEncryptionManager(manager);
        mOktaState.save(providerConfiguration);
        mOktaState.save(tokenResponse);
        mOktaState.save(authorizedRequest);
    } catch (OktaRepository.EncryptionException e) {
        throw AuthorizationException.EncryptionErrors.byEncryptionException(e);
    }
}
Also used : TokenResponse(com.okta.oidc.net.response.TokenResponse) WebRequest(com.okta.oidc.net.request.web.WebRequest) OktaRepository(com.okta.oidc.storage.OktaRepository) ProviderConfiguration(com.okta.oidc.net.request.ProviderConfiguration)

Example 18 with TokenResponse

use of com.okta.oidc.net.response.TokenResponse in project okta-oidc-android by okta.

the class SessionClientImplTest method refreshTokenInParallel.

@Test
public void refreshTokenInParallel() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(2);
    String nonce = CodeVerifierUtil.generateRandomState();
    String jws = TestValues.getJwt(mEndPoint.getUrl(), nonce, mConfig.getClientId());
    mEndPoint.enqueueTokenSuccess(jws);
    MockRequestCallback<Tokens, AuthorizationException> cb = new MockRequestCallback<>(latch);
    MockRequestCallback<Tokens, AuthorizationException> cb2 = new MockRequestCallback<>(latch);
    new Thread(() -> mSessionClient.refreshToken(cb)).start();
    new Thread(() -> mSessionClient.refreshToken(cb2)).start();
    latch.await();
    Tokens result = cb.getResult();
    Tokens result2 = cb2.getResult();
    TokenResponse original = mGson.fromJson(String.format(TOKEN_SUCCESS, jws), TokenResponse.class);
    assertEquals(original.getIdToken(), result.getIdToken());
    assertEquals(original.getRefreshToken(), result.getRefreshToken());
    assertEquals(original.getIdToken(), result.getIdToken());
    assertEquals(result, result2);
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) TokenResponse(com.okta.oidc.net.response.TokenResponse) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) Tokens(com.okta.oidc.Tokens) Test(org.junit.Test)

Example 19 with TokenResponse

use of com.okta.oidc.net.response.TokenResponse in project okta-oidc-android by okta.

the class SessionClientImplTest method requestsAreRunInSerial.

@Test
public void requestsAreRunInSerial() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(2);
    MockRequestCallback<UserInfo, AuthorizationException> userProfileCallback = new MockRequestCallback<>(latch);
    mEndPoint.enqueueUserInfoSuccess();
    mSessionClient.getUserProfile(userProfileCallback);
    String nonce = CodeVerifierUtil.generateRandomState();
    String jws = TestValues.getJwt(mEndPoint.getUrl(), nonce, mConfig.getClientId());
    mEndPoint.enqueueTokenSuccess(jws);
    MockRequestCallback<Tokens, AuthorizationException> refreshCallback = new MockRequestCallback<>(latch);
    mSessionClient.refreshToken(refreshCallback);
    latch.await();
    UserInfo userInfoResult = userProfileCallback.getResult();
    assertNotNull(userInfoResult);
    assertEquals("John Doe", userInfoResult.get("name"));
    assertEquals("Jimmy", userInfoResult.get("nickname"));
    Tokens result = refreshCallback.getResult();
    TokenResponse original = mGson.fromJson(String.format(TOKEN_SUCCESS, jws), TokenResponse.class);
    assertEquals(original.getIdToken(), result.getIdToken());
    assertEquals(original.getRefreshToken(), result.getRefreshToken());
    assertEquals(original.getIdToken(), result.getIdToken());
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) TokenResponse(com.okta.oidc.net.response.TokenResponse) AuthorizationException(com.okta.oidc.util.AuthorizationException) UserInfo(com.okta.oidc.net.response.UserInfo) CountDownLatch(java.util.concurrent.CountDownLatch) Tokens(com.okta.oidc.Tokens) Test(org.junit.Test)

Example 20 with TokenResponse

use of com.okta.oidc.net.response.TokenResponse in project okta-oidc-android by okta.

the class SyncSessionClientImplTest method refreshTokenRequest.

@Test
public void refreshTokenRequest() throws InterruptedException, JSONException, AuthorizationException, OktaRepository.EncryptionException {
    mOktaState.save(mTokenResponse);
    RefreshTokenRequest request = mSyncSessionClientImpl.refreshTokenRequest(mOktaState.getProviderConfiguration(), mTokenResponse);
    String nonce = CodeVerifierUtil.generateRandomState();
    String jws = TestValues.getJwt(mEndPoint.getUrl(), nonce, mConfig.getClientId());
    mEndPoint.enqueueTokenSuccess(jws);
    TokenResponse response = request.executeRequest(mHttpClient);
    assertNotNull(response);
    TokenResponse original = mGson.fromJson(String.format(TOKEN_SUCCESS, jws), TokenResponse.class);
    assertEquals(original.getIdToken(), response.getIdToken());
    assertEquals(original.getRefreshToken(), response.getRefreshToken());
    assertEquals(original.getIdToken(), response.getIdToken());
}
Also used : RefreshTokenRequest(com.okta.oidc.net.request.RefreshTokenRequest) TokenResponse(com.okta.oidc.net.response.TokenResponse) Test(org.junit.Test)

Aggregations

TokenResponse (com.okta.oidc.net.response.TokenResponse)25 Test (org.junit.Test)16 ProviderConfiguration (com.okta.oidc.net.request.ProviderConfiguration)7 AuthorizationException (com.okta.oidc.util.AuthorizationException)6 OktaRepository (com.okta.oidc.storage.OktaRepository)5 Gson (com.google.gson.Gson)4 Tokens (com.okta.oidc.Tokens)4 TokenRequest (com.okta.oidc.net.request.TokenRequest)4 AuthorizeResponse (com.okta.oidc.net.response.web.AuthorizeResponse)4 RefreshTokenRequest (com.okta.oidc.net.request.RefreshTokenRequest)3 AuthorizeRequest (com.okta.oidc.net.request.web.AuthorizeRequest)3 MockRequestCallback (com.okta.oidc.util.MockRequestCallback)3 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 JSONObject (org.json.JSONObject)3 OIDCConfig (com.okta.oidc.OIDCConfig)2 AuthorizedRequest (com.okta.oidc.net.request.AuthorizedRequest)2 RevokeTokenRequest (com.okta.oidc.net.request.RevokeTokenRequest)2 WebRequest (com.okta.oidc.net.request.web.WebRequest)2 UserInfo (com.okta.oidc.net.response.UserInfo)2