Search in sources :

Example 1 with AuthorizationException

use of com.okta.oidc.util.AuthorizationException in project okta-oidc-android by okta.

the class SyncAuthClientTest method signInNativeCancel.

@Test
public void signInNativeCancel() throws AuthorizationException, InterruptedException {
    String nonce = CodeVerifierUtil.generateRandomState();
    String state = CodeVerifierUtil.generateRandomState();
    String jws = TestValues.getJwt(mEndPoint.getUrl(), nonce, mConfig.getClientId());
    AuthenticationPayload payload = new AuthenticationPayload.Builder().addParameter("nonce", nonce).setState(state).build();
    mEndPoint.enqueueNativeRequestSuccess(state, 5);
    mEndPoint.enqueueTokenSuccess(jws);
    CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<Result, AuthorizationException> mockCallback = new MockRequestCallback<>(latch);
    mAuthClient.signIn(SESSION_TOKEN, payload, mockCallback);
    // wait for request to be created
    Thread.sleep(200);
    mAuthClient.cancel();
    latch.await();
    assertNull(mockCallback.getResult());
    assertNotNull(mockCallback.getException());
    String errorMessage = mockCallback.getException().getMessage();
    // Socket closed or canceled or stream is closed or network error.
    if (errorMessage == null) {
        assertTrue(mockCallback.getException().getCause() instanceof InterruptedException);
    } else {
        assertTrue("Socket closed".equals(errorMessage) || "Canceled".equals(errorMessage) || "stream is closed".equals(errorMessage) || "Network error".equals(errorMessage) || "interrupted".equals(errorMessage));
    }
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) AuthenticationPayload(com.okta.oidc.AuthenticationPayload) Result(com.okta.oidc.results.Result) Test(org.junit.Test)

Example 2 with AuthorizationException

use of com.okta.oidc.util.AuthorizationException in project okta-oidc-android by okta.

the class SyncAuthClientTest method signOutWithCallback.

@Test
public void signOutWithCallback() throws InterruptedException {
    mEndPoint.enqueueReturnSuccessEmptyBody();
    mEndPoint.enqueueReturnSuccessEmptyBody();
    MockResultCallback<Integer, AuthorizationException> mockCallback = new MockResultCallback<>();
    mAuthClient.signOut(mockCallback);
    // wait for request to be created
    Thread.sleep(200);
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    assertThat(recordedRequest.getPath(), equalTo("/revoke?client_id=CLIENT_ID&token=ACCESS_TOKEN"));
    recordedRequest = mEndPoint.takeRequest();
    assertThat(recordedRequest.getPath(), equalTo("/revoke?client_id=CLIENT_ID&token=REFRESH_TOKEN"));
    int status = mockCallback.getResult();
    assertEquals(status, SUCCESS);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) AuthorizationException(com.okta.oidc.util.AuthorizationException) MockResultCallback(com.okta.oidc.util.MockResultCallback) MockEndPoint(com.okta.oidc.util.MockEndPoint) Test(org.junit.Test)

Example 3 with AuthorizationException

use of com.okta.oidc.util.AuthorizationException in project okta-oidc-android by okta.

the class SessionClientImplTest method authorizedRequestCancel.

@Test
public void authorizedRequestCancel() throws InterruptedException, JSONException {
    mEndPoint.enqueueUserInfoSuccess(5);
    Uri uri = Uri.parse(mProviderConfig.userinfo_endpoint);
    HashMap<String, String> properties = new HashMap<>();
    properties.put("state", CUSTOM_STATE);
    final CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<JSONObject, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.authorizedRequest(uri, properties, null, ConnectionParameters.RequestMethod.GET, cb);
    // wait for request to be created
    Thread.sleep(200);
    mSessionClient.cancel();
    latch.await();
    assertNull(cb.getResult());
    assertNotNull(cb.getException());
    String errorMessage = cb.getException().getMessage();
    // Socket closed or canceled or stream is closed or network error.
    if (errorMessage == null) {
        assertTrue(cb.getException().getCause() instanceof InterruptedException);
    } else {
        assertTrue("Socket closed".equals(errorMessage) || "Canceled".equals(errorMessage) || "stream is closed".equals(errorMessage) || "Network error".equals(errorMessage) || "interrupted".equals(errorMessage));
    }
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) Test(org.junit.Test)

Example 4 with AuthorizationException

use of com.okta.oidc.util.AuthorizationException in project okta-oidc-android by okta.

the class SessionClientImplTest method refreshToken.

@Test
public void refreshToken() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    String nonce = CodeVerifierUtil.generateRandomState();
    String jws = TestValues.getJwt(mEndPoint.getUrl(), nonce, mConfig.getClientId());
    mEndPoint.enqueueTokenSuccess(jws);
    MockRequestCallback<Tokens, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.refreshToken(cb);
    latch.await();
    Tokens result = cb.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) CountDownLatch(java.util.concurrent.CountDownLatch) Tokens(com.okta.oidc.Tokens) Test(org.junit.Test)

Example 5 with AuthorizationException

use of com.okta.oidc.util.AuthorizationException in project okta-oidc-android by okta.

the class SessionClientImplTest method getUserProfileFailure.

@Test
public void getUserProfileFailure() throws InterruptedException, JSONException {
    mEndPoint.enqueueReturnUnauthorizedRevoked();
    final CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<UserInfo, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.getUserProfile(cb);
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    latch.await();
    assertNull(cb.getResult());
    assertNotNull(cb.getException());
    assertThat(recordedRequest.getPath(), equalTo("/userinfo"));
    assertEquals(TYPE_GENERAL_ERROR, cb.getException().type);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockRequestCallback(com.okta.oidc.util.MockRequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) UserInfo(com.okta.oidc.net.response.UserInfo) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

AuthorizationException (com.okta.oidc.util.AuthorizationException)39 Test (org.junit.Test)19 CountDownLatch (java.util.concurrent.CountDownLatch)16 MockRequestCallback (com.okta.oidc.util.MockRequestCallback)15 IOException (java.io.IOException)9 Tokens (com.okta.oidc.Tokens)8 JSONObject (org.json.JSONObject)8 HttpResponse (com.okta.oidc.net.HttpResponse)6 TokenResponse (com.okta.oidc.net.response.TokenResponse)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 IntrospectInfo (com.okta.oidc.net.response.IntrospectInfo)5 UserInfo (com.okta.oidc.net.response.UserInfo)5 Uri (android.net.Uri)4 JSONException (org.json.JSONException)4 NonNull (androidx.annotation.NonNull)3 WorkerThread (androidx.annotation.WorkerThread)3 Gson (com.google.gson.Gson)3 RequestCallback (com.okta.oidc.RequestCallback)3 ProviderConfiguration (com.okta.oidc.net.request.ProviderConfiguration)3 TokenRequest (com.okta.oidc.net.request.TokenRequest)3