Search in sources :

Example 6 with AuthorizationException

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

the class OktaIdTokenTest method validateWithCustomValidatorThatAlwaysThrowsException.

@Test
public void validateWithCustomValidatorThatAlwaysThrowsException() throws AuthorizationException {
    mExpectedEx.expect(AuthorizationException.class);
    String jwt = TestValues.getJwt(CUSTOM_URL, CUSTOM_NONCE, mConfig.getClientId(), "fakeaud");
    OktaIdToken idToken = OktaIdToken.parseIdToken(jwt);
    String verifier = CodeVerifierUtil.generateRandomCodeVerifier();
    TokenRequest tokenRequest = TestValues.getTokenRequest(mConfig, getAuthorizeRequest(mConfig, verifier), getAuthorizeResponse(CUSTOM_STATE, CUSTOM_CODE), mConfiguration);
    idToken.validate(tokenRequest, oktaIdToken -> {
        throw new AuthorizationException("Expected", null);
    });
}
Also used : AuthorizationException(com.okta.oidc.util.AuthorizationException) TokenRequest(com.okta.oidc.net.request.TokenRequest) Test(org.junit.Test)

Example 7 with AuthorizationException

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

the class SyncWebAuthClientImpl method processSignInResult.

@NonNull
private Result processSignInResult(StateResult result) {
    if (result == null) {
        return Result.error(new AuthorizationException("Result is empty", new NullPointerException()));
    }
    switch(result.getStatus()) {
        case CANCELED:
            return Result.cancel();
        case ERROR:
            return Result.error(result.getException());
        case AUTHORIZED:
            mOktaState.setCurrentState(State.TOKEN_EXCHANGE);
            TokenResponse response;
            try {
                WebRequest authorizedRequest = mOktaState.getAuthorizeRequest();
                ProviderConfiguration providerConfiguration = mOktaState.getProviderConfiguration();
                AuthorizeResponse authResponse = (AuthorizeResponse) result.getAuthorizationResponse();
                if (isVerificationFlow((authResponse))) {
                    return processEmailVerification(authResponse);
                }
                validateResult(result.getAuthorizationResponse(), authorizedRequest);
                TokenRequest request = tokenExchange((AuthorizeResponse) result.getAuthorizationResponse(), providerConfiguration, (AuthorizeRequest) authorizedRequest);
                mCurrentRequest.set(new WeakReference<>(request));
                response = request.executeRequest(mHttpClient);
                mOktaState.save(response);
            } catch (OktaRepository.EncryptionException e) {
                return Result.error(EncryptionErrors.byEncryptionException(e));
            } catch (AuthorizationException e) {
                return Result.error(e);
            }
            return Result.success();
        default:
            return Result.error(new AuthorizationException("StateResult with invalid status: " + result.getStatus().name(), new IllegalStateException()));
    }
}
Also used : AuthorizeResponse(com.okta.oidc.net.response.web.AuthorizeResponse) TokenResponse(com.okta.oidc.net.response.TokenResponse) WebRequest(com.okta.oidc.net.request.web.WebRequest) AuthorizationException(com.okta.oidc.util.AuthorizationException) OktaRepository(com.okta.oidc.storage.OktaRepository) TokenRequest(com.okta.oidc.net.request.TokenRequest) ProviderConfiguration(com.okta.oidc.net.request.ProviderConfiguration) NonNull(androidx.annotation.NonNull)

Example 8 with AuthorizationException

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

the class AuthorizedRequest method executeRequest.

@Override
public JSONObject executeRequest(OktaHttpClient client) throws AuthorizationException {
    AuthorizationException exception = null;
    HttpResponse response = null;
    try {
        response = openConnection(client);
        return response.asJson();
    } catch (IOException io) {
        exception = new AuthorizationException(io.getMessage(), io);
    } catch (JSONException je) {
        exception = AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.JSON_DESERIALIZATION_ERROR, je);
    } catch (Exception e) {
        exception = AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.NETWORK_ERROR, e);
    } finally {
        if (response != null) {
            response.disconnect();
        }
        if (exception != null) {
            throw exception;
        }
    }
    return null;
}
Also used : AuthorizationException(com.okta.oidc.util.AuthorizationException) HttpResponse(com.okta.oidc.net.HttpResponse) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONException(org.json.JSONException) IOException(java.io.IOException) AuthorizationException(com.okta.oidc.util.AuthorizationException)

Example 9 with AuthorizationException

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

the class NativeAuthorizeRequest method executeRequest.

@Override
public AuthorizeResponse executeRequest(OktaHttpClient client) throws AuthorizationException {
    AuthorizationException exception = null;
    HttpResponse response = null;
    try {
        response = openConnection(client);
        if (response.getStatusCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
            exception = AuthorizationException.TokenRequestErrors.INVALID_CLIENT;
        } else if (response.getStatusCode() == HttpURLConnection.HTTP_OK || response.getStatusCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
            Uri locationUri = Uri.parse(response.getHeaderField("Location"));
            return AuthorizeResponse.fromUri(locationUri);
        }
    } catch (IOException ex) {
        exception = new AuthorizationException(ex.getMessage(), ex);
    } catch (Exception e) {
        exception = new AuthorizationException(e.getMessage(), e);
    } finally {
        if (response != null) {
            response.disconnect();
        }
        if (exception != null) {
            throw exception;
        }
    }
    return null;
}
Also used : AuthorizationException(com.okta.oidc.util.AuthorizationException) HttpResponse(com.okta.oidc.net.HttpResponse) IOException(java.io.IOException) Uri(android.net.Uri) IOException(java.io.IOException) AuthorizationException(com.okta.oidc.util.AuthorizationException)

Example 10 with AuthorizationException

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

the class TokenRequest method executeRequest.

@Override
public TokenResponse executeRequest(OktaHttpClient client) throws AuthorizationException {
    HttpResponse response = null;
    TokenResponse tokenResponse;
    try {
        response = openConnection(client);
        JSONObject json = response.asJsonWithErrorDescription();
        if (json.has(AuthorizationException.PARAM_ERROR)) {
            try {
                final String error = json.getString(AuthorizationException.PARAM_ERROR);
                throw AuthorizationException.fromOAuthTemplate(AuthorizationException.TokenRequestErrors.byString(error), error, json.optString(AuthorizationException.PARAM_ERROR_DESCRIPTION, null), UriUtil.parseUriIfAvailable(json.optString(AuthorizationException.PARAM_ERROR_URI)));
            } catch (JSONException jsonEx) {
                throw AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.JSON_DESERIALIZATION_ERROR, jsonEx);
            }
        }
        tokenResponse = new Gson().fromJson(json.toString(), TokenResponse.class);
        tokenResponse.setCreationTime(System.currentTimeMillis());
        if (tokenResponse.getIdToken() != null) {
            OktaIdToken idToken;
            try {
                idToken = OktaIdToken.parseIdToken(tokenResponse.getIdToken());
            } catch (IllegalArgumentException | JsonIOException ex) {
                Log.e(TAG, "", ex);
                throw AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.ID_TOKEN_PARSING_ERROR, ex);
            }
            idToken.validate(this, mConfig.getIdTokenValidator());
        }
        return tokenResponse;
    } catch (IOException ex) {
        throw new AuthorizationException(ex.getMessage(), ex);
    } catch (JSONException ex) {
        throw AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.JSON_DESERIALIZATION_ERROR, ex);
    } catch (AuthorizationException ae) {
        throw ae;
    } catch (Exception e) {
        throw AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.NETWORK_ERROR, e);
    } finally {
        if (response != null) {
            response.disconnect();
        }
    }
}
Also used : AuthorizationException(com.okta.oidc.util.AuthorizationException) HttpResponse(com.okta.oidc.net.HttpResponse) JSONException(org.json.JSONException) Gson(com.google.gson.Gson) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) OktaIdToken(com.okta.oidc.OktaIdToken) IOException(java.io.IOException) AuthorizationException(com.okta.oidc.util.AuthorizationException) JSONException(org.json.JSONException) JsonIOException(com.google.gson.JsonIOException) TokenResponse(com.okta.oidc.net.response.TokenResponse) JSONObject(org.json.JSONObject) JsonIOException(com.google.gson.JsonIOException)

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