future = am.getAuthToken(
account,
AccountTypeUtils.getAuthTokenTypePass(account.type),
@@ -162,24 +149,12 @@ public class OwnCloudClientFactory {
*/
public static OwnCloudClient createOwnCloudClient(Uri uri, Context context,
boolean followRedirects) {
- try {
- NetworkUtils.registerAdvancedSslContext(true, context);
- } catch (GeneralSecurityException e) {
- Log_OC.e(TAG, "Advanced SSL Context could not be loaded. Default SSL management in" +
- " the system will be used for HTTPS connections", e);
-
- } catch (IOException e) {
- Log_OC.e(TAG, "The local server truststore could not be read. Default SSL management" +
- " in the system will be used for HTTPS connections", e);
- }
-
- OwnCloudClient client = new OwnCloudClient(uri, NetworkUtils.getMultiThreadedConnManager());
- client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
+ OwnCloudClient client = new OwnCloudClient(uri);
+
client.setFollowRedirects(followRedirects);
+
client.setContext(context);
return client;
}
-
-
-}
+}
\ No newline at end of file
diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java
index 856a11ba..fbaebc06 100644
--- a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java
+++ b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java
@@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license
- * Copyright (C) 2016 ownCloud GmbH.
+ * Copyright (C) 2018 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -32,24 +32,23 @@ import android.content.Context;
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
-
/**
* Manager to create and reuse OwnCloudClient instances to access remote OC servers.
*
* @author David A. Velasco
* @author masensio
+ * @author Christian Schabesberger
*/
public interface OwnCloudClientManager {
- public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
- throws AccountNotFoundException, OperationCanceledException, AuthenticatorException,
+ OwnCloudClient getClientFor(OwnCloudAccount account, Context context) throws AccountNotFoundException,
+ OperationCanceledException, AuthenticatorException,
IOException;
- public OwnCloudClient removeClientFor(OwnCloudAccount account);
+ OwnCloudClient removeClientFor(OwnCloudAccount account);
- public void saveAllClients(Context context, String accountType)
+ void saveAllClients(Context context, String accountType)
throws AccountNotFoundException, AuthenticatorException,
IOException, OperationCanceledException;
-
-}
+}
\ No newline at end of file
diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java b/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java
index fc675411..4fc6ea65 100644
--- a/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java
+++ b/src/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java
@@ -100,5 +100,4 @@ public class OwnCloudClientManagerFactory {
}
return false;
}
-
-}
+}
\ No newline at end of file
diff --git a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java
index b955c517..f7d9e983 100644
--- a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java
+++ b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java
@@ -40,9 +40,8 @@ public class SimpleFactoryManager implements OwnCloudClientManager {
private static final String TAG = SimpleFactoryManager.class.getSimpleName();
@Override
- public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
- throws AccountNotFoundException, OperationCanceledException, AuthenticatorException,
- IOException {
+ public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) throws
+ OperationCanceledException, AuthenticatorException, IOException {
Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : ");
diff --git a/src/com/owncloud/android/lib/common/SingleSessionManager.java b/src/com/owncloud/android/lib/common/SingleSessionManager.java
index 05396480..99642813 100644
--- a/src/com/owncloud/android/lib/common/SingleSessionManager.java
+++ b/src/com/owncloud/android/lib/common/SingleSessionManager.java
@@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license
- * Copyright (C) 2016 ownCloud GmbH.
+ * Copyright (C) 2018 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -24,14 +24,8 @@
package com.owncloud.android.lib.common;
-import java.io.IOException;
-import java.util.Iterator;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-
-import org.apache.commons.httpclient.cookie.CookiePolicy;
-
import android.accounts.Account;
+import android.accounts.AccountManager;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
@@ -39,17 +33,23 @@ import android.net.Uri;
import android.util.Log;
import com.owncloud.android.lib.common.accounts.AccountUtils;
-import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
-import com.owncloud.android.lib.common.authentication.OwnCloudCredentials;
+import com.owncloud.android.lib.common.authentication.OwnCloudSamlSsoCredentials;
import com.owncloud.android.lib.common.utils.Log_OC;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
/**
* Implementation of {@link OwnCloudClientManager}
- *
+ *
* TODO check multithreading safety
*
* @author David A. Velasco
* @author masensio
+ * @author Christian Schabesberger
+ * @author David González Verdugo
*/
public class SingleSessionManager implements OwnCloudClientManager {
@@ -57,16 +57,15 @@ public class SingleSessionManager implements OwnCloudClientManager {
private static final String TAG = SingleSessionManager.class.getSimpleName();
private ConcurrentMap mClientsWithKnownUsername =
- new ConcurrentHashMap();
+ new ConcurrentHashMap<>();
private ConcurrentMap mClientsWithUnknownUsername =
- new ConcurrentHashMap();
+ new ConcurrentHashMap<>();
@Override
- public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
- throws AccountNotFoundException, OperationCanceledException, AuthenticatorException,
- IOException {
+ public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) throws OperationCanceledException,
+ AuthenticatorException, IOException {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log_OC.d(TAG, "getClientFor starting ");
@@ -80,8 +79,7 @@ public class SingleSessionManager implements OwnCloudClientManager {
String sessionName = account.getCredentials() == null ? "" :
AccountUtils.buildAccountName(
account.getBaseUri(),
- account.getCredentials().getAuthToken()
- );
+ account.getCredentials().getAuthToken());
if (accountName != null) {
client = mClientsWithKnownUsername.get(accountName);
@@ -115,16 +113,17 @@ public class SingleSessionManager implements OwnCloudClientManager {
account.getBaseUri(),
context.getApplicationContext(),
true); // TODO remove dependency on OwnCloudClientFactory
- client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.setAccount(account);
client.setContext(context);
client.setOwnCloudClientManager(this);
- // enable cookie tracking
- AccountUtils.restoreCookies(account.getSavedAccount(), client, context);
-
account.loadCredentials(context);
client.setCredentials(account.getCredentials());
+
+ if (client.getCredentials() instanceof OwnCloudSamlSsoCredentials) {
+ client.disableAutomaticCookiesHandling();
+ }
+
if (accountName != null) {
mClientsWithKnownUsername.put(accountName, client);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
@@ -141,7 +140,9 @@ public class SingleSessionManager implements OwnCloudClientManager {
if (!reusingKnown && Log.isLoggable(TAG, Log.VERBOSE)) {
Log_OC.v(TAG, "reusing client for session " + sessionName);
}
- keepCredentialsUpdated(account, client);
+
+ keepCredentialsUpdated(client);
+ keepCookiesUpdated(context, account, client);
keepUriUpdated(account, client);
}
@@ -154,7 +155,6 @@ public class SingleSessionManager implements OwnCloudClientManager {
@Override
public OwnCloudClient removeClientFor(OwnCloudAccount account) {
-
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log_OC.d(TAG, "removeClientFor starting ");
}
@@ -185,14 +185,11 @@ public class SingleSessionManager implements OwnCloudClientManager {
Log_OC.d(TAG, "removeClientFor finishing ");
}
return null;
-
}
@Override
- public void saveAllClients(Context context, String accountType)
- throws AccountNotFoundException, AuthenticatorException, IOException,
- OperationCanceledException {
+ public void saveAllClients(Context context, String accountType) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log_OC.d(TAG, "Saving sessions... ");
@@ -215,14 +212,19 @@ public class SingleSessionManager implements OwnCloudClientManager {
}
}
+ private void keepCredentialsUpdated(OwnCloudClient reusedClient) {
+ reusedClient.applyCredentials();
+ }
- private void keepCredentialsUpdated(OwnCloudAccount account, OwnCloudClient reusedClient) {
- OwnCloudCredentials recentCredentials = account.getCredentials();
- if (recentCredentials != null && !recentCredentials.getAuthToken().equals(
- reusedClient.getCredentials().getAuthToken())) {
- reusedClient.setCredentials(recentCredentials);
+ private void keepCookiesUpdated(Context context, OwnCloudAccount account, OwnCloudClient reusedClient) {
+ AccountManager am = AccountManager.get(context.getApplicationContext());
+ if (am != null && account.getSavedAccount() != null) {
+ String recentCookies = am.getUserData(account.getSavedAccount(), AccountUtils.Constants.KEY_COOKIES);
+ String previousCookies = reusedClient.getCookiesString();
+ if (recentCookies != null && previousCookies != "" && !recentCookies.equals(previousCookies)) {
+ AccountUtils.restoreCookies(account.getSavedAccount(), reusedClient, context);
+ }
}
-
}
// this method is just a patch; we need to distinguish accounts in the same host but
@@ -232,8 +234,5 @@ public class SingleSessionManager implements OwnCloudClientManager {
if (!recentUri.equals(reusedClient.getBaseUri())) {
reusedClient.setBaseUri(recentUri);
}
-
}
-
-
-}
+}
\ No newline at end of file
diff --git a/src/com/owncloud/android/lib/common/accounts/AccountUtils.java b/src/com/owncloud/android/lib/common/accounts/AccountUtils.java
index a93ad141..0b0f7ab9 100644
--- a/src/com/owncloud/android/lib/common/accounts/AccountUtils.java
+++ b/src/com/owncloud/android/lib/common/accounts/AccountUtils.java
@@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license
- * Copyright (C) 2017 ownCloud GmbH.
+ * Copyright (C) 2018 ownCloud GmbH.
* Copyright (C) 2012 Bartek Przybylski
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -25,11 +25,6 @@
package com.owncloud.android.lib.common.accounts;
-import java.io.IOException;
-
-import org.apache.commons.httpclient.Cookie;
-import org.apache.commons.httpclient.HttpStatus;
-
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountsException;
@@ -38,15 +33,19 @@ import android.accounts.OperationCanceledException;
import android.content.Context;
import android.net.Uri;
-import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.OwnCloudClient;
-import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
import com.owncloud.android.lib.common.authentication.OwnCloudCredentials;
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory;
-import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import okhttp3.Cookie;
+
public class AccountUtils {
private static final String TAG = AccountUtils.class.getSimpleName();
@@ -61,11 +60,23 @@ public class AccountUtils {
*/
public static String getWebDavUrlForAccount(Context context, Account account)
throws AccountNotFoundException {
+ String webDavUrlForAccount = "";
- return getBaseUrlForAccount(context, account) + OwnCloudClient.WEBDAV_PATH_4_0;
+ try {
+ OwnCloudCredentials ownCloudCredentials = getCredentialsForAccount(context, account);
+ webDavUrlForAccount = getBaseUrlForAccount(context, account) + OwnCloudClient.NEW_WEBDAV_FILES_PATH_4_0
+ + ownCloudCredentials.getUsername();
+ } catch (OperationCanceledException e) {
+ e.printStackTrace();
+ } catch (AuthenticatorException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return webDavUrlForAccount;
}
-
/**
* Extracts url server from the account
*
@@ -86,7 +97,6 @@ public class AccountUtils {
return baseurl;
}
-
/**
* Get the username corresponding to an OC account.
*
@@ -132,7 +142,7 @@ public class AccountUtils {
public static OwnCloudCredentials getCredentialsForAccount(Context context, Account account)
throws OperationCanceledException, AuthenticatorException, IOException {
- OwnCloudCredentials credentials = null;
+ OwnCloudCredentials credentials;
AccountManager am = AccountManager.get(context);
String supportsOAuth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2);
@@ -176,10 +186,8 @@ public class AccountUtils {
}
return credentials;
-
}
-
public static String buildAccountNameOld(Uri serverBaseUrl, String username) {
if (serverBaseUrl.getScheme() == null) {
serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString());
@@ -207,7 +215,6 @@ public class AccountUtils {
}
public static void saveClient(OwnCloudClient client, Account savedAccount, Context context) {
-
// Account Manager
AccountManager ac = AccountManager.get(context.getApplicationContext());
@@ -215,13 +222,11 @@ public class AccountUtils {
String cookiesString = client.getCookiesString();
if (!"".equals(cookiesString)) {
ac.setUserData(savedAccount, Constants.KEY_COOKIES, cookiesString);
- // Log_OC.d(TAG, "Saving Cookies: "+ cookiesString );
+ Log_OC.d(TAG, "Saving Cookies: "+ cookiesString );
}
}
-
}
-
/**
* Restore the client cookies persisted in an account stored in the system AccountManager.
*
@@ -239,23 +244,28 @@ public class AccountUtils {
// Account Manager
AccountManager am = AccountManager.get(context.getApplicationContext());
- Uri serverUri = (client.getBaseUri() != null) ? client.getBaseUri() : client.getWebdavUri();
+ Uri serverUri = (client.getBaseUri() != null) ? client.getBaseUri() : client.getNewFilesWebDavUri();
String cookiesString = am.getUserData(account, Constants.KEY_COOKIES);
if (cookiesString != null) {
- String[] cookies = cookiesString.split(";");
- if (cookies.length > 0) {
- for (int i = 0; i < cookies.length; i++) {
- Cookie cookie = new Cookie();
- int equalPos = cookies[i].indexOf('=');
- cookie.setName(cookies[i].substring(0, equalPos));
- cookie.setValue(cookies[i].substring(equalPos + 1));
- cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT
- cookie.setPath(serverUri.getPath()); // VERY IMPORTANT
-
- client.getState().addCookie(cookie);
- }
+ String[] rawCookies = cookiesString.split(";");
+ List cookieList = new ArrayList<>(rawCookies.length);
+ for(String rawCookie : rawCookies) {
+ rawCookie = rawCookie.replace(" ", "");
+ final int equalPos = rawCookie.indexOf('=');
+ if (equalPos == -1) continue;
+ cookieList.add(new Cookie.Builder()
+ .name(rawCookie.substring(0, equalPos))
+ .value(rawCookie.substring(equalPos + 1))
+ .domain(serverUri.getHost())
+ .path(
+ serverUri.getPath().equals("")
+ ? FileUtils.PATH_SEPARATOR
+ : serverUri.getPath()
+ )
+ .build());
}
+ client.setCookiesForCurrentAccount(cookieList);
}
}
}
@@ -279,7 +289,6 @@ public class AccountUtils {
}
}
-
public static class Constants {
/**
* Version should be 3 numbers separated by dot so it can be parsed by
@@ -320,5 +329,4 @@ public class AccountUtils {
public static final String KEY_OAUTH2_REFRESH_TOKEN = "oc_oauth2_refresh_token";
}
-
-}
+}
\ No newline at end of file
diff --git a/src/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java b/src/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java
index 96ed4568..ecd2a0c4 100644
--- a/src/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java
+++ b/src/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java
@@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license
- * Copyright (C) 2016 ownCloud GmbH.
+ * Copyright (C) 2018 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -24,48 +24,36 @@
package com.owncloud.android.lib.common.authentication;
import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.common.http.HttpClient;
+import com.owncloud.android.lib.common.http.HttpConstants;
-import java.util.ArrayList;
-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;
+import okhttp3.Credentials;
public class OwnCloudBasicCredentials implements OwnCloudCredentials {
+ private static final String TAG = OwnCloudCredentials.class.getSimpleName();
+
private String mUsername;
private String mPassword;
- private boolean mAuthenticationPreemptive;
public OwnCloudBasicCredentials(String username, String password) {
mUsername = username != null ? username : "";
mPassword = password != null ? password : "";
- mAuthenticationPreemptive = true;
}
public OwnCloudBasicCredentials(String username, String password, boolean preemptiveMode) {
mUsername = username != null ? username : "";
mPassword = password != null ? password : "";
- mAuthenticationPreemptive = preemptiveMode;
}
@Override
public void applyTo(OwnCloudClient client) {
- AuthPolicy.registerAuthScheme(AuthState.PREEMPTIVE_AUTH_SCHEME, BasicScheme.class);
+ // Clear previous basic credentials
+ HttpClient.deleteHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER);
+ HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER);
- List authPrefs = new ArrayList(1);
- authPrefs.add(AuthPolicy.BASIC);
- client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
-
- client.getParams().setAuthenticationPreemptive(mAuthenticationPreemptive);
- client.getParams().setCredentialCharset(OwnCloudCredentialsFactory.CREDENTIAL_CHARSET);
- client.getState().setCredentials(
- AuthScope.ANY,
- new UsernamePasswordCredentials(mUsername, mPassword)
- );
+ HttpClient.addHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER,
+ Credentials.basic(mUsername, mPassword));
}
@Override
@@ -87,5 +75,4 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials {
public boolean authTokenCanBeRefreshed() {
return false;
}
-
-}
+}
\ No newline at end of file
diff --git a/src/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java b/src/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java
index 968d6703..a53712d6 100644
--- a/src/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java
+++ b/src/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java
@@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license
- * Copyright (C) 2016 ownCloud GmbH.
+ * Copyright (C) 2018 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -23,16 +23,9 @@
*/
package com.owncloud.android.lib.common.authentication;
-import java.util.ArrayList;
-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.OwnCloudClient;
-import com.owncloud.android.lib.common.authentication.oauth.BearerAuthScheme;
-import com.owncloud.android.lib.common.authentication.oauth.BearerCredentials;
+import com.owncloud.android.lib.common.http.HttpClient;
+import com.owncloud.android.lib.common.http.HttpConstants;
public class OwnCloudBearerCredentials implements OwnCloudCredentials {
@@ -46,19 +39,12 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials {
@Override
public void applyTo(OwnCloudClient client) {
- AuthPolicy.registerAuthScheme(BearerAuthScheme.AUTH_POLICY, BearerAuthScheme.class);
- AuthPolicy.registerAuthScheme(AuthState.PREEMPTIVE_AUTH_SCHEME, BearerAuthScheme.class);
+ // Clear previous credentials
+ HttpClient.deleteHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER);
+ HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER);
- List 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.getState().setCredentials(
- AuthScope.ANY,
- new BearerCredentials(mAccessToken)
- );
+ HttpClient.addHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER,
+ HttpConstants.BEARER_AUTHORIZATION_KEY + mAccessToken);
}
@Override
@@ -81,5 +67,4 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials {
public boolean authTokenCanBeRefreshed() {
return true;
}
-
-}
+}
\ No newline at end of file
diff --git a/src/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java b/src/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java
index 8643aaea..a7f2ff80 100644
--- a/src/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java
+++ b/src/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java
@@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license
- * Copyright (C) 2016 ownCloud GmbH.
+ * Copyright (C) 2018 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -25,6 +25,8 @@
package com.owncloud.android.lib.common.authentication;
import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.common.http.HttpClient;
+import com.owncloud.android.lib.common.http.HttpConstants;
public class OwnCloudCredentialsFactory {
@@ -64,8 +66,9 @@ public class OwnCloudCredentialsFactory {
@Override
public void applyTo(OwnCloudClient client) {
- client.getState().clearCredentials();
- client.getState().clearCookies();
+ // Clear previous basic credentials
+ HttpClient.deleteHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER);
+ HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER);
}
@Override
@@ -89,5 +92,4 @@ public class OwnCloudCredentialsFactory {
return null;
}
}
-
-}
+}
\ No newline at end of file
diff --git a/src/com/owncloud/android/lib/common/authentication/OwnCloudSamlSsoCredentials.java b/src/com/owncloud/android/lib/common/authentication/OwnCloudSamlSsoCredentials.java
index 0f475428..a01e05da 100644
--- a/src/com/owncloud/android/lib/common/authentication/OwnCloudSamlSsoCredentials.java
+++ b/src/com/owncloud/android/lib/common/authentication/OwnCloudSamlSsoCredentials.java
@@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license
- * Copyright (C) 2016 ownCloud GmbH.
+ * Copyright (C) 2018 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -23,12 +23,9 @@
*/
package com.owncloud.android.lib.common.authentication;
-import org.apache.commons.httpclient.Cookie;
-import org.apache.commons.httpclient.cookie.CookiePolicy;
-
-import android.net.Uri;
-
import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.common.http.HttpClient;
+import com.owncloud.android.lib.common.http.HttpConstants;
public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials {
@@ -42,28 +39,12 @@ public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials {
@Override
public void applyTo(OwnCloudClient client) {
- client.getParams().setAuthenticationPreemptive(false);
- client.getParams().setCredentialCharset(OwnCloudCredentialsFactory.CREDENTIAL_CHARSET);
- client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
+ // Clear previous credentials
+ HttpClient.deleteHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER);
+ HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER);
+
+ HttpClient.addHeaderForAllRequests(HttpConstants.COOKIE_HEADER, mSessionCookie);
client.setFollowRedirects(false);
-
- Uri serverUri = client.getBaseUri();
-
- String[] cookies = mSessionCookie.split(";");
- if (cookies.length > 0) {
- Cookie cookie = null;
- for (int i = 0; i < cookies.length; i++) {
- int equalPos = cookies[i].indexOf('=');
- if (equalPos >= 0) {
- cookie = new Cookie();
- cookie.setName(cookies[i].substring(0, equalPos));
- cookie.setValue(cookies[i].substring(equalPos + 1));
- cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT
- cookie.setPath(serverUri.getPath()); // VERY IMPORTANT
- client.getState().addCookie(cookie);
- }
- }
- }
}
@Override
@@ -86,5 +67,4 @@ public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials {
public boolean authTokenCanBeRefreshed() {
return false;
}
-
-}
+}
\ No newline at end of file
diff --git a/src/com/owncloud/android/lib/common/authentication/oauth/BearerAuthScheme.java b/src/com/owncloud/android/lib/common/authentication/oauth/BearerAuthScheme.java
deleted file mode 100644
index b0104342..00000000
--- a/src/com/owncloud/android/lib/common/authentication/oauth/BearerAuthScheme.java
+++ /dev/null
@@ -1,262 +0,0 @@
-/* ownCloud Android Library is available under MIT license
- * Copyright (C) 2016 ownCloud GmbH.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
-
-package com.owncloud.android.lib.common.authentication.oauth;
-
-import java.util.Map;
-
-import org.apache.commons.httpclient.Credentials;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.auth.AuthChallengeParser;
-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;
-
-
-
-/**
- * Bearer authentication scheme as defined in RFC 6750.
- *
- * @author David A. Velasco
- */
-
-public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
-
- private static final String TAG = BearerAuthScheme.class.getSimpleName();
-
- public static final String AUTH_POLICY = "Bearer";
-
- /** Whether the bearer authentication process is complete */
- private boolean mComplete;
-
- /** Authentication parameter map */
- @SuppressWarnings("rawtypes")
- private Map mParams = null;
-
-
- /**
- * Default constructor for the bearer authentication scheme.
- */
- public BearerAuthScheme() {
- mComplete = false;
- }
-
- /**
- * Constructor for the basic authentication scheme.
- *
- * @param challenge Authentication challenge
- *
- * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
- */
- public BearerAuthScheme(final String challenge) throws MalformedChallengeException {
- processChallenge(challenge);
- mComplete = true;
- }
-
- /**
- * Returns textual designation of the bearer authentication scheme.
- *
- * @return "Bearer"
- */
- public String getSchemeName() {
- return "bearer";
- }
-
- /**
- * Processes the Bearer challenge.
- *
- * @param challenge The challenge string
- *
- * @throws MalformedChallengeException Thrown if the authentication challenge is malformed
- */
- public void processChallenge(String challenge) throws MalformedChallengeException {
- String s = AuthChallengeParser.extractScheme(challenge);
- if (!s.equalsIgnoreCase(getSchemeName())) {
- throw new MalformedChallengeException(
- "Invalid " + getSchemeName() + " challenge: " + challenge);
- }
- mParams = AuthChallengeParser.extractParams(challenge);
- mComplete = true;
- }
-
- /**
- * Tests if the Bearer authentication process has been completed.
- *
- * @return 'true' if Bearer authorization has been processed, 'false' otherwise.
- */
- public boolean isComplete() {
- return this.mComplete;
- }
-
- /**
- * Produces bearer authorization string for the given set of
- * {@link Credentials}.
- *
- * @param credentials The set of credentials to be used for authentication
- * @param method Method name is ignored by the bearer authentication scheme
- * @param uri URI is ignored by the bearer authentication scheme
- * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable
- * for this authentication scheme
- * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure
- * @return A bearer authorization string
- */
- public String authenticate(Credentials credentials, String method, String uri) throws AuthenticationException {
- BearerCredentials bearer;
- try {
- bearer = (BearerCredentials) credentials;
- } catch (ClassCastException e) {
- throw new InvalidCredentialsException(
- "Credentials cannot be used for bearer authentication: "
- + credentials.getClass().getName());
- }
- return BearerAuthScheme.authenticate(bearer);
- }
-
-
- /**
- * Returns 'false'. Bearer authentication scheme is request based.
- *
- * @return 'false'.
- */
- public boolean isConnectionBased() {
- return false;
- }
-
- /**
- * Produces bearer authorization string for the given set of {@link Credentials}.
- *
- * @param credentials The set of credentials to be used for authentication
- * @param method The method being authenticated
- * @throws InvalidCredentialsException If authentication credentials are not valid or not applicable for this authentication
- * scheme.
- * @throws AuthenticationException If authorization string cannot be generated due to an authentication failure.
- *
- * @return a basic authorization string
- */
- public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
- if (method == null) {
- throw new IllegalArgumentException("Method may not be null");
- }
- BearerCredentials bearer = null;
- try {
- bearer = (BearerCredentials) credentials;
- } catch (ClassCastException e) {
- throw new InvalidCredentialsException(
- "Credentials cannot be used for bearer authentication: "
- + credentials.getClass().getName());
- }
- return BearerAuthScheme.authenticate(
- bearer,
- method.getParams().getCredentialCharset());
- }
-
- /**
- * Returns a bearer Authorization header value for the given
- * {@link BearerCredentials}.
- *
- * @param credentials The credentials to encode.
- *
- * @return A bearer authorization string
- */
- public static String authenticate(BearerCredentials credentials) {
- return authenticate(credentials, "ISO-8859-1");
- }
-
- /**
- * Returns a bearer Authorization header value for the given
- * {@link BearerCredentials} and charset.
- *
- * @param credentials The credentials to encode.
- * @param charset The charset to use for encoding the credentials
- *
- * @return A bearer authorization string
- *
- * @since 3.0
- */
- public static String authenticate(BearerCredentials credentials, String charset) {
-
- if (credentials == null) {
- throw new IllegalArgumentException("Credentials may not be null");
- }
- if (charset == null || charset.length() == 0) {
- throw new IllegalArgumentException("charset may not be null or empty");
- }
- StringBuffer buffer = new StringBuffer();
- buffer.append(credentials.getAccessToken());
-
- return "Bearer " + buffer.toString();
- }
-
- /**
- * Returns a String identifying the authentication challenge. This is
- * used, in combination with the host and port to determine if
- * authorization has already been attempted or not. Schemes which
- * require multiple requests to complete the authentication should
- * return a different value for each stage in the request.
- *
- * Additionally, the ID should take into account any changes to the
- * authentication challenge and return a different value when appropriate.
- * For example when the realm changes in basic authentication it should be
- * considered a different authentication attempt and a different value should
- * be returned.
- *
- * This method simply returns the realm for the challenge.
- *
- * @return String a String identifying the authentication challenge.
- */
- @Override
- public String getID() {
- return getRealm();
- }
-
- /**
- * Returns authentication parameter with the given name, if available.
- *
- * @param name The name of the parameter to be returned
- *
- * @return The parameter with the given name
- */
- @Override
- public String getParameter(String name) {
- if (name == null) {
- throw new IllegalArgumentException("Parameter name may not be null");
- }
- if (mParams == null) {
- return null;
- }
- return (String) mParams.get(name.toLowerCase());
- }
-
- /**
- * Returns authentication realm. The realm may not be null.
- *
- * @return The authentication realm
- */
- @Override
- public String getRealm() {
- return getParameter("realm");
- }
-
-}
diff --git a/src/com/owncloud/android/lib/common/authentication/oauth/BearerCredentials.java b/src/com/owncloud/android/lib/common/authentication/oauth/BearerCredentials.java
index c815bc4c..6d82bdb3 100644
--- a/src/com/owncloud/android/lib/common/authentication/oauth/BearerCredentials.java
+++ b/src/com/owncloud/android/lib/common/authentication/oauth/BearerCredentials.java
@@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license
- * Copyright (C) 2016 ownCloud GmbH.
+ * Copyright (C) 2018 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -24,16 +24,14 @@
package com.owncloud.android.lib.common.authentication.oauth;
-import org.apache.commons.httpclient.Credentials;
-import org.apache.commons.httpclient.util.LangUtils;
-
/**
- * Bearer token {@link Credentials}
- *
* @author David A. Velasco
+ * @author Christian Schabesberger
*/
-public class BearerCredentials implements Credentials {
+public class BearerCredentials {
+ public static final int HASH_SEED = 17;
+ public static final int HASH_OFFSET = 37;
private String mAccessToken;
@@ -76,9 +74,7 @@ public class BearerCredentials implements Credentials {
* @return The hash code of the access token
*/
public int hashCode() {
- int hash = LangUtils.HASH_SEED;
- hash = LangUtils.hashCode(hash, mAccessToken);
- return hash;
+ return HASH_SEED * HASH_OFFSET + mAccessToken.hashCode();
}
/**
@@ -93,12 +89,10 @@ public class BearerCredentials implements Credentials {
if (this == o) return true;
if (this.getClass().equals(o.getClass())) {
BearerCredentials that = (BearerCredentials) o;
- if (LangUtils.equals(mAccessToken, that.mAccessToken)) {
+ if (mAccessToken.equals(that.mAccessToken)) {
return true;
}
}
return false;
}
-
-}
-
+}
\ No newline at end of file
diff --git a/src/com/owncloud/android/lib/common/authentication/oauth/OAuth2GetAccessTokenOperation.java b/src/com/owncloud/android/lib/common/authentication/oauth/OAuth2GetAccessTokenOperation.java
index 33b3c8b5..a3e5666f 100644
--- a/src/com/owncloud/android/lib/common/authentication/oauth/OAuth2GetAccessTokenOperation.java
+++ b/src/com/owncloud/android/lib/common/authentication/oauth/OAuth2GetAccessTokenOperation.java
@@ -1,7 +1,8 @@
/* ownCloud Android Library is available under MIT license
*
* @author David A. Velasco
- * Copyright (C) 2017 ownCloud GmbH.
+ * @author Christian Schabesberger
+ * Copyright (C) 2018 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -31,21 +32,21 @@ import android.net.Uri;
import com.owncloud.android.lib.common.authentication.OwnCloudBasicCredentials;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.authentication.OwnCloudCredentials;
+import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod;
+import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
-import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
-import org.apache.commons.httpclient.NameValuePair;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.json.JSONException;
import org.json.JSONObject;
-import java.util.ArrayList;
-import java.util.HashMap;
+import java.net.URL;
import java.util.Map;
+import okhttp3.MultipartBody;
+import okhttp3.RequestBody;
-public class OAuth2GetAccessTokenOperation extends RemoteOperation {
+
+public class OAuth2GetAccessTokenOperation extends RemoteOperation