1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00
This commit is contained in:
David A. Velasco 2017-03-28 18:53:57 +02:00
parent 312b21cb1e
commit e4b57e8063
6 changed files with 57 additions and 40 deletions

View File

@ -29,6 +29,8 @@ import java.util.List;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.auth.AuthState;
import org.apache.commons.httpclient.auth.BasicScheme;
public class OwnCloudBasicCredentials implements OwnCloudCredentials {
@ -50,6 +52,8 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials {
@Override
public void applyTo(OwnCloudClient client) {
AuthPolicy.registerAuthScheme(AuthState.PREEMPTIVE_AUTH_SCHEME, BasicScheme.class);
List<String> authPrefs = new ArrayList<String>(1);
authPrefs.add(AuthPolicy.BASIC);
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

View File

@ -28,27 +28,31 @@ import java.util.List;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.auth.AuthState;
import com.owncloud.android.lib.common.network.BearerAuthScheme;
import com.owncloud.android.lib.common.network.BearerCredentials;
public class OwnCloudBearerCredentials implements OwnCloudCredentials {
private String mUsername;
private String mAccessToken;
public OwnCloudBearerCredentials(String accessToken) {
public OwnCloudBearerCredentials(String username, String accessToken) {
mUsername = username != null ? username : "";
mAccessToken = accessToken != null ? accessToken : "";
}
@Override
public void applyTo(OwnCloudClient client) {
AuthPolicy.registerAuthScheme(BearerAuthScheme.AUTH_POLICY, BearerAuthScheme.class);
AuthPolicy.registerAuthScheme(AuthState.PREEMPTIVE_AUTH_SCHEME, BearerAuthScheme.class);
List<String> authPrefs = new ArrayList<String>(1);
List<String> authPrefs = new ArrayList<>(1);
authPrefs.add(BearerAuthScheme.AUTH_POLICY);
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
client.getParams().setAuthenticationPreemptive(true);
client.getParams().setAuthenticationPreemptive(true); // true enforces BASIC AUTH ; library is stupid
client.getParams().setCredentialCharset(OwnCloudCredentialsFactory.CREDENTIAL_CHARSET);
client.getState().setCredentials(
AuthScope.ANY,
@ -58,8 +62,8 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials {
@Override
public String getUsername() {
// its unknown
return null;
// not relevant for authentication, but relevant for informational purposes
return mUsername;
}
@Override

View File

@ -96,7 +96,7 @@ public class OwnCloudClientFactory {
false);
client.setCredentials(
OwnCloudCredentialsFactory.newBearerCredentials(accessToken)
OwnCloudCredentialsFactory.newBearerCredentials(username, accessToken)
);
} else if (isSamlSso) { // TODO avoid a call to getUserData here
@ -161,7 +161,7 @@ public class OwnCloudClientFactory {
String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
if (accessToken == null) throw new AuthenticatorException("WTF!");
client.setCredentials(
OwnCloudCredentialsFactory.newBearerCredentials(accessToken)
OwnCloudCredentialsFactory.newBearerCredentials(username, accessToken)
);
} else if (isSamlSso) { // TODO avoid a call to getUserData here

View File

@ -40,8 +40,8 @@ public class OwnCloudCredentialsFactory {
return new OwnCloudBasicCredentials(username, password, preemptiveMode);
}
public static OwnCloudCredentials newBearerCredentials(String authToken) {
return new OwnCloudBearerCredentials(authToken);
public static OwnCloudCredentials newBearerCredentials(String username, String authToken) {
return new OwnCloudBearerCredentials(username, authToken);
}
public static OwnCloudCredentials newSamlSsoCredentials(String username, String sessionCookie) {

View File

@ -48,6 +48,7 @@ public class AccountUtils {
private static final String TAG = AccountUtils.class.getSimpleName();
public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";
public static final String ODAV_PATH = "/remote.php/webdav";
public static final String STATUS_PATH = "/status.php";
/**
@ -171,7 +172,7 @@ public class AccountUtils {
AccountTypeUtils.getAuthTokenTypeAccessToken(account.type),
false);
credentials = OwnCloudCredentialsFactory.newBearerCredentials(accessToken);
credentials = OwnCloudCredentialsFactory.newBearerCredentials(username, accessToken);
} else if (isSamlSso) {
String accessToken = am.blockingGetAuthToken(

View File

@ -26,6 +26,7 @@ package com.owncloud.android.lib.common.network;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.auth.AuthChallengeParser;
@ -33,6 +34,7 @@ import org.apache.commons.httpclient.auth.AuthScheme;
import org.apache.commons.httpclient.auth.AuthenticationException;
import org.apache.commons.httpclient.auth.InvalidCredentialsException;
import org.apache.commons.httpclient.auth.MalformedChallengeException;
import org.apache.commons.httpclient.util.EncodingUtil;
import com.owncloud.android.lib.common.utils.Log_OC;
@ -219,8 +221,14 @@ public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
StringBuffer buffer = new StringBuffer();
buffer.append(credentials.getAccessToken());
//return "Bearer " + EncodingUtil.getAsciiString(EncodingUtil.getBytes(buffer.toString(), charset));
Log_OC.v(TAG, "OAUTH2: string to authorize: " + "Bearer " + buffer.toString());
return "Bearer " + buffer.toString();
//return "Bearer " + EncodingUtil.getAsciiString(EncodingUtil.getBytes(buffer.toString(), charset));
/*return "Bearer " + EncodingUtil.getAsciiString(
Base64.encodeBase64(
EncodingUtil.getBytes(buffer.toString(), charset)
)
);*/
}
/**