1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

replace old cookies but don't delete them

This commit is contained in:
Christian Schabesberger 2020-09-22 16:38:28 +02:00 committed by Abel García de Prada
parent 9f6244147b
commit edcbddfd25
5 changed files with 101 additions and 61 deletions

View File

@ -27,6 +27,8 @@ package com.owncloud.android.lib.common;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation;
public class OwnCloudClientFactory { public class OwnCloudClientFactory {
/** /**
@ -43,7 +45,13 @@ public class OwnCloudClientFactory {
client.setFollowRedirects(followRedirects); client.setFollowRedirects(followRedirects);
client.setContext(context); client.setContext(context);
retriveCookisFromMiddleware(client);
return client; return client;
} }
public static void retriveCookisFromMiddleware(OwnCloudClient client) {
final GetRemoteStatusOperation statusOperation = new GetRemoteStatusOperation();
statusOperation.run(client);
}
} }

View File

@ -0,0 +1,40 @@
package com.owncloud.android.lib.common.http
import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
class CookieJarImpl(
private val sCookieStore: HashMap<String, List<Cookie>>
) : CookieJar {
private fun containsCookieWithName(cookies: List<Cookie>, name: String): Boolean {
for (cookie: Cookie in cookies) {
if (cookie.name == name) {
return true;
}
}
return false;
}
private fun getUpdatedCookies(oldCookies: List<Cookie>, newCookies: List<Cookie>): List<Cookie> {
val updatedList = ArrayList<Cookie>(newCookies);
for (oldCookie: Cookie in oldCookies) {
if (!containsCookieWithName(updatedList, oldCookie.name)) {
updatedList.add(oldCookie);
}
}
return updatedList;
}
override fun saveFromResponse(url: HttpUrl, newCookies: List<Cookie>) {
// Avoid duplicated cookies but update
val currentCookies: List<Cookie> = sCookieStore[url.host] ?: ArrayList()
val updatedCookies: List<Cookie> = getUpdatedCookies(currentCookies, newCookies);
sCookieStore.put(url.host, updatedCookies);
}
override fun loadForRequest(url: HttpUrl) =
sCookieStore[url.host] ?: ArrayList()
}

View File

@ -39,13 +39,11 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -53,6 +51,7 @@ import java.util.concurrent.TimeUnit;
* *
* @author David González Verdugo * @author David González Verdugo
*/ */
public class HttpClient { public class HttpClient {
private static OkHttpClient sOkHttpClient; private static OkHttpClient sOkHttpClient;
private static Context sContext; private static Context sContext;
@ -64,53 +63,52 @@ public class HttpClient {
try { try {
final X509TrustManager trustManager = new AdvancedX509TrustManager( final X509TrustManager trustManager = new AdvancedX509TrustManager(
NetworkUtils.getKnownServersStore(sContext)); NetworkUtils.getKnownServersStore(sContext));
final SSLSocketFactory sslSocketFactory = getNewSslSocketFactory(trustManager);
// Automatic cookie handling, NOT PERSISTENT
final CookieJar cookieJar = new CookieJarImpl(sCookieStore);
SSLContext sslContext; // TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok.
//.hostnameVerifier(new BrowserCompatHostnameVerifier());
sOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar);
} catch (Exception e) {
Timber.e(e, "Could not setup SSL system.");
}
}
return sOkHttpClient;
}
private static SSLContext getSslContext() throws NoSuchAlgorithmException {
try { try {
sslContext = SSLContext.getInstance("TLSv1.3"); return SSLContext.getInstance("TLSv1.3");
} catch (NoSuchAlgorithmException tlsv13Exception) { } catch (NoSuchAlgorithmException tlsv13Exception) {
try { try {
Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2"); Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2");
sslContext = SSLContext.getInstance("TLSv1.2"); return SSLContext.getInstance("TLSv1.2");
} catch (NoSuchAlgorithmException tlsv12Exception) { } catch (NoSuchAlgorithmException tlsv12Exception) {
try { try {
Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1"); Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1");
sslContext = SSLContext.getInstance("TLSv1.1"); return SSLContext.getInstance("TLSv1.1");
} catch (NoSuchAlgorithmException tlsv11Exception) { } catch (NoSuchAlgorithmException tlsv11Exception) {
Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0"); Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0");
sslContext = SSLContext.getInstance("TLSv1"); return SSLContext.getInstance("TLSv1");
// should be available in any device; see reference of supported protocols in // should be available in any device; see reference of supported protocols in
// http://developer.android.com/reference/javax/net/ssl/SSLSocket.html // http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
} }
} }
} }
}
private static SSLSocketFactory getNewSslSocketFactory(X509TrustManager trustManager)
throws NoSuchAlgorithmException, KeyManagementException {
final SSLContext sslContext = getSslContext();
sslContext.init(null, new TrustManager[]{trustManager}, null); sslContext.init(null, new TrustManager[]{trustManager}, null);
return sslContext.getSocketFactory();
SSLSocketFactory sslSocketFactory;
sslSocketFactory = sslContext.getSocketFactory();
// Automatic cookie handling, NOT PERSISTENT
CookieJar cookieJar = new CookieJar() {
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
// Avoid duplicated cookies
Set<Cookie> nonDuplicatedCookiesSet = new HashSet<>(cookies);
List<Cookie> nonDuplicatedCookiesList = new ArrayList<>(nonDuplicatedCookiesSet);
sCookieStore.put(url.host(), nonDuplicatedCookiesList);
} }
@Override private static OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager,
public List<Cookie> loadForRequest(HttpUrl url) { CookieJar cookieJar) {
List<Cookie> cookies = sCookieStore.get(url.host()); return new OkHttpClient.Builder()
return cookies != null ? cookies : new ArrayList<>();
}
};
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.addNetworkInterceptor(getLogInterceptor()) .addNetworkInterceptor(getLogInterceptor())
.protocols(Arrays.asList(Protocol.HTTP_1_1)) .protocols(Arrays.asList(Protocol.HTTP_1_1))
.readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS) .readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
@ -119,17 +117,8 @@ public class HttpClient {
.followRedirects(false) .followRedirects(false)
.sslSocketFactory(sslSocketFactory, trustManager) .sslSocketFactory(sslSocketFactory, trustManager)
.hostnameVerifier((asdf, usdf) -> true) .hostnameVerifier((asdf, usdf) -> true)
.cookieJar(cookieJar); .cookieJar(cookieJar)
// TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok. .build();
//.hostnameVerifier(new BrowserCompatHostnameVerifier());
sOkHttpClient = clientBuilder.build();
} catch (Exception e) {
Timber.e(e, "Could not setup SSL system.");
}
}
return sOkHttpClient;
} }
public Context getContext() { public Context getContext() {

View File

@ -45,7 +45,7 @@ import timber.log.Timber
*/ */
class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() { class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> { public override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
client.baseUri = buildFullHttpsUrl(client.baseUri) client.baseUri = buildFullHttpsUrl(client.baseUri)
var result = tryToConnect(client) var result = tryToConnect(client)

View File

@ -21,6 +21,7 @@ package com.owncloud.android.lib.resources.status.services.implementation
import android.net.Uri import android.net.Uri
import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.common.OwnCloudClientFactory
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory.getAnonymousCredentials import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory.getAnonymousCredentials
import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.resources.status.services.ServerInfoService import com.owncloud.android.lib.resources.status.services.ServerInfoService
@ -39,6 +40,8 @@ class OCServerInfoService : ServerInfoService {
GetRemoteStatusOperation().execute(createClientFromPath(path)) GetRemoteStatusOperation().execute(createClientFromPath(path))
private fun createClientFromPath(path: String): OwnCloudClient { private fun createClientFromPath(path: String): OwnCloudClient {
return OwnCloudClient(Uri.parse(path)).apply { credentials = getAnonymousCredentials() } val client = OwnCloudClient(Uri.parse(path)).apply { credentials = getAnonymousCredentials() }
OwnCloudClientFactory.retriveCookisFromMiddleware(client)
return client
} }
} }