package com.commit451.gitlab.api;

import com.commit451.gitlab.model.Account;
import com.commit451.gitlab.ssl.CustomTrustManager;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Creates an OkHttpClient with the needed defaults
 */
public final class OkHttpClientFactory {

    /**
     * Creates an {@link OkHttpClient} configured with the account configuration
     *
     * @param account the account
     * @return a configured {@link okhttp3.OkHttpClient.Builder}
     */
    public static OkHttpClient.Builder create(Account account) {
        CustomTrustManager customTrustManager = null;
        //Do we even need a custom trust manager?
        if (account.getTrustedCertificate() != null
                || account.getTrustedHostname() != null
                || account.getPrivateKeyAlias() != null) {
            customTrustManager = new CustomTrustManager();
            customTrustManager.setTrustedCertificate(account.getTrustedCertificate());
            customTrustManager.setTrustedHostname(account.getTrustedHostname());
            customTrustManager.setPrivateKeyAlias(account.getPrivateKeyAlias());
        }
        OpenSignInAuthenticator authenticator = new OpenSignInAuthenticator(account);
        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .authenticator(authenticator)
                .proxyAuthenticator(authenticator)
                .addInterceptor(new AuthenticationRequestInterceptor(account));
        //Only apply these custom things when needed, since they slow down the init
        if (customTrustManager != null && customTrustManager.getSSLSocketFactory() != null) {
            clientBuilder.sslSocketFactory(customTrustManager.getSSLSocketFactory(), X509TrustManagerProvider.get());
        }
        if (customTrustManager != null && customTrustManager.getHostnameVerifier() != null) {
            clientBuilder.hostnameVerifier(customTrustManager.getHostnameVerifier());
        }
        return clientBuilder;
    }
}

@@ -575,282 +575,161 @@
     
-CustomTrustManager customTrustManager = null;%0A        //Do we even need a custom trust manager?%0A        if (account.getTrustedCertificate() != null%0A                %7C%7C account.getTrustedHostname() != null%0A                %7C%7C account.getPrivateKeyAlias() != null) %7B%0A           
+//Do we even need a custom trust manager?%0A        // Yep. Otherwise SSL won't work properly with some configurations :) -Michi%0A        CustomTrustManager
  cus
@@ -772,20 +772,16 @@
 ager();%0A
-    
         
@@ -855,36 +855,32 @@
 ate());%0A        
- 
-   
 customTrustManag
@@ -928,28 +928,24 @@
 ostname());%0A
-    
         cust
@@ -1005,33 +1005,24 @@
 eyAlias());%0A
-        %7D
 %0A        Ope
@@ -1108,44 +1108,14 @@
     
-OkHttpClient.Builder clientBuilder =
+return
  new
@@ -1367,17 +1367,16 @@
 ccount))
-;
 %0A       
@@ -1380,203 +1380,16 @@
     
-//Only apply these custom things when needed, since they slow down the init%0A        if (customTrustManager != null && customTrustManager.getSSLSocketFactory() != null) %7B%0A            clientBuilder
+        
 .ssl
@@ -1475,25 +1475,24 @@
 r.get())
-;
 %0A       
  %7D%0A     
@@ -1483,27 +1483,24 @@
 %0A       
- %7D%0A
         
 if (cust
@@ -1495,119 +1495,9 @@
     
-if (customTrustManager != null && customTrustManager.getHostnameVerifier() != null) %7B%0A            clientBuilder
+ 
 .hos
@@ -1557,48 +1557,8 @@
 ));%0A
-        %7D%0A        return clientBuilder;%0A
     

Fix #84 - Problems with SSL