mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	make https connection work with new library
This commit is contained in:
		
							parent
							
								
									45e4b98510
								
							
						
					
					
						commit
						c2bdea1c91
					
				| @ -35,6 +35,7 @@ import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory | |||||||
| import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory.OwnCloudAnonymousCredentials; | import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory.OwnCloudAnonymousCredentials; | ||||||
| import com.owncloud.android.lib.common.http.methods.HttpBaseMethod; | import com.owncloud.android.lib.common.http.methods.HttpBaseMethod; | ||||||
| import com.owncloud.android.lib.common.network.RedirectionPath; | import com.owncloud.android.lib.common.network.RedirectionPath; | ||||||
|  | import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||||
| import com.owncloud.android.lib.common.utils.Log_OC; | import com.owncloud.android.lib.common.utils.Log_OC; | ||||||
| import com.owncloud.android.lib.resources.status.OwnCloudVersion; | import com.owncloud.android.lib.resources.status.OwnCloudVersion; | ||||||
| 
 | 
 | ||||||
| @ -292,6 +293,8 @@ public class OwnCloudClient extends HttpClient { | |||||||
|         } while (repeatWithFreshCredentials); |         } while (repeatWithFreshCredentials); | ||||||
| 
 | 
 | ||||||
|         return status; |         return status; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private void checkFirstRedirection(HttpMethod method) { |     private void checkFirstRedirection(HttpMethod method) { | ||||||
|  | |||||||
| @ -24,12 +24,25 @@ | |||||||
| 
 | 
 | ||||||
| package com.owncloud.android.lib.common.http; | package com.owncloud.android.lib.common.http; | ||||||
| 
 | 
 | ||||||
|  | import android.content.Context; | ||||||
|  | 
 | ||||||
| import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; | import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; | ||||||
| import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor; | import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor; | ||||||
| import com.owncloud.android.lib.common.http.interceptors.UserAgentInterceptor; | import com.owncloud.android.lib.common.http.interceptors.UserAgentInterceptor; | ||||||
|  | import com.owncloud.android.lib.common.network.AdvancedX509TrustManager; | ||||||
|  | import com.owncloud.android.lib.common.network.NetworkUtils; | ||||||
|  | import com.owncloud.android.lib.common.utils.Log_OC; | ||||||
|  | 
 | ||||||
|  | import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier; | ||||||
| 
 | 
 | ||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| 
 | 
 | ||||||
|  | import javax.net.ssl.HostnameVerifier; | ||||||
|  | import javax.net.ssl.SSLContext; | ||||||
|  | import javax.net.ssl.SSLSession; | ||||||
|  | import javax.net.ssl.TrustManager; | ||||||
|  | import javax.net.ssl.X509TrustManager; | ||||||
|  | 
 | ||||||
| import okhttp3.OkHttpClient; | import okhttp3.OkHttpClient; | ||||||
| import okhttp3.Protocol; | import okhttp3.Protocol; | ||||||
| 
 | 
 | ||||||
| @ -38,17 +51,33 @@ import okhttp3.Protocol; | |||||||
|  * @author David González Verdugo |  * @author David González Verdugo | ||||||
|  */ |  */ | ||||||
| public class HttpClient { | public class HttpClient { | ||||||
|  |     private static final String TAG = HttpClient.class.toString(); | ||||||
| 
 | 
 | ||||||
|     private static OkHttpClient sOkHttpClient; |     private static OkHttpClient sOkHttpClient; | ||||||
|     private static HttpInterceptor sOkHttpInterceptor; |     private static HttpInterceptor sOkHttpInterceptor; | ||||||
|  |     private static Context sContext; | ||||||
|  | 
 | ||||||
|  |     public static void setContext(Context context) { | ||||||
|  |         sContext = context; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     public static OkHttpClient getOkHttpClient() { |     public static OkHttpClient getOkHttpClient() { | ||||||
|         if (sOkHttpClient == null) { |         if (sOkHttpClient == null) { | ||||||
|  |             try { | ||||||
|  |                 final X509TrustManager trustManager = new AdvancedX509TrustManager( | ||||||
|  |                         NetworkUtils.getKnownServersStore(sContext)); | ||||||
|  |                 final SSLContext sslContext = SSLContext.getInstance("TLS"); | ||||||
|  |                 sslContext.init(null, new TrustManager[] {trustManager}, null); | ||||||
|                 sOkHttpClient = new OkHttpClient.Builder() |                 sOkHttpClient = new OkHttpClient.Builder() | ||||||
|                         .addInterceptor(getOkHttpInterceptor()) |                         .addInterceptor(getOkHttpInterceptor()) | ||||||
|                         .protocols(Arrays.asList(Protocol.HTTP_1_1)) |                         .protocols(Arrays.asList(Protocol.HTTP_1_1)) | ||||||
|                         .followRedirects(false) |                         .followRedirects(false) | ||||||
|  |                         .sslSocketFactory(sslContext.getSocketFactory(), trustManager) | ||||||
|  |                         .hostnameVerifier(new BrowserCompatHostnameVerifier()) | ||||||
|                         .build(); |                         .build(); | ||||||
|  |             } catch (Exception e) { | ||||||
|  |                 Log_OC.e(TAG, "Could not setup SSL system.", e); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|         return sOkHttpClient; |         return sOkHttpClient; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -147,7 +147,7 @@ public class NetworkUtils { | |||||||
|      * @throws CertificateException         When an exception occurred while loading the certificates from the local  |      * @throws CertificateException         When an exception occurred while loading the certificates from the local  | ||||||
|      * 										trust store. |      * 										trust store. | ||||||
|      */ |      */ | ||||||
|     private static KeyStore getKnownServersStore(Context context)  |     public static KeyStore getKnownServersStore(Context context) | ||||||
|     		throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { |     		throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { | ||||||
|         if (mKnownServersStore == null) { |         if (mKnownServersStore == null) { | ||||||
|             //mKnownServersStore = KeyStore.getInstance("BKS"); |             //mKnownServersStore = KeyStore.getInstance("BKS"); | ||||||
|  | |||||||
| @ -53,10 +53,15 @@ import java.util.List; | |||||||
| import java.util.Map; | import java.util.Map; | ||||||
| 
 | 
 | ||||||
| import javax.net.ssl.SSLException; | import javax.net.ssl.SSLException; | ||||||
|  | import javax.net.ssl.SSLPeerUnverifiedException; | ||||||
| 
 | 
 | ||||||
| import okhttp3.Headers; | import okhttp3.Headers; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * The result of a remote operation required to an ownCloud server. |  * The result of a remote operation required to an ownCloud server. | ||||||
|  * |  * | ||||||
| @ -191,6 +196,9 @@ public class RemoteOperationResult implements Serializable { | |||||||
|             mCode = ResultCode.ACCOUNT_EXCEPTION; |             mCode = ResultCode.ACCOUNT_EXCEPTION; | ||||||
| 
 | 
 | ||||||
|         } else if (e instanceof SSLException || e instanceof RuntimeException) { |         } else if (e instanceof SSLException || e instanceof RuntimeException) { | ||||||
|  |             if(e instanceof SSLPeerUnverifiedException) { | ||||||
|  |                 mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED; | ||||||
|  |             } else { | ||||||
|                 CertificateCombinedException se = getCertificateCombinedException(e); |                 CertificateCombinedException se = getCertificateCombinedException(e); | ||||||
|                 if (se != null) { |                 if (se != null) { | ||||||
|                     mException = se; |                     mException = se; | ||||||
| @ -203,6 +211,7 @@ public class RemoteOperationResult implements Serializable { | |||||||
|                 } else { |                 } else { | ||||||
|                     mCode = ResultCode.SSL_ERROR; |                     mCode = ResultCode.SSL_ERROR; | ||||||
|                 } |                 } | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|         } else if (e instanceof FileNotFoundException) { |         } else if (e instanceof FileNotFoundException) { | ||||||
|             mCode = ResultCode.LOCAL_FILE_NOT_FOUND; |             mCode = ResultCode.LOCAL_FILE_NOT_FOUND; | ||||||
|  | |||||||
| @ -40,11 +40,15 @@ import com.owncloud.android.lib.common.utils.Log_OC; | |||||||
| import org.json.JSONException; | import org.json.JSONException; | ||||||
| import org.json.JSONObject; | import org.json.JSONObject; | ||||||
| 
 | 
 | ||||||
|  | import java.security.cert.CertPathValidatorException; | ||||||
| import java.sql.Time; | import java.sql.Time; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | ||||||
| 
 | 
 | ||||||
|  | import javax.net.ssl.SSLPeerUnverifiedException; | ||||||
|  | 
 | ||||||
| import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | ||||||
|  | import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Checks if the server is valid and if the server supports the Share API |  * Checks if the server is valid and if the server supports the Share API | ||||||
| @ -87,9 +91,15 @@ public class GetRemoteStatusOperation extends RemoteOperation { | |||||||
|             getMethod.setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS); |             getMethod.setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS); | ||||||
|             getMethod.setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS); |             getMethod.setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS); | ||||||
| 
 | 
 | ||||||
|             int status = client.executeHttpMethod(getMethod); |             int status; | ||||||
| 
 |             try { | ||||||
|  |                 status = client.executeHttpMethod(getMethod); | ||||||
|                 mLatestResult = new RemoteOperationResult(OK); |                 mLatestResult = new RemoteOperationResult(OK); | ||||||
|  |             } catch (SSLPeerUnverifiedException certEx) { | ||||||
|  |                 mLatestResult = new RemoteOperationResult(certEx); | ||||||
|  |                 return false; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
|             boolean isRedirectToNonSecureConnection = false; |             boolean isRedirectToNonSecureConnection = false; | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user