Search in sources :

Example 26 with AuthorizationException

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

the class SessionClientImplTest method authorizedRequest.

@Test
public void authorizedRequest() throws InterruptedException, JSONException {
    mEndPoint.enqueueUserInfoSuccess();
    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);
    latch.await();
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    assertNotNull(cb.getResult());
    JSONObject result = cb.getResult();
    assertThat(recordedRequest.getHeader("state"), is(CUSTOM_STATE));
    assertNull(cb.getException());
    assertEquals("John Doe", result.getString("name"));
    assertEquals("Jimmy", result.getString("nickname"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) 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 27 with AuthorizationException

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

the class SessionClientImplTest method introspectTokenFailure.

@Test
public void introspectTokenFailure() throws InterruptedException {
    mEndPoint.enqueueReturnInvalidClient();
    final CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<IntrospectInfo, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.introspectToken(ACCESS_TOKEN, TokenTypeHint.ACCESS_TOKEN, cb);
    latch.await();
    assertNull(cb.getResult());
    assertNotNull(cb.getException());
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) IntrospectInfo(com.okta.oidc.net.response.IntrospectInfo) Test(org.junit.Test)

Example 28 with AuthorizationException

use of com.okta.oidc.util.AuthorizationException 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 29 with AuthorizationException

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

the class SessionClientImplTest method authorizedRequestFailure.

@Test
public void authorizedRequestFailure() throws InterruptedException, JSONException {
    mEndPoint.enqueueReturnInvalidClient();
    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);
    latch.await();
    assertNull(cb.getResult());
    assertNotNull(cb.getException());
}
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 30 with AuthorizationException

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

the class SessionClientImplTest method getUserProfile.

@Test
public void getUserProfile() throws InterruptedException, JSONException {
    mEndPoint.enqueueUserInfoSuccess();
    final CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<UserInfo, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.getUserProfile(cb);
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    latch.await();
    UserInfo result = cb.getResult();
    assertThat(recordedRequest.getHeader("Authorization"), is("Bearer " + ACCESS_TOKEN));
    assertThat(recordedRequest.getHeader("Accept"), is(ConnectionParameters.JSON_CONTENT_TYPE));
    assertThat(recordedRequest.getPath(), equalTo("/userinfo"));
    assertNotNull(result);
    assertEquals("John Doe", result.get("name"));
    assertEquals("Jimmy", result.get("nickname"));
}
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