Search in sources :

Example 1 with ConnectionParameters

use of com.okta.oidc.net.ConnectionParameters in project okta-oidc-android by okta.

the class ConnectionParametersTest method create.

@Test
public void create() throws UnsupportedEncodingException {
    ConnectionParameters parameters = new ParameterBuilder().setRequestMethod(RequestMethod.POST).setRequestType(RequestType.AUTHORIZE).setPostParameter("post1", "post1").setRequestProperty("prop1", "prop1").setPostParameters(Collections.singletonMap("post2", "post2")).setRequestProperties(Collections.singletonMap("prop2", "prop2")).create();
    mConnParams = new ParameterBuilder().setRequestMethod(RequestMethod.POST).setRequestType(RequestType.AUTHORIZE).setPostParameter("post1", "post1").setRequestProperty("prop1", "prop1").setPostParameters(Collections.singletonMap("post2", "post2")).setRequestProperties(Collections.singletonMap("prop2", "prop2")).create();
    String decode1 = new String(parameters.getEncodedPostParameters(), DEFAULT_ENCODING);
    String decode2 = new String(mConnParams.getEncodedPostParameters(), DEFAULT_ENCODING);
    assertEquals(decode1, decode2);
    assertEquals(parameters.getRequestType(), mConnParams.getRequestType());
    assertEquals(parameters.postParameters(), mConnParams.postParameters());
    assertEquals(parameters.requestProperties(), mConnParams.requestProperties());
    assertEquals(parameters.requestMethod(), mConnParams.requestMethod());
}
Also used : ConnectionParameters(com.okta.oidc.net.ConnectionParameters) Test(org.junit.Test)

Example 2 with ConnectionParameters

use of com.okta.oidc.net.ConnectionParameters in project okta-oidc-android by okta.

the class OkHttp method buildRequest.

protected Request buildRequest(Uri uri, ConnectionParameters param) {
    try {
        mTrustAllSslContext = SSLContext.getInstance("SSL");
        mTrustAllSslContext.init(null, mTrustManager, new java.security.SecureRandom());
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new RuntimeException(e);
    }
    mTrustAllSslSocketFactory = mTrustAllSslContext.getSocketFactory();
    if (sOkHttpClient == null) {
        sOkHttpClient = new OkHttpClient.Builder().readTimeout(param.readTimeOutMs(), TimeUnit.MILLISECONDS).connectTimeout(param.connectionTimeoutMs(), TimeUnit.MILLISECONDS).hostnameVerifier((hostname, session) -> true).sslSocketFactory(mTrustAllSslSocketFactory, (X509TrustManager) mTrustManager[0]).build();
    }
    Request.Builder requestBuilder = new Request.Builder().url(uri.toString());
    for (Map.Entry<String, String> headerEntry : param.requestProperties().entrySet()) {
        String key = headerEntry.getKey();
        requestBuilder.addHeader(key, headerEntry.getValue());
    }
    if (param.requestMethod() == ConnectionParameters.RequestMethod.GET) {
        requestBuilder = requestBuilder.get();
    } else {
        Map<String, String> postParameters = param.postParameters();
        if (postParameters != null) {
            FormBody.Builder formBuilder = new FormBody.Builder();
            for (Map.Entry<String, String> postEntry : postParameters.entrySet()) {
                String key = postEntry.getKey();
                formBuilder.add(key, postEntry.getValue());
            }
            RequestBody formBody = formBuilder.build();
            requestBuilder.post(formBody);
        } else {
            requestBuilder.post(RequestBody.create(null, ""));
        }
    }
    return requestBuilder.build();
}
Also used : SSLContext(javax.net.ssl.SSLContext) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) WorkerThread(androidx.annotation.WorkerThread) TrustManager(javax.net.ssl.TrustManager) RequestBody(okhttp3.RequestBody) FormBody(okhttp3.FormBody) Map(java.util.Map) Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) OktaHttpClient(com.okta.oidc.net.OktaHttpClient) Request(okhttp3.Request) ConnectionParameters(com.okta.oidc.net.ConnectionParameters) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) X509TrustManager(javax.net.ssl.X509TrustManager) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InputStream(java.io.InputStream) Request(okhttp3.Request) FormBody(okhttp3.FormBody) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Example 3 with ConnectionParameters

use of com.okta.oidc.net.ConnectionParameters in project okta-oidc-android by okta.

the class ConnectionParametersTest method testBuilderWithTimeOut.

@Test
public void testBuilderWithTimeOut() throws Exception {
    mServer.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
    URL url = mServer.url("/").url();
    Date start = DateUtil.getNow();
    ConnectionParameters parameters = new ParameterBuilder().setRequestMethod(RequestMethod.GET).create();
    HttpClientImpl httpClient = new HttpClientImpl(5000, 2000);
    httpClient.connect(Uri.parse(url.toString()), parameters);
    try {
        httpClient.getResponseCode();
    } catch (SocketTimeoutException ex) {
    // NO-OP
    }
    long end = DateUtil.getNow().getTime();
    Date date = new Date(end - 2000);
    assertEquals(start.toString(), date.toString());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SocketTimeoutException(java.net.SocketTimeoutException) ConnectionParameters(com.okta.oidc.net.ConnectionParameters) URL(java.net.URL) Date(java.util.Date) Test(org.junit.Test)

Example 4 with ConnectionParameters

use of com.okta.oidc.net.ConnectionParameters in project okta-oidc-android by okta.

the class ConnectionParametersTest method testBuilderWithRequestProperty.

@Test
public void testBuilderWithRequestProperty() throws Exception {
    mServer.enqueue(new MockResponse());
    Map<String, String> properties = new HashMap<>();
    properties.put(USER_AGENT, CUSTOM_USER_AGENT);
    properties.put("PROP", "PROP");
    URL url = mServer.url("/").url();
    ConnectionParameters parameters = new ParameterBuilder().setRequestMethod(RequestMethod.GET).setRequestProperties(properties).setRequestProperty("PROP1", "PROP1").create();
    HttpClientImpl httpClient = new HttpClientImpl();
    httpClient.connect(Uri.parse(url.toString()), parameters);
    int code = httpClient.getResponseCode();
    RecordedRequest recordedRequest = mServer.takeRequest();
    assertEquals(code, HTTP_OK);
    // ConnectionParameters overrides custom user agent.
    assertEquals(recordedRequest.getHeader(USER_AGENT), USER_AGENT_HEADER);
    assertEquals(recordedRequest.getHeader("PROP"), "PROP");
    assertEquals(recordedRequest.getHeader("PROP1"), "PROP1");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) ConnectionParameters(com.okta.oidc.net.ConnectionParameters) URL(java.net.URL) Test(org.junit.Test)

Example 5 with ConnectionParameters

use of com.okta.oidc.net.ConnectionParameters in project okta-oidc-android by okta.

the class ConnectionParametersTest method testBuilderWithPostParameters.

@Test
public void testBuilderWithPostParameters() throws Exception {
    mServer.enqueue(new MockResponse());
    Map<String, String> params = new HashMap<>();
    params.put("client_id", CLIENT_ID);
    params.put("grant_type", GrantTypes.AUTHORIZATION_CODE);
    params.put("redirect_uri", REDIRECT_URI);
    params.put("code_verifier", "code_verifier");
    params.put("code", "code");
    params.put("nonce", "nonce");
    URL url = mServer.url("/").url();
    ConnectionParameters parameters = new ParameterBuilder().setRequestMethod(RequestMethod.POST).setPostParameters(params).setPostParameter("POST", "POST").create();
    HttpClientImpl httpClient = new HttpClientImpl();
    httpClient.connect(Uri.parse(url.toString()), parameters);
    int code = httpClient.getResponseCode();
    RecordedRequest recordedRequest = mServer.takeRequest();
    String postbody = recordedRequest.getBody().readUtf8Line();
    assertEquals(code, HTTP_OK);
    Assert.assertTrue(postbody.contains("client_id=" + CLIENT_ID));
    Assert.assertTrue(postbody.contains("grant_type=" + GrantTypes.AUTHORIZATION_CODE));
    Assert.assertTrue(postbody.contains("redirect_uri=" + URLEncoder.encode(REDIRECT_URI, "UTF-8")));
    Assert.assertTrue(postbody.contains("code_verifier=code_verifier"));
    Assert.assertTrue(postbody.contains("code=code"));
    Assert.assertTrue(postbody.contains("nonce=nonce"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) ConnectionParameters(com.okta.oidc.net.ConnectionParameters) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ConnectionParameters (com.okta.oidc.net.ConnectionParameters)5 Test (org.junit.Test)4 URL (java.net.URL)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 HashMap (java.util.HashMap)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Uri (android.net.Uri)1 NonNull (androidx.annotation.NonNull)1 WorkerThread (androidx.annotation.WorkerThread)1 OktaHttpClient (com.okta.oidc.net.OktaHttpClient)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SocketTimeoutException (java.net.SocketTimeoutException)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1