1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Fix SAML with new cookies handling

This commit is contained in:
davigonz 2018-10-01 10:16:12 +02:00
parent 549cc32703
commit 5d3bba65e2
5 changed files with 36 additions and 16 deletions

View File

@ -115,10 +115,6 @@ public class OwnCloudClient extends HttpClient {
mCredentials.applyTo(this);
}
public void applyCookies() {
AccountUtils.restoreCookies(this.getAccount().getSavedAccount(), this, getContext());
}
public int executeHttpMethod (HttpBaseMethod method) throws Exception {
boolean repeatWithFreshCredentials;

View File

@ -43,6 +43,7 @@ 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 okhttp3.Cookie;
@ -55,6 +56,7 @@ import okhttp3.Cookie;
* @author David A. Velasco
* @author masensio
* @author Christian Schabesberger
* @author David González Verdugo
*/
public class SingleSessionManager implements OwnCloudClientManager {
@ -124,10 +126,12 @@ public class SingleSessionManager implements OwnCloudClientManager {
client.setContext(context);
client.setOwnCloudClientManager(this);
// enable cookie tracking
AccountUtils.restoreCookies(account.getSavedAccount(), client, 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)) {
@ -144,6 +148,7 @@ public class SingleSessionManager implements OwnCloudClientManager {
if (!reusingKnown && Log.isLoggable(TAG, Log.VERBOSE)) {
Log_OC.v(TAG, "reusing client for session " + sessionName);
}
keepCredentialsUpdated(account, client);
keepCookiesUpdated(context, account, client);
keepUriUpdated(account, client);
@ -189,7 +194,6 @@ public class SingleSessionManager implements OwnCloudClientManager {
Log_OC.d(TAG, "removeClientFor finishing ");
}
return null;
}
@ -224,15 +228,18 @@ public class SingleSessionManager implements OwnCloudClientManager {
if (recentCredentials != null && !recentCredentials.getAuthToken().equals(
reusedClient.getCredentials().getAuthToken())) {
reusedClient.setCredentials(recentCredentials);
reusedClient.applyCredentials();
}
}
private void keepCookiesUpdated(Context context, OwnCloudAccount account, OwnCloudClient reusedClient) {
AccountManager am = AccountManager.get(context.getApplicationContext());
String currentCookies = am.getUserData(account.getSavedAccount(), AccountUtils.Constants.KEY_COOKIES);
String previousCookies = reusedClient.getCookiesString();
if (currentCookies != null && previousCookies != "" && !currentCookies.equals(previousCookies)) {
AccountUtils.restoreCookies(account.getSavedAccount(), reusedClient, context);
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);
}
}
}

View File

@ -135,6 +135,22 @@ public class HttpClient {
sOkHttpClient = clientBuilder.build();
}
public void disableAutomaticCookiesHandling() {
OkHttpClient.Builder clientBuilder = getOkHttpClient().newBuilder();
clientBuilder.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
// DO NOTHING
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
return new ArrayList<>();
}
});
sOkHttpClient = clientBuilder.build();
}
/**
* Add header that will be included for all the requests from now on
* @param headerName

View File

@ -51,6 +51,7 @@ public abstract class HttpBaseMethod {
protected Request mRequest;
protected RequestBody mRequestBody;
protected Response mResponse;
protected String mResponseBodyString;
protected Call mCall;
protected HttpBaseMethod(URL url) {
@ -103,10 +104,11 @@ public abstract class HttpBaseMethod {
}
public String getResponseBodyAsString() throws IOException {
if (mResponse.body() != null) {
return mResponse.body().string();
if (mResponseBodyString == null && mResponse.body() != null) {
mResponseBodyString = mResponse.body().string();
}
return null;
return mResponseBodyString;
}
public InputStream getResponseBodyAsStream() {

View File

@ -145,7 +145,6 @@ public abstract class RemoteOperation<T extends Object> implements Runnable {
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(ocAccount, mContext);
mClient.applyCredentials();
} else {
throw new IllegalStateException("Trying to run a remote operation " +
"asynchronously with no client and no chance to create one (no account)");