33 */
44package ai .deepcode .javaclient ;
55
6+ import javax .net .ssl .SSLContext ;
7+ import javax .net .ssl .SSLSocketFactory ;
8+ import javax .net .ssl .TrustManager ;
9+ import javax .net .ssl .X509TrustManager ;
10+
611import ai .deepcode .javaclient .requests .*;
712import ai .deepcode .javaclient .responses .*;
813
1621import retrofit2 .http .*;
1722
1823import java .io .IOException ;
24+ import java .security .KeyManagementException ;
25+ import java .security .NoSuchAlgorithmException ;
26+ import java .security .SecureRandom ;
27+ import java .security .cert .X509Certificate ;
1928import java .util .List ;
2029import java .util .concurrent .TimeUnit ;
2130
@@ -31,29 +40,64 @@ private DeepCodeRestApi() {}
3140
3241 private static final String API_URL = "https://www.deepcode.ai/" ;
3342
34- private static Retrofit retrofit = buildRetrofit (API_URL );
43+ private static Retrofit retrofit = buildRetrofit (API_URL , false );
3544
3645 // Create simple REST adapter which points the baseUrl.
37- private static Retrofit buildRetrofit (String baseUrl ) {
38- OkHttpClient client = new OkHttpClient .Builder ()
46+ private static Retrofit buildRetrofit (String baseUrl , boolean disableSslVerification ) {
47+ OkHttpClient . Builder builder = new OkHttpClient .Builder ()
3948 .connectTimeout (100 , TimeUnit .SECONDS )
4049 .writeTimeout (100 , TimeUnit .SECONDS )
41- .readTimeout (100 , TimeUnit .SECONDS ).build ();
50+ .readTimeout (100 , TimeUnit .SECONDS );
51+ if (disableSslVerification ) {
52+ X509TrustManager x509TrustManager = buildUnsafeTrustManager ();
53+ final TrustManager [] trustAllCertificates = new TrustManager []{ x509TrustManager };
54+
55+ try {
56+ SSLContext sslContext = SSLContext .getInstance ("SSL" );
57+ sslContext .init (null , trustAllCertificates , new SecureRandom ());
58+ SSLSocketFactory sslSocketFactory = sslContext .getSocketFactory ();
59+ builder .sslSocketFactory (sslSocketFactory , x509TrustManager );
60+ } catch (NoSuchAlgorithmException | KeyManagementException e ) {
61+ //TODO(pavel): extract Retrofit and OkHttpClient into configuration object to simplify API client building.
62+ e .printStackTrace ();
63+ }
64+ }
65+ OkHttpClient client = builder .build ();
4266 return new Retrofit .Builder ()
4367 .baseUrl (baseUrl + "publicapi/" )
44- .client (client )
68+ .client (client )
4569 .addConverterFactory (GsonConverterFactory .create ())
4670 .build ();
4771 }
4872
73+ @ NotNull
74+ private static X509TrustManager buildUnsafeTrustManager () {
75+ return new X509TrustManager () {
76+ @ Override
77+ public void checkClientTrusted (X509Certificate [] chain , String authType ) {}
78+
79+ @ Override
80+ public void checkServerTrusted (X509Certificate [] chain , String authType ) {}
81+
82+ @ Override
83+ public X509Certificate [] getAcceptedIssuers () {
84+ return new X509Certificate []{};
85+ }
86+ };
87+ }
88+
4989 /**
5090 * Re-set baseUrl for retrofit instance
5191 *
5292 * @param baseUrl new baseUrl. <b>Null</b> or empty "" value will reset to default {@code
5393 * #API_URL}
5494 */
5595 public static void setBaseUrl (@ Nullable String baseUrl ) {
56- retrofit = buildRetrofit ((baseUrl == null || baseUrl .isEmpty ()) ? API_URL : baseUrl );
96+ setBaseUrl (baseUrl , false );
97+ }
98+
99+ public static void setBaseUrl (@ Nullable String baseUrl , boolean disableSslVerification ) {
100+ retrofit = buildRetrofit ((baseUrl == null || baseUrl .isEmpty ()) ? API_URL : baseUrl , disableSslVerification );
57101 }
58102
59103 private interface LoginCall {
0 commit comments