Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit c3189b7

Browse files
theScrabiabelgardep
authored andcommitted
replace old cookies but don't delete them
1 parent c54f1c9 commit c3189b7

File tree

5 files changed

+104
-61
lines changed

5 files changed

+104
-61
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import android.content.Context;
2828
import android.net.Uri;
2929

30+
import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation;
31+
3032
public class OwnCloudClientFactory {
3133

3234
/**
@@ -43,7 +45,13 @@ public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, bool
4345
client.setFollowRedirects(followRedirects);
4446

4547
client.setContext(context);
48+
retriveCookisFromMiddleware(client);
4649

4750
return client;
4851
}
52+
53+
public static void retriveCookisFromMiddleware(OwnCloudClient client) {
54+
final GetRemoteStatusOperation statusOperation = new GetRemoteStatusOperation();
55+
statusOperation.run(client);
56+
}
4957
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.owncloud.android.lib.common.http
2+
3+
import okhttp3.Cookie
4+
import okhttp3.CookieJar
5+
import okhttp3.HttpUrl
6+
7+
class CookieJarImpl(
8+
private val sCookieStore: HashMap<String, List<Cookie>>
9+
) : CookieJar {
10+
11+
private fun containsCookieWithName(cookies: List<Cookie>, name: String): Boolean {
12+
for (cookie: Cookie in cookies) {
13+
if (cookie.name == name) {
14+
return true;
15+
}
16+
}
17+
return false;
18+
}
19+
20+
private fun getUpdatedCookies(oldCookies: List<Cookie>, newCookies: List<Cookie>): List<Cookie> {
21+
val updatedList = ArrayList<Cookie>(newCookies);
22+
for (oldCookie: Cookie in oldCookies) {
23+
if (!containsCookieWithName(updatedList, oldCookie.name)) {
24+
updatedList.add(oldCookie);
25+
}
26+
}
27+
return updatedList;
28+
}
29+
30+
override fun saveFromResponse(url: HttpUrl, newCookies: List<Cookie>) {
31+
// Avoid duplicated cookies but update
32+
val currentCookies: List<Cookie> = sCookieStore[url.host] ?: ArrayList()
33+
val updatedCookies: List<Cookie> = getUpdatedCookies(currentCookies, newCookies);
34+
sCookieStore.put(url.host, updatedCookies);
35+
}
36+
37+
override fun loadForRequest(url: HttpUrl) =
38+
sCookieStore[url.host] ?: ArrayList()
39+
40+
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,19 @@
3939
import javax.net.ssl.SSLSocketFactory;
4040
import javax.net.ssl.TrustManager;
4141
import javax.net.ssl.X509TrustManager;
42+
import java.security.KeyManagementException;
4243
import java.security.NoSuchAlgorithmException;
43-
import java.util.ArrayList;
4444
import java.util.Arrays;
4545
import java.util.HashMap;
46-
import java.util.HashSet;
4746
import java.util.List;
48-
import java.util.Set;
4947
import java.util.concurrent.TimeUnit;
5048

5149
/**
5250
* Client used to perform network operations
5351
*
5452
* @author David González Verdugo
5553
*/
54+
5655
public class HttpClient {
5756
private static OkHttpClient sOkHttpClient;
5857
private static Context sContext;
@@ -64,66 +63,13 @@ public static OkHttpClient getOkHttpClient() {
6463
try {
6564
final X509TrustManager trustManager = new AdvancedX509TrustManager(
6665
NetworkUtils.getKnownServersStore(sContext));
67-
68-
SSLContext sslContext;
69-
70-
try {
71-
sslContext = SSLContext.getInstance("TLSv1.3");
72-
} catch (NoSuchAlgorithmException tlsv13Exception) {
73-
try {
74-
Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2");
75-
sslContext = SSLContext.getInstance("TLSv1.2");
76-
} catch (NoSuchAlgorithmException tlsv12Exception) {
77-
try {
78-
Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1");
79-
sslContext = SSLContext.getInstance("TLSv1.1");
80-
} catch (NoSuchAlgorithmException tlsv11Exception) {
81-
Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0");
82-
sslContext = SSLContext.getInstance("TLSv1");
83-
// should be available in any device; see reference of supported protocols in
84-
// http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
85-
}
86-
}
87-
}
88-
89-
sslContext.init(null, new TrustManager[]{trustManager}, null);
90-
91-
SSLSocketFactory sslSocketFactory;
92-
93-
sslSocketFactory = sslContext.getSocketFactory();
94-
66+
final SSLSocketFactory sslSocketFactory = getNewSslSocketFactory(trustManager);
9567
// Automatic cookie handling, NOT PERSISTENT
96-
CookieJar cookieJar = new CookieJar() {
97-
@Override
98-
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
99-
// Avoid duplicated cookies
100-
Set<Cookie> nonDuplicatedCookiesSet = new HashSet<>(cookies);
101-
List<Cookie> nonDuplicatedCookiesList = new ArrayList<>(nonDuplicatedCookiesSet);
102-
103-
sCookieStore.put(url.host(), nonDuplicatedCookiesList);
104-
}
105-
106-
@Override
107-
public List<Cookie> loadForRequest(HttpUrl url) {
108-
List<Cookie> cookies = sCookieStore.get(url.host());
109-
return cookies != null ? cookies : new ArrayList<>();
110-
}
111-
};
112-
113-
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
114-
.addNetworkInterceptor(getLogInterceptor())
115-
.protocols(Arrays.asList(Protocol.HTTP_1_1))
116-
.readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
117-
.writeTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
118-
.connectTimeout(HttpConstants.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
119-
.followRedirects(false)
120-
.sslSocketFactory(sslSocketFactory, trustManager)
121-
.hostnameVerifier((asdf, usdf) -> true)
122-
.cookieJar(cookieJar);
68+
final CookieJar cookieJar = new CookieJarImpl(sCookieStore);
69+
12370
// TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok.
12471
//.hostnameVerifier(new BrowserCompatHostnameVerifier());
125-
126-
sOkHttpClient = clientBuilder.build();
72+
sOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar);
12773

12874
} catch (Exception e) {
12975
Timber.e(e, "Could not setup SSL system.");
@@ -132,6 +78,49 @@ public List<Cookie> loadForRequest(HttpUrl url) {
13278
return sOkHttpClient;
13379
}
13480

81+
private static SSLContext getSslContext() throws NoSuchAlgorithmException {
82+
try {
83+
return SSLContext.getInstance("TLSv1.3");
84+
} catch (NoSuchAlgorithmException tlsv13Exception) {
85+
try {
86+
Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2");
87+
return SSLContext.getInstance("TLSv1.2");
88+
} catch (NoSuchAlgorithmException tlsv12Exception) {
89+
try {
90+
Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1");
91+
return SSLContext.getInstance("TLSv1.1");
92+
} catch (NoSuchAlgorithmException tlsv11Exception) {
93+
Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0");
94+
return SSLContext.getInstance("TLSv1");
95+
// should be available in any device; see reference of supported protocols in
96+
// http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
97+
}
98+
}
99+
}
100+
}
101+
102+
private static SSLSocketFactory getNewSslSocketFactory(X509TrustManager trustManager)
103+
throws NoSuchAlgorithmException, KeyManagementException {
104+
final SSLContext sslContext = getSslContext();
105+
sslContext.init(null, new TrustManager[]{trustManager}, null);
106+
return sslContext.getSocketFactory();
107+
}
108+
109+
private static OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager,
110+
CookieJar cookieJar) {
111+
return new OkHttpClient.Builder()
112+
.addNetworkInterceptor(getLogInterceptor())
113+
.protocols(Arrays.asList(Protocol.HTTP_1_1))
114+
.readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
115+
.writeTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
116+
.connectTimeout(HttpConstants.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
117+
.followRedirects(false)
118+
.sslSocketFactory(sslSocketFactory, trustManager)
119+
.hostnameVerifier((asdf, usdf) -> true)
120+
.cookieJar(cookieJar)
121+
.build();
122+
}
123+
135124
public Context getContext() {
136125
return sContext;
137126
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import timber.log.Timber
4545
*/
4646
class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
4747

48-
override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
48+
public override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
4949
client.baseUri = buildFullHttpsUrl(client.baseUri)
5050

5151
var result = tryToConnect(client)

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ class OCServerInfoService : ServerInfoService {
4343
client: OwnCloudClient
4444
): RemoteOperationResult<OwnCloudVersion> =
4545
GetRemoteStatusOperation().execute(client)
46+
47+
private fun createClientFromPath(path: String): OwnCloudClient {
48+
val client = OwnCloudClient(Uri.parse(path)).apply { credentials = getAnonymousCredentials() }
49+
OwnCloudClientFactory.retriveCookisFromMiddleware(client)
50+
return client
51+
}
4652
}

0 commit comments

Comments
 (0)