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:
parent
549cc32703
commit
5d3bba65e2
@ -115,10 +115,6 @@ public class OwnCloudClient extends HttpClient {
|
|||||||
mCredentials.applyTo(this);
|
mCredentials.applyTo(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void applyCookies() {
|
|
||||||
AccountUtils.restoreCookies(this.getAccount().getSavedAccount(), this, getContext());
|
|
||||||
}
|
|
||||||
|
|
||||||
public int executeHttpMethod (HttpBaseMethod method) throws Exception {
|
public int executeHttpMethod (HttpBaseMethod method) throws Exception {
|
||||||
|
|
||||||
boolean repeatWithFreshCredentials;
|
boolean repeatWithFreshCredentials;
|
||||||
|
@ -43,6 +43,7 @@ import android.util.Log;
|
|||||||
import com.owncloud.android.lib.common.accounts.AccountUtils;
|
import com.owncloud.android.lib.common.accounts.AccountUtils;
|
||||||
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
|
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
|
||||||
import com.owncloud.android.lib.common.authentication.OwnCloudCredentials;
|
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 com.owncloud.android.lib.common.utils.Log_OC;
|
||||||
|
|
||||||
import okhttp3.Cookie;
|
import okhttp3.Cookie;
|
||||||
@ -55,6 +56,7 @@ import okhttp3.Cookie;
|
|||||||
* @author David A. Velasco
|
* @author David A. Velasco
|
||||||
* @author masensio
|
* @author masensio
|
||||||
* @author Christian Schabesberger
|
* @author Christian Schabesberger
|
||||||
|
* @author David González Verdugo
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class SingleSessionManager implements OwnCloudClientManager {
|
public class SingleSessionManager implements OwnCloudClientManager {
|
||||||
@ -124,10 +126,12 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
|||||||
client.setContext(context);
|
client.setContext(context);
|
||||||
client.setOwnCloudClientManager(this);
|
client.setOwnCloudClientManager(this);
|
||||||
|
|
||||||
// enable cookie tracking
|
|
||||||
AccountUtils.restoreCookies(account.getSavedAccount(), client, context);
|
|
||||||
|
|
||||||
client.setCredentials(account.getCredentials());
|
client.setCredentials(account.getCredentials());
|
||||||
|
|
||||||
|
if (client.getCredentials() instanceof OwnCloudSamlSsoCredentials) {
|
||||||
|
client.disableAutomaticCookiesHandling();
|
||||||
|
}
|
||||||
|
|
||||||
if (accountName != null) {
|
if (accountName != null) {
|
||||||
mClientsWithKnownUsername.put(accountName, client);
|
mClientsWithKnownUsername.put(accountName, client);
|
||||||
if (Log.isLoggable(TAG, Log.VERBOSE)) {
|
if (Log.isLoggable(TAG, Log.VERBOSE)) {
|
||||||
@ -144,6 +148,7 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
|||||||
if (!reusingKnown && Log.isLoggable(TAG, Log.VERBOSE)) {
|
if (!reusingKnown && Log.isLoggable(TAG, Log.VERBOSE)) {
|
||||||
Log_OC.v(TAG, "reusing client for session " + sessionName);
|
Log_OC.v(TAG, "reusing client for session " + sessionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
keepCredentialsUpdated(account, client);
|
keepCredentialsUpdated(account, client);
|
||||||
keepCookiesUpdated(context, account, client);
|
keepCookiesUpdated(context, account, client);
|
||||||
keepUriUpdated(account, client);
|
keepUriUpdated(account, client);
|
||||||
@ -189,7 +194,6 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
|||||||
Log_OC.d(TAG, "removeClientFor finishing ");
|
Log_OC.d(TAG, "removeClientFor finishing ");
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -224,15 +228,18 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
|||||||
if (recentCredentials != null && !recentCredentials.getAuthToken().equals(
|
if (recentCredentials != null && !recentCredentials.getAuthToken().equals(
|
||||||
reusedClient.getCredentials().getAuthToken())) {
|
reusedClient.getCredentials().getAuthToken())) {
|
||||||
reusedClient.setCredentials(recentCredentials);
|
reusedClient.setCredentials(recentCredentials);
|
||||||
|
reusedClient.applyCredentials();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void keepCookiesUpdated(Context context, OwnCloudAccount account, OwnCloudClient reusedClient) {
|
private void keepCookiesUpdated(Context context, OwnCloudAccount account, OwnCloudClient reusedClient) {
|
||||||
AccountManager am = AccountManager.get(context.getApplicationContext());
|
AccountManager am = AccountManager.get(context.getApplicationContext());
|
||||||
String currentCookies = am.getUserData(account.getSavedAccount(), AccountUtils.Constants.KEY_COOKIES);
|
if (am != null && account.getSavedAccount() != null) {
|
||||||
String previousCookies = reusedClient.getCookiesString();
|
String recentCookies = am.getUserData(account.getSavedAccount(), AccountUtils.Constants.KEY_COOKIES);
|
||||||
if (currentCookies != null && previousCookies != "" && !currentCookies.equals(previousCookies)) {
|
String previousCookies = reusedClient.getCookiesString();
|
||||||
AccountUtils.restoreCookies(account.getSavedAccount(), reusedClient, context);
|
if (recentCookies != null && previousCookies != "" && !recentCookies.equals(previousCookies)) {
|
||||||
|
AccountUtils.restoreCookies(account.getSavedAccount(), reusedClient, context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,6 +135,22 @@ public class HttpClient {
|
|||||||
sOkHttpClient = clientBuilder.build();
|
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
|
* Add header that will be included for all the requests from now on
|
||||||
* @param headerName
|
* @param headerName
|
||||||
|
@ -51,6 +51,7 @@ public abstract class HttpBaseMethod {
|
|||||||
protected Request mRequest;
|
protected Request mRequest;
|
||||||
protected RequestBody mRequestBody;
|
protected RequestBody mRequestBody;
|
||||||
protected Response mResponse;
|
protected Response mResponse;
|
||||||
|
protected String mResponseBodyString;
|
||||||
protected Call mCall;
|
protected Call mCall;
|
||||||
|
|
||||||
protected HttpBaseMethod(URL url) {
|
protected HttpBaseMethod(URL url) {
|
||||||
@ -103,10 +104,11 @@ public abstract class HttpBaseMethod {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getResponseBodyAsString() throws IOException {
|
public String getResponseBodyAsString() throws IOException {
|
||||||
if (mResponse.body() != null) {
|
if (mResponseBodyString == null && mResponse.body() != null) {
|
||||||
return mResponse.body().string();
|
mResponseBodyString = mResponse.body().string();
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
return mResponseBodyString;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getResponseBodyAsStream() {
|
public InputStream getResponseBodyAsStream() {
|
||||||
|
@ -145,7 +145,6 @@ public abstract class RemoteOperation<T extends Object> implements Runnable {
|
|||||||
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
|
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
|
||||||
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
|
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
|
||||||
getClientFor(ocAccount, mContext);
|
getClientFor(ocAccount, mContext);
|
||||||
mClient.applyCredentials();
|
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Trying to run a remote operation " +
|
throw new IllegalStateException("Trying to run a remote operation " +
|
||||||
"asynchronously with no client and no chance to create one (no account)");
|
"asynchronously with no client and no chance to create one (no account)");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user