mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-28 00:48:50 +00:00 
			
		
		
		
	Add cookie restore to methods OwnCloudClientFactory#createOwnCloudClient(Account ...)
This commit is contained in:
		
							parent
							
								
									1b3af22b89
								
							
						
					
					
						commit
						becf4f3c13
					
				| @ -27,9 +27,12 @@ package com.owncloud.android.lib.common; | ||||
| import java.io.IOException; | ||||
| import java.security.GeneralSecurityException; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.Cookie; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.accounts.AccountTypeUtils; | ||||
| 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.Constants; | ||||
| import com.owncloud.android.lib.common.network.NetworkUtils; | ||||
| 
 | ||||
| import android.accounts.Account; | ||||
| @ -85,7 +88,7 @@ public class OwnCloudClientFactory { | ||||
|         } else if (isSamlSso) {    // TODO avoid a call to getUserData here | ||||
|             String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), false); | ||||
|             client.setSsoSessionCookie(accessToken); | ||||
|              | ||||
| 
 | ||||
|         } else { | ||||
|             String username = account.name.substring(0, account.name.lastIndexOf('@')); | ||||
|             //String password = am.getPassword(account); | ||||
| @ -93,6 +96,9 @@ public class OwnCloudClientFactory { | ||||
|             client.setBasicCredentials(username, password); | ||||
|         } | ||||
|          | ||||
|         // Restore cookies | ||||
|         restoreCookies(am, account, client); | ||||
|          | ||||
|         return client; | ||||
|     } | ||||
|      | ||||
| @ -130,6 +136,9 @@ public class OwnCloudClientFactory { | ||||
|             client.setBasicCredentials(username, password); | ||||
|         } | ||||
|          | ||||
|         // Restore cookies | ||||
|         restoreCookies(am, account, client); | ||||
|          | ||||
|         return client; | ||||
|     } | ||||
|      | ||||
| @ -159,5 +168,34 @@ public class OwnCloudClientFactory { | ||||
|         return client; | ||||
|     } | ||||
|      | ||||
|      | ||||
|     /** | ||||
|      * Restore the client cookies | ||||
|      * @param am | ||||
|      * @param account | ||||
|      * @param client | ||||
|      */ | ||||
|     private static void restoreCookies(AccountManager am, Account account, OwnCloudClient client) { | ||||
| 
 | ||||
|     	Log.d(TAG, "Restoring cookies for " + account.name); | ||||
|     	 | ||||
|     	Uri serverUri = (client.getBaseUri() != null)? client.getBaseUri() : client.getWebdavUri(); | ||||
| 
 | ||||
|     	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); | ||||
|     			} | ||||
|     		} | ||||
|     	} | ||||
|     	 | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -124,6 +124,7 @@ public class OwnCloudClientMap { | ||||
|     	// Get all accounts | ||||
|     	Account [] accounts = AccountManager.get(context.getApplicationContext()) | ||||
|     			.getAccountsByType(accountType); | ||||
|     	 | ||||
|     	// Save cookies for all accounts | ||||
|     	for(Account account: accounts){ | ||||
|     		saveClient(account, context.getApplicationContext()); | ||||
| @ -142,4 +143,5 @@ public class OwnCloudClientMap { | ||||
|     	Log.d(TAG, "       comment: "+ cookie.getComment() ); | ||||
|     	Log.d(TAG, "       secure: "+ cookie.getSecure() ); | ||||
|     } | ||||
|      | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user