Search in sources :

Example 1 with RequestCallback

use of com.okta.oidc.RequestCallback in project okta-oidc-android by okta.

the class SampleActivity method onSignIn.

@Override
public void onSignIn(String username, String password) {
    mSignInDialog.dismiss();
    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
        mTvStatus.setText("Invalid username or password");
        return;
    }
    showNetworkProgress(true);
    mExecutor.submit(() -> {
        try {
            if (mAuthenticationClient == null) {
                return;
            }
            mAuthenticationClient.authenticate(username, password.toCharArray(), null, new AuthenticationStateHandlerAdapter() {

                @Override
                public void handleUnknown(AuthenticationResponse authenticationResponse) {
                    SampleActivity.this.runOnUiThread(() -> {
                        showNetworkProgress(false);
                        mTvStatus.setText(authenticationResponse.getStatus().name());
                    });
                }

                @Override
                public void handleLockedOut(AuthenticationResponse lockedOut) {
                    SampleActivity.this.runOnUiThread(() -> {
                        showNetworkProgress(false);
                        mTvStatus.setText("Account locked out");
                    });
                }

                @Override
                public void handleSuccess(AuthenticationResponse successResponse) {
                    String sessionToken = successResponse.getSessionToken();
                    mAuthClient.signIn(sessionToken, mPayload, new RequestCallback<Result, AuthorizationException>() {

                        @Override
                        public void onSuccess(@NonNull Result result) {
                            mTvStatus.setText("authentication authorized");
                            mIsSessionSignIn = true;
                            showAuthenticatedMode();
                            showNetworkProgress(false);
                        }

                        @Override
                        public void onError(String error, AuthorizationException exception) {
                            mTvStatus.setText(error);
                        }
                    });
                }
            });
        } catch (AuthenticationException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            runOnUiThread(() -> {
                showNetworkProgress(false);
                mTvStatus.setText(e.getMessage());
            });
        }
    });
}
Also used : RequestCallback(com.okta.oidc.RequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) AuthenticationException(com.okta.authn.sdk.AuthenticationException) NonNull(androidx.annotation.NonNull) AuthenticationStateHandlerAdapter(com.okta.authn.sdk.AuthenticationStateHandlerAdapter) AuthenticationResponse(com.okta.authn.sdk.resource.AuthenticationResponse) Result(com.okta.oidc.results.Result)

Example 2 with RequestCallback

use of com.okta.oidc.RequestCallback in project okta-oidc-android by okta.

the class SessionClientImpl method refreshToken.

public void refreshToken(final RequestCallback<Tokens, AuthorizationException> cb) {
    // Wrap the callback from the app because we want to be consistent in
    // returning a Tokens object instead of a TokenResponse.
    boolean isEmpty;
    if (Thread.holdsLock(refreshTokenRequestCallbacks)) {
        throw new RuntimeException("refreshToken can't be called from callback.");
    }
    CallbackWrapper<Tokens, AuthorizationException> wrapper = new CallbackWrapper<>(cb);
    synchronized (refreshTokenRequestCallbacks) {
        isEmpty = refreshTokenRequestCallbacks.isEmpty();
        refreshTokenRequestCallbacks.add(wrapper);
    }
    if (isEmpty) {
        executeSerial(wrapper, () -> {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            try {
                Tokens result = mSyncSessionClient.refreshToken();
                mDispatcher.submitResults(() -> {
                    synchronized (refreshTokenRequestCallbacks) {
                        for (RequestCallback<Tokens, AuthorizationException> callback : refreshTokenRequestCallbacks) {
                            callback.onSuccess(result);
                        }
                        refreshTokenRequestCallbacks.clear();
                    }
                });
            } catch (AuthorizationException ae) {
                mDispatcher.submitResults(() -> {
                    synchronized (refreshTokenRequestCallbacks) {
                        for (RequestCallback<Tokens, AuthorizationException> callback : refreshTokenRequestCallbacks) {
                            callback.onError(ae.error, ae);
                        }
                        refreshTokenRequestCallbacks.clear();
                    }
                });
            } catch (Exception ex) {
                mDispatcher.submitResults(() -> {
                    synchronized (refreshTokenRequestCallbacks) {
                        for (RequestCallback<Tokens, AuthorizationException> callback : refreshTokenRequestCallbacks) {
                            callback.onError(ex.getMessage(), new AuthorizationException(ex.getMessage(), ex));
                        }
                        refreshTokenRequestCallbacks.clear();
                    }
                });
            }
        });
    }
}
Also used : RequestCallback(com.okta.oidc.RequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) AuthorizationException(com.okta.oidc.util.AuthorizationException) Tokens(com.okta.oidc.Tokens)

Example 3 with RequestCallback

use of com.okta.oidc.RequestCallback in project okta-oidc-android by okta.

the class SessionClientImplTest method refreshTokenFailureInParallelCallbackCycle.

@Test
public void refreshTokenFailureInParallelCallbackCycle() 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> errorCb = new MockRequestCallback<>(latch);
    RequestCallback<Tokens, AuthorizationException> cb = new RequestCallback<Tokens, AuthorizationException>() {

        @Override
        public void onSuccess(@NonNull Tokens result) {
            try {
                mSessionClient.refreshToken(errorCb);
            } catch (RuntimeException runtimeException) {
                assertEquals(runtimeException.getMessage(), "refreshToken can't be called from callback.");
                latch.countDown();
            }
        }

        @Override
        public void onError(String error, AuthorizationException exception) {
            fail();
        }
    };
    mSessionClient.refreshToken(cb);
    latch.await();
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) MockRequestCallback(com.okta.oidc.util.MockRequestCallback) RequestCallback(com.okta.oidc.RequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) NonNull(androidx.annotation.NonNull) CountDownLatch(java.util.concurrent.CountDownLatch) Tokens(com.okta.oidc.Tokens) Test(org.junit.Test)

Aggregations

RequestCallback (com.okta.oidc.RequestCallback)3 AuthorizationException (com.okta.oidc.util.AuthorizationException)3 NonNull (androidx.annotation.NonNull)2 Tokens (com.okta.oidc.Tokens)2 AuthenticationException (com.okta.authn.sdk.AuthenticationException)1 AuthenticationStateHandlerAdapter (com.okta.authn.sdk.AuthenticationStateHandlerAdapter)1 AuthenticationResponse (com.okta.authn.sdk.resource.AuthenticationResponse)1 Result (com.okta.oidc.results.Result)1 MockRequestCallback (com.okta.oidc.util.MockRequestCallback)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1