Search in sources :

Example 1 with StateResult

use of com.okta.oidc.AuthenticationResultHandler.StateResult in project okta-oidc-android by okta.

the class SyncWebAuthClientTest method handleActivityResult.

@Test
public void handleActivityResult() throws InterruptedException {
    Intent intent = new Intent();
    intent.setData(Uri.parse("com.okta.test:/callback?code=" + CUSTOM_CODE + "&state=" + CUSTOM_STATE));
    CountDownLatch latch = new CountDownLatch(1);
    final StateResult[] stateResult = new StateResult[1];
    handler().setAuthenticationListener((result, type) -> {
        stateResult[0] = result;
        latch.countDown();
    });
    handler().onActivityResult(OktaResultFragment.REQUEST_CODE_SIGN_IN, RESULT_OK, intent);
    latch.await();
    assertNotNull(stateResult[0]);
    AuthorizeResponse response = (AuthorizeResponse) stateResult[0].getAuthorizationResponse();
    assertNotNull(response);
    assertEquals(stateResult[0].getStatus(), AuthenticationResultHandler.Status.AUTHORIZED);
    assertEquals(response.getState(), CUSTOM_STATE);
    assertEquals(response.getCode(), CUSTOM_CODE);
}
Also used : AuthorizeResponse(com.okta.oidc.net.response.web.AuthorizeResponse) StateResult(com.okta.oidc.AuthenticationResultHandler.StateResult) Intent(android.content.Intent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with StateResult

use of com.okta.oidc.AuthenticationResultHandler.StateResult in project okta-oidc-android by okta.

the class SyncWebAuthClientImpl method startSignOut.

private StateResult startSignOut(Activity activity, WebRequest request) throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<StateResult> resultWrapper = new AtomicReference<>();
    if (activity instanceof FragmentActivity) {
        if (!((FragmentActivity) activity).getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
            resetCurrentState();
            return StateResult.canceled();
        }
        activity.runOnUiThread(() -> addLogoutFragment(request, mCustomTabOptions, (FragmentActivity) activity, mSupportedBrowsers));
    } else {
        Intent intent = createAuthIntent(activity, request.toUri(), mCustomTabOptions, mSupportedBrowsers);
        activity.startActivityForResult(intent, REQUEST_CODE_SIGN_OUT);
    }
    mHandler.setAuthenticationListener((result, type) -> {
        resultWrapper.set(result);
        latch.countDown();
    });
    latch.await();
    return resultWrapper.get();
}
Also used : FragmentActivity(androidx.fragment.app.FragmentActivity) StateResult(com.okta.oidc.AuthenticationResultHandler.StateResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) OktaResultFragment.createAuthIntent(com.okta.oidc.OktaResultFragment.createAuthIntent) Intent(android.content.Intent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with StateResult

use of com.okta.oidc.AuthenticationResultHandler.StateResult in project okta-oidc-android by okta.

the class AuthenticationResultHandlerTest method onActivityResultSignInCanceled.

@Test
public void onActivityResultSignInCanceled() throws InterruptedException {
    Intent intent = new Intent();
    CountDownLatch latch = new CountDownLatch(1);
    final StateResult[] stateResult = new StateResult[1];
    final ResultType[] stateType = new ResultType[1];
    AuthenticationResultHandler.handler().setAuthenticationListener((result, type) -> {
        stateResult[0] = result;
        stateType[0] = type;
        latch.countDown();
    });
    AuthenticationResultHandler.handler().onActivityResult(REQUEST_CODE_SIGN_IN, RESULT_CANCELED, intent);
    latch.await();
    assertNotNull(stateResult[0]);
    assertEquals(stateResult[0].getStatus(), CANCELED);
    assertEquals(stateType[0], ResultType.SIGN_IN);
    assertNull(AuthenticationResultHandler.handler().mCachedResult);
    assertNull(AuthenticationResultHandler.handler().mCachedResultType);
}
Also used : StateResult(com.okta.oidc.AuthenticationResultHandler.StateResult) Intent(android.content.Intent) ResultType(com.okta.oidc.AuthenticationResultHandler.ResultType) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with StateResult

use of com.okta.oidc.AuthenticationResultHandler.StateResult in project okta-oidc-android by okta.

the class AuthenticationResultHandlerTest method onActivityResultSignOutSuccess.

@Test
public void onActivityResultSignOutSuccess() throws InterruptedException {
    Intent intent = new Intent();
    intent.setData(Uri.parse("com.okta.test:/logout?state=" + CUSTOM_STATE));
    CountDownLatch latch = new CountDownLatch(1);
    final StateResult[] stateResult = new StateResult[1];
    final ResultType[] stateType = new ResultType[1];
    AuthenticationResultHandler.handler().setAuthenticationListener((result, type) -> {
        stateResult[0] = result;
        stateType[0] = type;
        latch.countDown();
    });
    AuthenticationResultHandler.handler().onActivityResult(REQUEST_CODE_SIGN_OUT, RESULT_OK, intent);
    latch.await();
    assertNotNull(stateResult[0]);
    LogoutResponse response = (LogoutResponse) stateResult[0].getAuthorizationResponse();
    assertNotNull(response);
    assertEquals(stateResult[0].getStatus(), LOGGED_OUT);
    assertEquals(response.getState(), CUSTOM_STATE);
    assertEquals(stateType[0], ResultType.SIGN_OUT);
    assertNull(AuthenticationResultHandler.handler().mCachedResult);
    assertNull(AuthenticationResultHandler.handler().mCachedResultType);
}
Also used : LogoutResponse(com.okta.oidc.net.response.web.LogoutResponse) StateResult(com.okta.oidc.AuthenticationResultHandler.StateResult) Intent(android.content.Intent) ResultType(com.okta.oidc.AuthenticationResultHandler.ResultType) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with StateResult

use of com.okta.oidc.AuthenticationResultHandler.StateResult in project okta-oidc-android by okta.

the class AuthenticationResultHandlerTest method onActivityResultSignOutCanceled.

@Test
public void onActivityResultSignOutCanceled() throws InterruptedException {
    Intent intent = new Intent();
    CountDownLatch latch = new CountDownLatch(1);
    final StateResult[] stateResult = new StateResult[1];
    final ResultType[] stateType = new ResultType[1];
    AuthenticationResultHandler.handler().setAuthenticationListener((result, type) -> {
        stateResult[0] = result;
        stateType[0] = type;
        latch.countDown();
    });
    AuthenticationResultHandler.handler().onActivityResult(REQUEST_CODE_SIGN_OUT, RESULT_CANCELED, intent);
    latch.await();
    assertNotNull(stateResult[0]);
    assertEquals(stateResult[0].getStatus(), CANCELED);
    assertEquals(stateType[0], ResultType.SIGN_OUT);
    assertNull(AuthenticationResultHandler.handler().mCachedResult);
    assertNull(AuthenticationResultHandler.handler().mCachedResultType);
}
Also used : StateResult(com.okta.oidc.AuthenticationResultHandler.StateResult) Intent(android.content.Intent) ResultType(com.okta.oidc.AuthenticationResultHandler.ResultType) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

StateResult (com.okta.oidc.AuthenticationResultHandler.StateResult)15 Intent (android.content.Intent)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 Test (org.junit.Test)11 ResultType (com.okta.oidc.AuthenticationResultHandler.ResultType)10 AuthorizeResponse (com.okta.oidc.net.response.web.AuthorizeResponse)3 AuthorizationException (com.okta.oidc.util.AuthorizationException)3 FragmentActivity (androidx.fragment.app.FragmentActivity)2 OktaResultFragment.createAuthIntent (com.okta.oidc.OktaResultFragment.createAuthIntent)2 WebRequest (com.okta.oidc.net.request.web.WebRequest)2 LogoutResponse (com.okta.oidc.net.response.web.LogoutResponse)2 OktaRepository (com.okta.oidc.storage.OktaRepository)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 RESULT_CANCELED (android.app.Activity.RESULT_CANCELED)1 RESULT_OK (android.app.Activity.RESULT_OK)1 Uri (android.net.Uri)1 AnyThread (androidx.annotation.AnyThread)1 WorkerThread (androidx.annotation.WorkerThread)1 AuthResultListener (com.okta.oidc.AuthenticationResultHandler.AuthResultListener)1 Status (com.okta.oidc.AuthenticationResultHandler.Status)1