Search in sources :

Example 6 with StateResult

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

the class AuthenticationResultHandlerTest method setAuthenticationListener.

@Test
public void setAuthenticationListener() throws InterruptedException {
    AuthenticationResultHandler.handler().mCachedResult = StateResult.canceled();
    AuthenticationResultHandler.handler().mCachedResultType = ResultType.SIGN_IN;
    final StateResult[] resultFromListener = new StateResult[1];
    final ResultType[] typeFromListener = new ResultType[1];
    CountDownLatch latch = new CountDownLatch(1);
    AuthResultListener listener = (result, type) -> {
        resultFromListener[0] = result;
        typeFromListener[0] = type;
        latch.countDown();
    };
    AuthenticationResultHandler.handler().setAuthenticationListener(listener);
    latch.await();
    assertEquals(resultFromListener[0].getStatus(), CANCELED);
    assertEquals(typeFromListener[0], ResultType.SIGN_IN);
    assertNull(AuthenticationResultHandler.handler().mCachedResult);
    assertNull(AuthenticationResultHandler.handler().mCachedResultType);
}
Also used : JSON_DESERIALIZATION_ERROR(com.okta.oidc.util.AuthorizationException.GeneralErrors.JSON_DESERIALIZATION_ERROR) AuthResultListener(com.okta.oidc.AuthenticationResultHandler.AuthResultListener) RESULT_OK(android.app.Activity.RESULT_OK) LOGGED_OUT(com.okta.oidc.AuthenticationResultHandler.Status.LOGGED_OUT) Uri(android.net.Uri) RunWith(org.junit.runner.RunWith) Config(org.robolectric.annotation.Config) Intent(android.content.Intent) TestValues(com.okta.oidc.util.TestValues) REQUEST_CODE_SIGN_IN(com.okta.oidc.OktaResultFragment.REQUEST_CODE_SIGN_IN) LogoutResponse(com.okta.oidc.net.response.web.LogoutResponse) AuthorizeResponse(com.okta.oidc.net.response.web.AuthorizeResponse) CUSTOM_STATE(com.okta.oidc.util.TestValues.CUSTOM_STATE) AuthorizationRequestErrors(com.okta.oidc.util.AuthorizationException.AuthorizationRequestErrors) RESULT_CANCELED(android.app.Activity.RESULT_CANCELED) ResultType(com.okta.oidc.AuthenticationResultHandler.ResultType) CANCELED(com.okta.oidc.AuthenticationResultHandler.Status.CANCELED) Assert.assertNotNull(org.junit.Assert.assertNotNull) EXTRA_EXCEPTION(com.okta.oidc.OktaAuthenticationActivity.EXTRA_EXCEPTION) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AuthorizationException(com.okta.oidc.util.AuthorizationException) Status(com.okta.oidc.AuthenticationResultHandler.Status) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) CountDownLatch(java.util.concurrent.CountDownLatch) StateResult(com.okta.oidc.AuthenticationResultHandler.StateResult) Assert.assertNull(org.junit.Assert.assertNull) REQUEST_CODE_SIGN_OUT(com.okta.oidc.OktaResultFragment.REQUEST_CODE_SIGN_OUT) CUSTOM_CODE(com.okta.oidc.util.TestValues.CUSTOM_CODE) Assert.assertEquals(org.junit.Assert.assertEquals) StateResult(com.okta.oidc.AuthenticationResultHandler.StateResult) AuthResultListener(com.okta.oidc.AuthenticationResultHandler.AuthResultListener) ResultType(com.okta.oidc.AuthenticationResultHandler.ResultType) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 7 with StateResult

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

the class AuthenticationResultHandlerTest method onActivityResultSignOutFailed.

@Test
public void onActivityResultSignOutFailed() throws InterruptedException {
    Intent intent = new Intent();
    intent.setData(Uri.parse("com.okta.test:/authorize?error=" + TestValues.ERROR));
    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]);
    assertEquals(stateResult[0].getStatus(), Status.ERROR);
    assertEquals(stateResult[0].getException().error, TestValues.ERROR);
    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)

Example 8 with StateResult

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

the class SyncWebAuthClientImpl method signOutOfOkta.

@Override
@AnyThread
public Result signOutOfOkta(@NonNull final Activity activity) {
    try {
        mOktaState.setCurrentState(State.SIGN_OUT_REQUEST);
        WebRequest request;
        request = new LogoutRequest.Builder().provideConfiguration(mOktaState.getProviderConfiguration()).config(mOidcConfig).tokenResponse(mOktaState.getTokenResponse()).state(CodeVerifierUtil.generateRandomState()).create();
        mOktaState.save(request);
        StateResult logoutResult = startSignOut(activity, request);
        return processSignOutResult(logoutResult);
    } catch (InterruptedException e) {
        return Result.cancel();
    } catch (OktaRepository.EncryptionException e) {
        return Result.error(EncryptionErrors.byEncryptionException(e));
    } catch (AuthorizationException e) {
        return Result.error(e);
    } catch (NullPointerException e) {
        return Result.error(new AuthorizationException(e.getMessage(), e));
    } finally {
        resetCurrentState();
    }
}
Also used : WebRequest(com.okta.oidc.net.request.web.WebRequest) StateResult(com.okta.oidc.AuthenticationResultHandler.StateResult) AuthorizationException(com.okta.oidc.util.AuthorizationException) OktaRepository(com.okta.oidc.storage.OktaRepository) AnyThread(androidx.annotation.AnyThread)

Example 9 with StateResult

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

the class SyncWebAuthClientImpl method signIn.

@Override
@WorkerThread
public Result signIn(@NonNull final Activity activity, @Nullable AuthenticationPayload payload) {
    mCancel.set(false);
    try {
        if (!isRedirectUrisRegistered(mOidcConfig.getRedirectUri(), activity)) {
            String errorDescription = "No uri registered to handle redirect " + "or multiple applications registered";
            Log.e(TAG, errorDescription);
            throw new AuthorizationException(TYPE_OAUTH_REGISTRATION_ERROR, INVALID_REDIRECT_URI.code, INVALID_REDIRECT_URI.error, errorDescription, null, null);
        }
        ProviderConfiguration configuration = obtainNewConfiguration();
        checkIfCanceled();
        WebRequest request = new AuthorizeRequest.Builder().config(mOidcConfig).providerConfiguration(configuration).authenticationPayload(payload).create();
        mOktaState.save(request);
        mOktaState.setCurrentState(State.SIGN_IN_REQUEST);
        StateResult authResult = startSignIn(activity, request);
        return processSignInResult(authResult);
    } catch (AuthorizationException e) {
        return Result.error(e);
    } catch (IOException | InterruptedException e) {
        return Result.cancel();
    } catch (OktaRepository.EncryptionException e) {
        return Result.error(EncryptionErrors.byEncryptionException(e));
    } finally {
        resetCurrentState();
    }
}
Also used : WebRequest(com.okta.oidc.net.request.web.WebRequest) AuthorizationException(com.okta.oidc.util.AuthorizationException) StateResult(com.okta.oidc.AuthenticationResultHandler.StateResult) OktaRepository(com.okta.oidc.storage.OktaRepository) IOException(java.io.IOException) ProviderConfiguration(com.okta.oidc.net.request.ProviderConfiguration) WorkerThread(androidx.annotation.WorkerThread)

Example 10 with StateResult

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

the class SyncWebAuthClientImpl method startSignIn.

private StateResult startSignIn(Activity activity, WebRequest request) throws InterruptedException {
    AtomicReference<StateResult> resultWrapper = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    if (activity instanceof FragmentActivity) {
        if (!((FragmentActivity) activity).getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
            resetCurrentState();
            return StateResult.canceled();
        }
        activity.runOnUiThread(() -> addLoginFragment(request, mCustomTabOptions, (FragmentActivity) activity, mSupportedBrowsers));
    } else {
        Intent intent = createAuthIntent(activity, request.toUri(), mCustomTabOptions, mSupportedBrowsers);
        activity.startActivityForResult(intent, REQUEST_CODE_SIGN_IN);
    }
    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)

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