Search in sources :

Example 36 with AuthorizationException

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

the class RevokeTokenRequest method executeRequest.

@Override
public Boolean 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) {
            return true;
        }
    } catch (IOException ex) {
        exception = new AuthorizationException(ex.getMessage(), ex);
    } catch (Exception e) {
        exception = AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.NETWORK_ERROR, e);
    } finally {
        if (response != null) {
            response.disconnect();
        }
        if (exception != null) {
            throw exception;
        }
    }
    return false;
}
Also used : AuthorizationException(com.okta.oidc.util.AuthorizationException) HttpResponse(com.okta.oidc.net.HttpResponse) IOException(java.io.IOException) IOException(java.io.IOException) AuthorizationException(com.okta.oidc.util.AuthorizationException)

Example 37 with AuthorizationException

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

the class ConfigurationRequest method executeRequest.

@WorkerThread
@Override
public ProviderConfiguration executeRequest(OktaHttpClient client) throws AuthorizationException {
    AuthorizationException exception = null;
    HttpResponse response = null;
    try {
        response = openConnection(client);
        JSONObject json = response.asJson();
        ProviderConfiguration configuration = new Gson().fromJson(json.toString(), ProviderConfiguration.class);
        configuration.validate(mIsOAuth2);
        return configuration;
    } catch (IOException ex) {
        exception = new AuthorizationException(ex.getMessage(), ex);
    } catch (JSONException ex) {
        exception = AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.JSON_DESERIALIZATION_ERROR, ex);
    } catch (IllegalArgumentException ex) {
        exception = AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.INVALID_DISCOVERY_DOCUMENT, 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 : JSONObject(org.json.JSONObject) AuthorizationException(com.okta.oidc.util.AuthorizationException) HttpResponse(com.okta.oidc.net.HttpResponse) Gson(com.google.gson.Gson) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONException(org.json.JSONException) IOException(java.io.IOException) AuthorizationException(com.okta.oidc.util.AuthorizationException) WorkerThread(androidx.annotation.WorkerThread)

Example 38 with AuthorizationException

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

the class BaseRequestTest method setUp.

@Before
public void setUp() throws Exception {
    mEndPoint = new MockEndPoint();
    mRequest = new BaseRequest<String, AuthorizationException>() {

        @Override
        public String executeRequest(OktaHttpClient client) throws AuthorizationException {
            return null;
        }
    };
    mClientFactory = new HttpClientFactory();
    mClientFactory.setClientType(mClientType);
    mHttpClient = mClientFactory.build();
}
Also used : OktaHttpClient(com.okta.oidc.net.OktaHttpClient) AuthorizationException(com.okta.oidc.util.AuthorizationException) MockEndPoint(com.okta.oidc.util.MockEndPoint) HttpClientFactory(com.okta.oidc.util.HttpClientFactory) Before(org.junit.Before)

Example 39 with AuthorizationException

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

the class HttpClientImplTest method cancel.

@Test
public void cancel() throws AuthorizationException {
    mEndPoint.enqueueUserInfoSuccess(5);
    final CountDownLatch latch = new CountDownLatch(1);
    final JSONObject[] result = new JSONObject[1];
    final AuthorizationException[] exception = new AuthorizationException[1];
    Thread t = new Thread(() -> {
        try {
            result[0] = mRequest.executeRequest(mHttpClient);
        } catch (AuthorizationException e) {
            exception[0] = e;
        }
    });
    t.start();
    mHttpClient.cancel();
    if (exception[0] != null) {
        // The exception can be canceled or stream is closed.
        String errorMessage = exception[0].getMessage();
        assertTrue("Canceled".equals(errorMessage) || "stream is closed".equals(errorMessage));
    } else {
        assertNull(result[0]);
    }
}
Also used : JSONObject(org.json.JSONObject) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

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