Skip to content

Commit 64bf4b8

Browse files
authored
Merge pull request #65 from nextcloud/bruteforce
Bruteforce
2 parents bd2a8a0 + 80671c7 commit 64bf4b8

11 files changed

+198
-86
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.owncloud.android.lib.common;
2+
3+
import android.accounts.AuthenticatorException;
4+
import android.accounts.OperationCanceledException;
5+
import android.content.Context;
6+
7+
import com.owncloud.android.lib.common.accounts.AccountUtils;
8+
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
9+
10+
import java.io.IOException;
11+
12+
/**
13+
* Dynamic implementation of {@link OwnCloudClientManager}.
14+
* <p>
15+
* Wraps instances of {@link SingleSessionManager} and {@link SimpleFactoryManager} and delegates on one
16+
* or the other depending on the known version of the server corresponding to the {@link OwnCloudAccount}
17+
*
18+
* @author David A. Velasco
19+
*/
20+
21+
public class DynamicSessionManager implements OwnCloudClientManager {
22+
23+
private SimpleFactoryManager mSimpleFactoryManager = new SimpleFactoryManager();
24+
25+
private SingleSessionManager mSingleSessionManager = new SingleSessionManager();
26+
27+
@Override
28+
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
29+
throws AccountUtils.AccountNotFoundException,
30+
OperationCanceledException, AuthenticatorException, IOException {
31+
32+
OwnCloudVersion ownCloudVersion = null;
33+
if (account.getSavedAccount() != null) {
34+
ownCloudVersion = AccountUtils.getServerVersionForAccount(
35+
account.getSavedAccount(), context
36+
);
37+
}
38+
39+
if (ownCloudVersion != null && ownCloudVersion.isPreemptiveAuthenticationPreferred()) {
40+
return mSingleSessionManager.getClientFor(account, context);
41+
} else {
42+
return mSimpleFactoryManager.getClientFor(account, context);
43+
}
44+
}
45+
46+
@Override
47+
public OwnCloudClient removeClientFor(OwnCloudAccount account) {
48+
OwnCloudClient clientRemovedFromFactoryManager = mSimpleFactoryManager.removeClientFor(account);
49+
OwnCloudClient clientRemovedFromSingleSessionManager = mSingleSessionManager.removeClientFor(account);
50+
if (clientRemovedFromSingleSessionManager != null) {
51+
return clientRemovedFromSingleSessionManager;
52+
} else {
53+
return clientRemovedFromFactoryManager;
54+
}
55+
// clientRemoved and clientRemoved2 should not be != null at the same time
56+
}
57+
58+
@Override
59+
public void saveAllClients(Context context, String accountType)
60+
throws AccountUtils.AccountNotFoundException,
61+
AuthenticatorException, IOException, OperationCanceledException {
62+
mSimpleFactoryManager.saveAllClients(context, accountType);
63+
mSingleSessionManager.saveAllClients(context, accountType);
64+
}
65+
66+
}

src/com/owncloud/android/lib/common/OwnCloudAccount.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@
2525
package com.owncloud.android.lib.common;
2626

2727

28-
import java.io.IOException;
29-
30-
import com.owncloud.android.lib.common.accounts.AccountUtils;
31-
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
32-
3328
import android.accounts.Account;
3429
import android.accounts.AccountManager;
3530
import android.accounts.AuthenticatorException;
3631
import android.accounts.OperationCanceledException;
3732
import android.content.Context;
3833
import android.net.Uri;
3934

35+
import com.owncloud.android.lib.common.accounts.AccountUtils;
36+
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
37+
38+
import java.io.IOException;
39+
4040
/**
4141
* OwnCloud Account
4242
*
@@ -139,6 +139,10 @@ public String getName() {
139139
return mSavedAccountName;
140140
}
141141

142+
public Account getSavedAccount() {
143+
return mSavedAccount;
144+
}
145+
142146
public String getDisplayName() {
143147
if (mDisplayName != null && mDisplayName.length() > 0) {
144148
return mDisplayName;

src/com/owncloud/android/lib/common/OwnCloudBasicCredentials.java

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,55 @@
2323
*/
2424
package com.owncloud.android.lib.common;
2525

26-
import java.util.ArrayList;
27-
import java.util.List;
28-
2926
import org.apache.commons.httpclient.UsernamePasswordCredentials;
3027
import org.apache.commons.httpclient.auth.AuthPolicy;
3128
import org.apache.commons.httpclient.auth.AuthScope;
3229

30+
import java.util.ArrayList;
31+
import java.util.List;
32+
3333
public class OwnCloudBasicCredentials implements OwnCloudCredentials {
3434

35-
private String mUsername;
36-
private String mPassword;
35+
private String mUsername;
36+
private String mPassword;
37+
private boolean mAuthenticationPreemptive;
3738

38-
public OwnCloudBasicCredentials(String username, String password) {
39-
mUsername = username != null ? username : "";
40-
mPassword = password != null ? password : "";
41-
}
39+
public OwnCloudBasicCredentials(String username, String password) {
40+
mUsername = username != null ? username : "";
41+
mPassword = password != null ? password : "";
42+
mAuthenticationPreemptive = true;
43+
}
4244

43-
@Override
44-
public void applyTo(OwnCloudClient client) {
45+
public OwnCloudBasicCredentials(String username, String password, boolean preemptiveMode) {
46+
mUsername = username != null ? username : "";
47+
mPassword = password != null ? password : "";
48+
mAuthenticationPreemptive = preemptiveMode;
49+
}
50+
51+
@Override
52+
public void applyTo(OwnCloudClient client) {
4553
List<String> authPrefs = new ArrayList<String>(1);
4654
authPrefs.add(AuthPolicy.BASIC);
47-
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
48-
49-
client.getParams().setAuthenticationPreemptive(true);
55+
56+
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
57+
client.getParams().setAuthenticationPreemptive(mAuthenticationPreemptive);
5058
client.getParams().setCredentialCharset(OwnCloudCredentialsFactory.CREDENTIAL_CHARSET);
51-
client.getState().setCredentials(
52-
AuthScope.ANY,
53-
new UsernamePasswordCredentials(mUsername, mPassword)
54-
);
55-
}
59+
client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(mUsername, mPassword));
60+
}
5661

57-
@Override
58-
public String getUsername() {
59-
return mUsername;
60-
}
62+
@Override
63+
public String getUsername() {
64+
return mUsername;
65+
}
6166

62-
@Override
63-
public String getAuthToken() {
64-
return mPassword;
65-
}
67+
@Override
68+
public String getAuthToken() {
69+
return mPassword;
70+
}
6671

67-
@Override
68-
public boolean authTokenExpires() {
69-
return false;
70-
}
72+
@Override
73+
public boolean authTokenExpires() {
74+
return false;
75+
}
7176

7277
}

src/com/owncloud/android/lib/common/OwnCloudClient.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.apache.commons.httpclient.HostConfiguration;
4040
import org.apache.commons.httpclient.HttpClient;
4141
import org.apache.commons.httpclient.HttpConnectionManager;
42-
import org.apache.commons.httpclient.HttpException;
4342
import org.apache.commons.httpclient.HttpMethod;
4443
import org.apache.commons.httpclient.HttpMethodBase;
4544
import org.apache.commons.httpclient.HttpStatus;
@@ -149,7 +148,7 @@ public void clearCredentials() {
149148
* @throws Exception When the existence could not be determined
150149
*/
151150
@Deprecated
152-
public boolean existsFile(String path) throws IOException, HttpException {
151+
public boolean existsFile(String path) throws IOException {
153152
HeadMethod head = new HeadMethod(getWebdavUri() + WebdavUtils.encodePath(path));
154153
try {
155154
int status = executeMethod(head);
@@ -387,17 +386,17 @@ private void logCookiesAtRequest(Header[] headers, String when) {
387386
}
388387
}
389388
if (counter == 0) {
390-
Log_OC.d(TAG + " #" + mInstanceNumber, "No cookie at request before");
389+
Log_OC.d(TAG + " #" + mInstanceNumber, "No cookie at request (" + when + ")");
391390
}
392391
}
393392

394-
private void logCookiesAtState(String string) {
393+
private void logCookiesAtState(String when) {
395394
Cookie[] cookies = getState().getCookies();
396395
if (cookies.length == 0) {
397-
Log_OC.d(TAG + " #" + mInstanceNumber, "No cookie at STATE before");
396+
Log_OC.d(TAG + " #" + mInstanceNumber, "No cookie at STATE " + when);
398397
} else {
399-
Log_OC.d(TAG + " #" + mInstanceNumber, "Cookies at STATE (before)");
400-
for (int i=0; i<cookies.length; i++) {
398+
Log_OC.d(TAG + " #" + mInstanceNumber, "Cookies at STATE (" + when + ")");
399+
for (int i=0; i<cookies.length; i++) {
401400
Log_OC.d(TAG + " #" + mInstanceNumber, " (" + i + "):" +
402401
"\n name: " + cookies[i].getName() +
403402
"\n value: " + cookies[i].getValue() +

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424

2525
package com.owncloud.android.lib.common;
2626

27-
import java.io.IOException;
28-
import java.security.GeneralSecurityException;
29-
3027
import android.accounts.Account;
3128
import android.accounts.AccountManager;
3229
import android.accounts.AccountManagerFuture;
@@ -42,6 +39,10 @@
4239
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
4340
import com.owncloud.android.lib.common.network.NetworkUtils;
4441
import com.owncloud.android.lib.common.utils.Log_OC;
42+
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
43+
44+
import java.io.IOException;
45+
import java.security.GeneralSecurityException;
4546

4647
public class OwnCloudClientFactory {
4748

@@ -105,16 +106,17 @@ public static OwnCloudClient createOwnCloudClient (Account account, Context appC
105106
);
106107

107108
} else {
108-
//String password = am.getPassword(account);
109109
String password = am.blockingGetAuthToken(
110110
account,
111111
AccountTypeUtils.getAuthTokenTypePass(account.type),
112112
false);
113-
114-
client.setCredentials(
115-
OwnCloudCredentialsFactory.newBasicCredentials(username, password)
116-
);
117-
113+
114+
OwnCloudVersion version = AccountUtils.getServerVersionForAccount(account, appContext);
115+
116+
OwnCloudCredentials credentials = OwnCloudCredentialsFactory.newBasicCredentials(
117+
username, password, (version != null && version.isPreemptiveAuthenticationPreferred()));
118+
119+
client.setCredentials(credentials);
118120
}
119121

120122
// Restore cookies
@@ -185,9 +187,12 @@ public static OwnCloudClient createOwnCloudClient (Account account, Context appC
185187

186188
Bundle result = future.getResult();
187189
String password = result.getString(AccountManager.KEY_AUTHTOKEN);
188-
client.setCredentials(
189-
OwnCloudCredentialsFactory.newBasicCredentials(username, password)
190-
);
190+
191+
OwnCloudVersion version = AccountUtils.getServerVersionForAccount(account, appContext);
192+
OwnCloudCredentials credentials = OwnCloudCredentialsFactory.newBasicCredentials(
193+
username, password, (version != null && version.isPreemptiveAuthenticationPreferred()));
194+
195+
client.setCredentials(credentials);
191196
}
192197

193198
// Restore cookies

src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
package com.owncloud.android.lib.common;
2525

2626
public class OwnCloudClientManagerFactory {
27-
28-
public static enum Policy {
29-
ALWAYS_NEW_CLIENT,
30-
SINGLE_SESSION_PER_ACCOUNT
31-
}
27+
28+
public enum Policy {
29+
ALWAYS_NEW_CLIENT,
30+
SINGLE_SESSION_PER_ACCOUNT,
31+
SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING
32+
}
3233

3334
private static Policy sDefaultPolicy = Policy.ALWAYS_NEW_CLIENT;
3435

@@ -47,8 +48,11 @@ public static OwnCloudClientManager newOwnCloudClientManager(Policy policy) {
4748

4849
case SINGLE_SESSION_PER_ACCOUNT:
4950
return new SingleSessionManager();
50-
51-
default:
51+
52+
case SINGLE_SESSION_PER_ACCOUNT_IF_SERVER_SUPPORTS_SERVER_MONITORING:
53+
return new DynamicSessionManager();
54+
55+
default:
5256
throw new IllegalArgumentException("Unknown policy");
5357
}
5458
}
@@ -90,11 +94,8 @@ private static boolean defaultSingletonMustBeUpdated(Policy policy) {
9094
!(sDefaultSingleton instanceof SimpleFactoryManager)) {
9195
return true;
9296
}
93-
if (policy == Policy.SINGLE_SESSION_PER_ACCOUNT &&
94-
!(sDefaultSingleton instanceof SingleSessionManager)) {
95-
return true;
96-
}
97-
return false;
98-
}
97+
return policy == Policy.SINGLE_SESSION_PER_ACCOUNT &&
98+
!(sDefaultSingleton instanceof SingleSessionManager);
99+
}
99100

100101
}

src/com/owncloud/android/lib/common/OwnCloudCredentialsFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ public class OwnCloudCredentialsFactory {
3333
public static OwnCloudCredentials newBasicCredentials(String username, String password) {
3434
return new OwnCloudBasicCredentials(username, password);
3535
}
36-
37-
public static OwnCloudCredentials newBearerCredentials(String authToken) {
36+
37+
public static OwnCloudCredentials newBasicCredentials(String username, String password, boolean preemptiveMode) {
38+
return new OwnCloudBasicCredentials(username, password, preemptiveMode);
39+
}
40+
41+
public static OwnCloudCredentials newBearerCredentials(String authToken) {
3842
return new OwnCloudBearerCredentials(authToken);
3943
}
4044

0 commit comments

Comments
 (0)