Search in sources :

Example 16 with AuthorizationException

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

the class SyncAuthClientImpl method signIn.

@WorkerThread
@Override
public Result signIn(String sessionToken, @Nullable AuthenticationPayload payload) {
    try {
        mCancel.set(false);
        ProviderConfiguration providerConfiguration = obtainNewConfiguration();
        checkIfCanceled();
        mOktaState.setCurrentState(State.SIGN_IN_REQUEST);
        NativeAuthorizeRequest request = nativeAuthorizeRequest(sessionToken, providerConfiguration, payload);
        mCurrentRequest.set(new WeakReference<>(request));
        // Save the nativeAuth request in a AuthRequest because it is needed to verify results.
        AuthorizeRequest authRequest = new AuthorizeRequest(request.getParameters());
        mOktaState.save(authRequest);
        AuthorizeResponse authResponse = request.executeRequest(mHttpClient);
        checkIfCanceled();
        // This flow should never happen but if it does throw a exception.
        if (isVerificationFlow(authResponse)) {
            return Result.error(new AuthorizationException("Email verification required. Session: " + authResponse.getSessionHint(), null));
        }
        validateResult(authResponse, authRequest);
        mOktaState.setCurrentState(State.TOKEN_EXCHANGE);
        TokenRequest requestToken = tokenExchange(authResponse, providerConfiguration, authRequest);
        mCurrentRequest.set(new WeakReference<>(requestToken));
        TokenResponse tokenResponse = requestToken.executeRequest(mHttpClient);
        mOktaState.save(tokenResponse);
        return Result.success();
    } catch (AuthorizationException e) {
        return Result.error(e);
    } catch (IOException e) {
        return Result.cancel();
    } catch (Exception e) {
        return Result.error(new AuthorizationException(OTHER.code, e.getMessage(), e));
    } finally {
        resetCurrentState();
    }
}
Also used : AuthorizeResponse(com.okta.oidc.net.response.web.AuthorizeResponse) TokenResponse(com.okta.oidc.net.response.TokenResponse) AuthorizeRequest(com.okta.oidc.net.request.web.AuthorizeRequest) NativeAuthorizeRequest(com.okta.oidc.net.request.NativeAuthorizeRequest) AuthorizationException(com.okta.oidc.util.AuthorizationException) TokenRequest(com.okta.oidc.net.request.TokenRequest) IOException(java.io.IOException) NativeAuthorizeRequest(com.okta.oidc.net.request.NativeAuthorizeRequest) IOException(java.io.IOException) AuthorizationException(com.okta.oidc.util.AuthorizationException) ProviderConfiguration(com.okta.oidc.net.request.ProviderConfiguration) WorkerThread(androidx.annotation.WorkerThread)

Example 17 with AuthorizationException

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

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

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

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

the class SessionClientImpl method introspectToken.

public void introspectToken(String token, String tokenType, final RequestCallback<IntrospectInfo, AuthorizationException> cb) {
    CallbackWrapper<IntrospectInfo, AuthorizationException> wrapper = new CallbackWrapper<>(cb);
    executeSerial(wrapper, () -> {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        try {
            IntrospectInfo introspectInfo = mSyncSessionClient.introspectToken(token, tokenType);
            mDispatcher.submitResults(() -> wrapper.onSuccess(introspectInfo));
        } catch (AuthorizationException ae) {
            mDispatcher.submitResults(() -> wrapper.onError(ae.error, ae));
        } catch (Exception ex) {
            mDispatcher.submitResults(() -> wrapper.onError(ex.getMessage(), new AuthorizationException(ex.getMessage(), ex)));
        }
    });
}
Also used : AuthorizationException(com.okta.oidc.util.AuthorizationException) IntrospectInfo(com.okta.oidc.net.response.IntrospectInfo) AuthorizationException(com.okta.oidc.util.AuthorizationException)

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