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

View File

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

View File

@ -96,7 +96,7 @@ public class OwnCloudClientFactory {
false); false);
client.setCredentials( client.setCredentials(
OwnCloudCredentialsFactory.newBearerCredentials(accessToken) OwnCloudCredentialsFactory.newBearerCredentials(username, accessToken)
); );
} else if (isSamlSso) { // TODO avoid a call to getUserData here } else if (isSamlSso) { // TODO avoid a call to getUserData here
@ -161,7 +161,7 @@ public class OwnCloudClientFactory {
String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN); String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
if (accessToken == null) throw new AuthenticatorException("WTF!"); if (accessToken == null) throw new AuthenticatorException("WTF!");
client.setCredentials( client.setCredentials(
OwnCloudCredentialsFactory.newBearerCredentials(accessToken) OwnCloudCredentialsFactory.newBearerCredentials(username, accessToken)
); );
} else if (isSamlSso) { // TODO avoid a call to getUserData here } 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); return new OwnCloudBasicCredentials(username, password, preemptiveMode);
} }
public static OwnCloudCredentials newBearerCredentials(String authToken) { public static OwnCloudCredentials newBearerCredentials(String username, String authToken) {
return new OwnCloudBearerCredentials(authToken); return new OwnCloudBearerCredentials(username, authToken);
} }
public static OwnCloudCredentials newSamlSsoCredentials(String username, String sessionCookie) { 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(); private static final String TAG = AccountUtils.class.getSimpleName();
public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav"; 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"; public static final String STATUS_PATH = "/status.php";
/** /**
@ -171,7 +172,7 @@ public class AccountUtils {
AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), AccountTypeUtils.getAuthTokenTypeAccessToken(account.type),
false); false);
credentials = OwnCloudCredentialsFactory.newBearerCredentials(accessToken); credentials = OwnCloudCredentialsFactory.newBearerCredentials(username, accessToken);
} else if (isSamlSso) { } else if (isSamlSso) {
String accessToken = am.blockingGetAuthToken( String accessToken = am.blockingGetAuthToken(

View File

@ -26,6 +26,7 @@ package com.owncloud.android.lib.common.network;
import java.util.Map; import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.auth.AuthChallengeParser; 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.AuthenticationException;
import org.apache.commons.httpclient.auth.InvalidCredentialsException; import org.apache.commons.httpclient.auth.InvalidCredentialsException;
import org.apache.commons.httpclient.auth.MalformedChallengeException; import org.apache.commons.httpclient.auth.MalformedChallengeException;
import org.apache.commons.httpclient.util.EncodingUtil;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
@ -218,9 +220,15 @@ public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
} }
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
buffer.append(credentials.getAccessToken()); 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 " + buffer.toString();
//return "Bearer " + EncodingUtil.getAsciiString(EncodingUtil.getBytes(buffer.toString(), charset));
/*return "Bearer " + EncodingUtil.getAsciiString(
Base64.encodeBase64(
EncodingUtil.getBytes(buffer.toString(), charset)
)
);*/
} }
/** /**