mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	add OCAccount
add OCAccount
This commit is contained in:
		
							parent
							
								
									e354a716cb
								
							
						
					
					
						commit
						1249e205d6
					
				| @ -2,6 +2,7 @@ package com.owncloud.android.lib.refactor; | |||||||
| 
 | 
 | ||||||
| import android.net.Uri; | import android.net.Uri; | ||||||
| 
 | 
 | ||||||
|  | import com.owncloud.android.lib.refactor.account.OCAccount; | ||||||
| import com.owncloud.android.lib.refactor.authentication.OCCredentials; | import com.owncloud.android.lib.refactor.authentication.OCCredentials; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -18,33 +19,13 @@ public class OCContext { | |||||||
|     private static final boolean PARAM_SINGLE_COOKIE_HEADER_VALUE = true; |     private static final boolean PARAM_SINGLE_COOKIE_HEADER_VALUE = true; | ||||||
|     private static final String PARAM_PROTOCOL_VERSION = "http.protocol.version"; |     private static final String PARAM_PROTOCOL_VERSION = "http.protocol.version"; | ||||||
| 
 | 
 | ||||||
|     private OCCredentials mCredentials = null; |     private OCAccount mOCAccount; | ||||||
|     private Uri mBaseUri; |  | ||||||
| 
 | 
 | ||||||
|     public class Builder { |     public OCContext(OCAccount account) { | ||||||
|         OCContext ocContext = new OCContext(); |         mOCAccount = account; | ||||||
| 
 |  | ||||||
|         public Builder setCredentials(OCCredentials credentials) { |  | ||||||
|             ocContext.mCredentials = credentials; |  | ||||||
|             return this; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|         public Builder setBaseUri(Uri baseUri) { |     public OCAccount getOCAccount() { | ||||||
|             ocContext.mBaseUri = baseUri; |         return mOCAccount; | ||||||
|             return this; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         public OCContext build() { |  | ||||||
|             return ocContext; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     public OCCredentials getCredentials() { |  | ||||||
|         return mCredentials; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public Uri getBaseUri() { |  | ||||||
|         return mBaseUri; |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,5 +1,7 @@ | |||||||
| package com.owncloud.android.lib.refactor; | package com.owncloud.android.lib.refactor; | ||||||
| 
 | 
 | ||||||
|  | import android.net.Uri; | ||||||
|  | 
 | ||||||
| import java.util.Map; | import java.util.Map; | ||||||
| 
 | 
 | ||||||
| import okhttp3.OkHttpClient; | import okhttp3.OkHttpClient; | ||||||
| @ -29,16 +31,26 @@ public abstract class RemoteOperation { | |||||||
|         return httpClient; |         return httpClient; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public Uri.Builder getBaseUriBuilder() { | ||||||
|  |         return mContext.getOCAccount().getBaseUri().buildUpon(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     public Request.Builder getRequestBuilder() { |     public Request.Builder getRequestBuilder() { | ||||||
|         Request.Builder builder = new Request.Builder(); |         Request.Builder builder = new Request.Builder(); | ||||||
| 
 | 
 | ||||||
|         for(Map.Entry<String, String> header |         for(Map.Entry<String, String> header | ||||||
|                 : mContext.getCredentials().getCredentialHeaders().entrySet()) { |                 : mContext.getOCAccount() | ||||||
|  |                 .getCredentials() | ||||||
|  |                 .getCredentialHeaders() | ||||||
|  |                 .entrySet()) { | ||||||
|             builder.addHeader(header.getKey(), header.getValue()); |             builder.addHeader(header.getKey(), header.getValue()); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         //TODO: Remove this part once SAML is obsolet |         //TODO: Remove this part once SAML is obsolet | ||||||
|         final String credentialCookie = mContext.getCredentials().getCredentialCookie(); |         final String credentialCookie = mContext | ||||||
|  |                 .getOCAccount() | ||||||
|  |                 .getCredentials() | ||||||
|  |                 .getCredentialCookie(); | ||||||
|         if(credentialCookie == null) { |         if(credentialCookie == null) { | ||||||
|             builder.addHeader("Cookie", credentialCookie); |             builder.addHeader("Cookie", credentialCookie); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -27,7 +27,7 @@ package com.owncloud.android.lib.refactor; | |||||||
| import android.accounts.Account; | import android.accounts.Account; | ||||||
| import android.accounts.AccountsException; | import android.accounts.AccountsException; | ||||||
| 
 | 
 | ||||||
| import com.owncloud.android.lib.refactor.utils.AccountUtils; | import com.owncloud.android.lib.refactor.exceptions.AccountNotFoundException; | ||||||
| import com.owncloud.android.lib.refactor.exceptions.CertificateCombinedException; | import com.owncloud.android.lib.refactor.exceptions.CertificateCombinedException; | ||||||
| import com.owncloud.android.lib.refactor.exceptions.OperationCancelledException; | import com.owncloud.android.lib.refactor.exceptions.OperationCancelledException; | ||||||
| import com.owncloud.android.lib.refactor.utils.ErrorMessageParser; | import com.owncloud.android.lib.refactor.utils.ErrorMessageParser; | ||||||
| @ -71,7 +71,7 @@ public class RemoteOperationResult implements Serializable { | |||||||
|     /** |     /** | ||||||
|      * Generated - should be refreshed every time the class changes!! |      * Generated - should be refreshed every time the class changes!! | ||||||
|      */ |      */ | ||||||
|     private static final long serialVersionUID = 4968939884332372230L; |     private static final long serialVersionUID = 4968939884332652230L; | ||||||
| 
 | 
 | ||||||
|     private static final String TAG = RemoteOperationResult.class.getSimpleName(); |     private static final String TAG = RemoteOperationResult.class.getSimpleName(); | ||||||
| 
 | 
 | ||||||
| @ -184,7 +184,7 @@ public class RemoteOperationResult implements Serializable { | |||||||
|         } else if (e instanceof UnknownHostException) { |         } else if (e instanceof UnknownHostException) { | ||||||
|             mCode = ResultCode.HOST_NOT_AVAILABLE; |             mCode = ResultCode.HOST_NOT_AVAILABLE; | ||||||
| 
 | 
 | ||||||
|         } else if (e instanceof AccountUtils.AccountNotFoundException) { |         } else if (e instanceof AccountNotFoundException) { | ||||||
|             mCode = ResultCode.ACCOUNT_NOT_FOUND; |             mCode = ResultCode.ACCOUNT_NOT_FOUND; | ||||||
| 
 | 
 | ||||||
|         } else if (e instanceof AccountsException) { |         } else if (e instanceof AccountsException) { | ||||||
| @ -444,9 +444,9 @@ public class RemoteOperationResult implements Serializable { | |||||||
|             if(mException instanceof DavException) return "Unexpected WebDAV exception"; |             if(mException instanceof DavException) return "Unexpected WebDAV exception"; | ||||||
|             if(mException instanceof HttpException) return "HTTP violation"; |             if(mException instanceof HttpException) return "HTTP violation"; | ||||||
|             if(mException instanceof IOException) return "Unrecovered transport exception"; |             if(mException instanceof IOException) return "Unrecovered transport exception"; | ||||||
|             if(mException instanceof AccountUtils.AccountNotFoundException) { |             if(mException instanceof AccountNotFoundException) { | ||||||
|                 Account failedAccount = |                 Account failedAccount = | ||||||
|                         ((AccountUtils.AccountNotFoundException) mException).getFailedAccount(); |                         ((AccountNotFoundException) mException).getFailedAccount(); | ||||||
|                 return mException.getMessage() + " (" + |                 return mException.getMessage() + " (" + | ||||||
|                         (failedAccount != null ? failedAccount.name : "NULL") + ")"; |                         (failedAccount != null ? failedAccount.name : "NULL") + ")"; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -22,7 +22,7 @@ | |||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| package com.owncloud.android.lib.refactor.utils; | package com.owncloud.android.lib.refactor.account; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * @author masensio |  * @author masensio | ||||||
| @ -23,7 +23,7 @@ | |||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| package com.owncloud.android.lib.refactor.utils; | package com.owncloud.android.lib.refactor.account; | ||||||
| 
 | 
 | ||||||
| import android.accounts.Account; | import android.accounts.Account; | ||||||
| import android.accounts.AccountManager; | import android.accounts.AccountManager; | ||||||
| @ -33,16 +33,23 @@ import android.accounts.OperationCanceledException; | |||||||
| import android.content.Context; | import android.content.Context; | ||||||
| import android.net.Uri; | import android.net.Uri; | ||||||
| 
 | 
 | ||||||
| import com.owncloud.android.lib.common.OwnCloudClient; |  | ||||||
| import com.owncloud.android.lib.common.accounts.AccountTypeUtils; |  | ||||||
| import com.owncloud.android.lib.common.authentication.OwnCloudCredentials; |  | ||||||
| import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; | import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; | ||||||
| import com.owncloud.android.lib.common.utils.Log_OC; | import com.owncloud.android.lib.refactor.Log_OC; | ||||||
|  | import com.owncloud.android.lib.refactor.OCContext; | ||||||
|  | import com.owncloud.android.lib.refactor.authentication.OCCredentials; | ||||||
|  | import com.owncloud.android.lib.refactor.authentication.credentials.OCBasicCredentials; | ||||||
|  | import com.owncloud.android.lib.refactor.authentication.credentials.OCBearerCredentials; | ||||||
|  | import com.owncloud.android.lib.refactor.authentication.credentials.OCSamlSsoCredentials; | ||||||
|  | import com.owncloud.android.lib.refactor.exceptions.AccountNotFoundException; | ||||||
| import com.owncloud.android.lib.resources.status.OwnCloudVersion; | import com.owncloud.android.lib.resources.status.OwnCloudVersion; | ||||||
| 
 | 
 | ||||||
| import org.apache.commons.httpclient.Cookie; | import org.apache.commons.httpclient.auth.AuthenticationException; | ||||||
| 
 | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | import okhttp3.Cookie; | ||||||
| 
 | 
 | ||||||
| public class AccountUtils { | public class AccountUtils { | ||||||
| 
 | 
 | ||||||
| @ -59,7 +66,7 @@ public class AccountUtils { | |||||||
|     public static String getWebDavUrlForAccount(Context context, Account account) |     public static String getWebDavUrlForAccount(Context context, Account account) | ||||||
|         throws AccountNotFoundException { |         throws AccountNotFoundException { | ||||||
| 
 | 
 | ||||||
|         return getBaseUrlForAccount(context, account) + OwnCloudClient.WEBDAV_PATH_4_0; |         return getBaseUrlForAccount(context, account) + OCContext.WEBDAV_PATH_4_0; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -121,72 +128,64 @@ public class AccountUtils { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @return |      * @return OCCredentials | ||||||
|      * @throws IOException |      * @throws IOException | ||||||
|      * @throws AuthenticatorException |      * @throws AuthenticatorException | ||||||
|      * @throws OperationCanceledException |      * @throws OperationCanceledException | ||||||
|      */ |      */ | ||||||
|     public static OwnCloudCredentials getCredentialsForAccount(Context context, Account account) | 
 | ||||||
|  |     /** | ||||||
|  |      * | ||||||
|  |      * @param context an Android context | ||||||
|  |      * @param account the coresponding Android account | ||||||
|  |      * @return | ||||||
|  |      * @throws OperationCanceledException | ||||||
|  |      * @throws AuthenticatorException | ||||||
|  |      * @throws IOException | ||||||
|  |      */ | ||||||
|  |     public static OCCredentials getCredentialsForAccount(Context context, Account account) | ||||||
|         throws OperationCanceledException, AuthenticatorException, IOException { |         throws OperationCanceledException, AuthenticatorException, IOException { | ||||||
| 
 | 
 | ||||||
|         OwnCloudCredentials credentials = null; |         final AccountManager am = AccountManager.get(context); | ||||||
|         AccountManager am = AccountManager.get(context); |         final String supportsOAuth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2); | ||||||
| 
 |         final boolean isOauth2 = supportsOAuth2 != null && supportsOAuth2.equals("TRUE"); | ||||||
|         String supportsOAuth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2); |  | ||||||
|         boolean isOauth2 = supportsOAuth2 != null && supportsOAuth2.equals("TRUE"); |  | ||||||
| 
 |  | ||||||
|         String supportsSamlSSo = am.getUserData(account, |         String supportsSamlSSo = am.getUserData(account, | ||||||
|                 AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO); |                 AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO); | ||||||
| 
 |         final boolean isSamlSso = supportsSamlSSo != null && supportsSamlSSo.equals("TRUE"); | ||||||
|         boolean isSamlSso = supportsSamlSSo != null && supportsSamlSSo.equals("TRUE"); |         final String username = AccountUtils.getUsernameForAccount(account); | ||||||
| 
 |  | ||||||
|         String username = AccountUtils.getUsernameForAccount(account); |  | ||||||
|         OwnCloudVersion version = new OwnCloudVersion(am.getUserData(account, Constants.KEY_OC_VERSION)); |  | ||||||
| 
 | 
 | ||||||
|         if (isOauth2) { |         if (isOauth2) { | ||||||
|             String accessToken = am.blockingGetAuthToken( |             final String accessToken = am.blockingGetAuthToken( | ||||||
|                 account, |                 account, | ||||||
|                 AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), |                 AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), | ||||||
|                 false); |                 false); | ||||||
| 
 | 
 | ||||||
|             credentials = OwnCloudCredentialsFactory.newBearerCredentials(username, accessToken); |             return new OCBearerCredentials(username, accessToken); | ||||||
| 
 | 
 | ||||||
|         } else if (isSamlSso) { |         } else if (isSamlSso) { | ||||||
|             String accessToken = am.blockingGetAuthToken( | 
 | ||||||
|  |             try { | ||||||
|  |                 final String accessToken = am.blockingGetAuthToken( | ||||||
|                         account, |                         account, | ||||||
|                         AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), |                         AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), | ||||||
|                         false); |                         false); | ||||||
| 
 | 
 | ||||||
|             credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials(username, accessToken); |                 return new OCSamlSsoCredentials(username, accessToken, | ||||||
|  |                         Uri.parse(getBaseUrlForAccount(context, account))); | ||||||
|  |             } catch (AccountNotFoundException e) { | ||||||
|  |                 throw new AuthenticationException("Account not found", e); | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|         } else { |         } else { | ||||||
|             String password = am.blockingGetAuthToken( |             final String password = am.blockingGetAuthToken( | ||||||
|                 account, |                 account, | ||||||
|                 AccountTypeUtils.getAuthTokenTypePass(account.type), |                 AccountTypeUtils.getAuthTokenTypePass(account.type), | ||||||
|                 false); |                 false); | ||||||
| 
 | 
 | ||||||
|             credentials = OwnCloudCredentialsFactory.newBasicCredentials( |             return new OCBasicCredentials(username, password); | ||||||
|                 username, |         } | ||||||
|                 password, |  | ||||||
|                 version.isPreemptiveAuthenticationPreferred() |  | ||||||
|             ); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|         return credentials; |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     public static String buildAccountNameOld(Uri serverBaseUrl, String username) { |  | ||||||
|         if (serverBaseUrl.getScheme() == null) { |  | ||||||
|             serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString()); |  | ||||||
|         } |  | ||||||
|         String accountName = username + "@" + serverBaseUrl.getHost(); |  | ||||||
|         if (serverBaseUrl.getPort() >= 0) { |  | ||||||
|             accountName += ":" + serverBaseUrl.getPort(); |  | ||||||
|         } |  | ||||||
|         return accountName; |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     public static String buildAccountName(Uri serverBaseUrl, String username) { |     public static String buildAccountName(Uri serverBaseUrl, String username) { | ||||||
|         if (serverBaseUrl.getScheme() == null) { |         if (serverBaseUrl.getScheme() == null) { | ||||||
| @ -203,17 +202,18 @@ public class AccountUtils { | |||||||
|         return accountName; |         return accountName; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static void saveClient(OwnCloudClient client, Account savedAccount, Context context) { |     public static void saveCookies(List<Cookie> cookies, Account savedAccount, Context context) { | ||||||
| 
 | 
 | ||||||
|         // Account Manager |         // Account Manager | ||||||
|         AccountManager ac = AccountManager.get(context.getApplicationContext()); |         AccountManager ac = AccountManager.get(context.getApplicationContext()); | ||||||
| 
 | 
 | ||||||
|         if (client != null) { |         if (cookies != null && cookies.size() != 0) { | ||||||
|             String cookiesString = client.getCookiesString(); |             StringBuilder cookiesString = new StringBuilder(); | ||||||
|             if (!"".equals(cookiesString)) { |             for (Cookie cookie : cookies) { | ||||||
|                 ac.setUserData(savedAccount, Constants.KEY_COOKIES, cookiesString); |                 cookiesString.append(cookiesString + cookie.toString() + ";"); | ||||||
|                 // Log_OC.d(TAG, "Saving Cookies: "+ cookiesString ); |  | ||||||
|             } |             } | ||||||
|  | 
 | ||||||
|  |             ac.setUserData(savedAccount, Constants.KEY_COOKIES, cookiesString.toString()); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|     } |     } | ||||||
| @ -222,60 +222,39 @@ public class AccountUtils { | |||||||
|     /** |     /** | ||||||
|      *  Restore the client cookies persisted in an account stored in the system AccountManager. |      *  Restore the client cookies persisted in an account stored in the system AccountManager. | ||||||
|      * |      * | ||||||
|      * @param account           Stored account. |      * @param account | ||||||
|      * @param client            Client to restore cookies in. |      * @param context | ||||||
|      * @param context           Android context used to access the system AccountManager. |      * @return | ||||||
|  |      * @throws AccountsException | ||||||
|      */ |      */ | ||||||
|     public static void restoreCookies(Account account, OwnCloudClient client, Context context) { |     public static List<Cookie> getCookiesFromAccount(Account account, Context context) throws AccountsException { | ||||||
|         if (account == null) { |         if (account == null) { | ||||||
|             Log_OC.d(TAG, "Cannot restore cookie for null account"); |             Log_OC.d(TAG, "Cannot restore cookie for null account"); | ||||||
|  |             return new ArrayList<>(); | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|         } else { |  | ||||||
|         Log_OC.d(TAG, "Restoring cookies for " + account.name); |         Log_OC.d(TAG, "Restoring cookies for " + account.name); | ||||||
| 
 | 
 | ||||||
|             // Account Manager |         final AccountManager am = AccountManager.get(context.getApplicationContext()); | ||||||
|             AccountManager am = AccountManager.get(context.getApplicationContext()); |         final Uri serverUri = Uri.parse(getBaseUrlForAccount(context, account)); | ||||||
|  |         final String cookiesString = am.getUserData(account, Constants.KEY_COOKIES); | ||||||
|  |         final List<Cookie> cookies = new ArrayList<>(); | ||||||
| 
 | 
 | ||||||
|             Uri serverUri = (client.getBaseUri() != null) ? client.getBaseUri() : client.getWebdavUri(); |  | ||||||
| 
 |  | ||||||
|             String cookiesString = am.getUserData(account, Constants.KEY_COOKIES); |  | ||||||
|         if (cookiesString != null) { |         if (cookiesString != null) { | ||||||
|                 String[] cookies = cookiesString.split(";"); |             String[] rawCookies = cookiesString.split(";"); | ||||||
|                 if (cookies.length > 0) { |             for (String rawCookie : rawCookies) { | ||||||
|                     for (int i = 0; i < cookies.length; i++) { |                 final int equalPos = rawCookie.indexOf('='); | ||||||
|                         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); |                 cookies.add(new Cookie.Builder() | ||||||
|  |                         .name(rawCookie.substring(0, equalPos)) | ||||||
|  |                         .value(rawCookie.substring(equalPos + 1)) | ||||||
|  |                         .domain(serverUri.getHost()) | ||||||
|  |                         .path(serverUri.getPath()) | ||||||
|  |                         .build()); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |         return cookies; | ||||||
|     } |     } | ||||||
|         } |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public static class AccountNotFoundException extends AccountsException { |  | ||||||
| 
 |  | ||||||
|         /** |  | ||||||
|          * Generated - should be refreshed every time the class changes!! |  | ||||||
|          */ |  | ||||||
|         private static final long serialVersionUID = -1684392454798508693L; |  | ||||||
| 
 |  | ||||||
|         private Account mFailedAccount; |  | ||||||
| 
 |  | ||||||
|         public AccountNotFoundException(Account failedAccount, String message, Throwable cause) { |  | ||||||
|             super(message, cause); |  | ||||||
|             mFailedAccount = failedAccount; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         public Account getFailedAccount() { |  | ||||||
|             return mFailedAccount; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
|     public static class Constants { |     public static class Constants { | ||||||
|         /** |         /** | ||||||
							
								
								
									
										155
									
								
								src/com/owncloud/android/lib/refactor/account/OCAccount.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										155
									
								
								src/com/owncloud/android/lib/refactor/account/OCAccount.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,155 @@ | |||||||
|  | /* 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.refactor.account; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | import android.accounts.Account; | ||||||
|  | import android.accounts.AccountManager; | ||||||
|  | import android.accounts.AuthenticatorException; | ||||||
|  | import android.accounts.OperationCanceledException; | ||||||
|  | import android.content.Context; | ||||||
|  | import android.net.Uri; | ||||||
|  | 
 | ||||||
|  | import com.owncloud.android.lib.refactor.authentication.OCCredentials; | ||||||
|  | import com.owncloud.android.lib.refactor.authentication.credentials.OCAnonymousCredentials; | ||||||
|  | import com.owncloud.android.lib.refactor.exceptions.AccountNotFoundException; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * OwnCloud Account | ||||||
|  |  *  | ||||||
|  |  * @author David A. Velasco | ||||||
|  |  */ | ||||||
|  | public class OCAccount { | ||||||
|  | 
 | ||||||
|  |     private Uri mBaseUri; | ||||||
|  |     private OCCredentials mCredentials; | ||||||
|  |     private String mDisplayName; | ||||||
|  |     private String mSavedAccountName; | ||||||
|  |     private Account mSavedAccount; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Constructor for already saved OC accounts. | ||||||
|  |      * | ||||||
|  |      * Do not use for anonymous credentials. | ||||||
|  |      */ | ||||||
|  |     public OCAccount(Account savedAccount, Context context) throws AccountNotFoundException { | ||||||
|  |         if (savedAccount == null) { | ||||||
|  |             throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null"); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (context == null) { | ||||||
|  |             throw new IllegalArgumentException("Parameter 'context' cannot be null"); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         mSavedAccount = savedAccount; | ||||||
|  |         mSavedAccountName = savedAccount.name; | ||||||
|  |         mCredentials = null;    // load of credentials is delayed | ||||||
|  | 
 | ||||||
|  |         AccountManager ama = AccountManager.get(context.getApplicationContext()); | ||||||
|  |         String baseUrl = ama.getUserData(mSavedAccount, AccountUtils.Constants.KEY_OC_BASE_URL); | ||||||
|  |         if (baseUrl == null ) { | ||||||
|  |             throw new AccountNotFoundException(mSavedAccount, "Account not found", null); | ||||||
|  |         } | ||||||
|  |         mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, mSavedAccount)); | ||||||
|  |         mDisplayName = ama.getUserData(mSavedAccount, AccountUtils.Constants.KEY_DISPLAY_NAME); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Constructor for non yet saved OC accounts. | ||||||
|  |      * | ||||||
|  |      * @param baseUri           URI to the OC server to get access to. | ||||||
|  |      * @param credentials       Credentials to authenticate in the server. NULL is valid for anonymous credentials. | ||||||
|  |      */ | ||||||
|  |     public OCAccount(Uri baseUri, OCCredentials credentials) { | ||||||
|  |         if (baseUri == null) { | ||||||
|  |             throw new IllegalArgumentException("Parameter 'baseUri' cannot be null"); | ||||||
|  |         } | ||||||
|  |         mSavedAccount = null; | ||||||
|  |         mSavedAccountName = null; | ||||||
|  |         mBaseUri = baseUri; | ||||||
|  |         mCredentials = credentials != null | ||||||
|  |                 ? credentials | ||||||
|  |                 : new OCAnonymousCredentials(); | ||||||
|  |         String username = mCredentials.getUsername(); | ||||||
|  |         if (username != null) { | ||||||
|  |             mSavedAccountName = AccountUtils.buildAccountName(mBaseUri, username); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Method for deferred load of account attributes from AccountManager | ||||||
|  |      * | ||||||
|  |      * @param context | ||||||
|  |      * @throws AccountNotFoundException | ||||||
|  |      * @throws AuthenticatorException | ||||||
|  |      * @throws IOException | ||||||
|  |      * @throws OperationCanceledException | ||||||
|  |      */ | ||||||
|  |     public void loadCredentials(Context context) | ||||||
|  |         throws AccountNotFoundException, AuthenticatorException, | ||||||
|  |                 IOException, OperationCanceledException { | ||||||
|  | 
 | ||||||
|  |         if (context == null) { | ||||||
|  |             throw new IllegalArgumentException("Parameter 'context' cannot be null"); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (mSavedAccount != null) { | ||||||
|  |             mCredentials = AccountUtils.getCredentialsForAccount(context, mSavedAccount); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Uri getBaseUri() { | ||||||
|  |         return mBaseUri; | ||||||
|  |     } | ||||||
|  |              | ||||||
|  |     public OCCredentials getCredentials() { | ||||||
|  |         return mCredentials; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     public String getName() { | ||||||
|  |     	return mSavedAccountName; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Account getSavedAccount() { | ||||||
|  |         return mSavedAccount; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getDisplayName() { | ||||||
|  |         if (mDisplayName != null && mDisplayName.length() > 0) { | ||||||
|  |             return mDisplayName; | ||||||
|  |         } else if (mCredentials != null) { | ||||||
|  |             return mCredentials.getUsername(); | ||||||
|  |         } else if (mSavedAccount != null) { | ||||||
|  |             return AccountUtils.getUsernameForAccount(mSavedAccount); | ||||||
|  |         } else { | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -84,13 +84,6 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation { | |||||||
|     @Override |     @Override | ||||||
|     public RemoteOperationResult exec() { |     public RemoteOperationResult exec() { | ||||||
|         try { |         try { | ||||||
| 
 |  | ||||||
|             final Uri uri = getOCContext() |  | ||||||
|                     .getBaseUri() |  | ||||||
|                     .buildUpon() |  | ||||||
|                     .appendEncodedPath(mAccessTokenEndpointPath) |  | ||||||
|                     .build(); |  | ||||||
| 
 |  | ||||||
|             final RequestBody requestBody = new MultipartBody.Builder() |             final RequestBody requestBody = new MultipartBody.Builder() | ||||||
|                     .setType(MultipartBody.FORM) |                     .setType(MultipartBody.FORM) | ||||||
|                     .addFormDataPart(OAuth2Constants.KEY_GRANT_TYPE, mGrantType) |                     .addFormDataPart(OAuth2Constants.KEY_GRANT_TYPE, mGrantType) | ||||||
| @ -100,7 +93,10 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation { | |||||||
|                     .build(); |                     .build(); | ||||||
| 
 | 
 | ||||||
|             final Request request = getRequestBuilder() |             final Request request = getRequestBuilder() | ||||||
|                     .url(uri.toString()) |                     .url(getBaseUriBuilder() | ||||||
|  |                             .appendEncodedPath(mAccessTokenEndpointPath) | ||||||
|  |                             .build() | ||||||
|  |                             .toString()) | ||||||
|                     .method("POST", requestBody) |                     .method("POST", requestBody) | ||||||
|                     .build(); |                     .build(); | ||||||
| 
 | 
 | ||||||
| @ -109,6 +105,7 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation { | |||||||
|                     .execute(); |                     .execute(); | ||||||
| 
 | 
 | ||||||
|             final String responseData = response.body().string(); |             final String responseData = response.body().string(); | ||||||
|  |              | ||||||
|             if (responseData != null && responseData.length() > 0) { |             if (responseData != null && responseData.length() > 0) { | ||||||
|                 JSONObject tokenJson = new JSONObject(responseData); |                 JSONObject tokenJson = new JSONObject(responseData); | ||||||
|                 Map<String, String> accessTokenResult = |                 Map<String, String> accessTokenResult = | ||||||
|  | |||||||
| @ -79,8 +79,6 @@ public class OAuth2RefreshAccessTokenOperation extends RemoteOperation { | |||||||
|     @Override |     @Override | ||||||
|     public RemoteOperationResult exec() { |     public RemoteOperationResult exec() { | ||||||
|         try { |         try { | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|             final RequestBody requestBody = new MultipartBody.Builder() |             final RequestBody requestBody = new MultipartBody.Builder() | ||||||
|                     .setType(MultipartBody.FORM) |                     .setType(MultipartBody.FORM) | ||||||
|                     .addFormDataPart(OAuth2Constants.KEY_GRANT_TYPE, |                     .addFormDataPart(OAuth2Constants.KEY_GRANT_TYPE, | ||||||
| @ -90,20 +88,15 @@ public class OAuth2RefreshAccessTokenOperation extends RemoteOperation { | |||||||
|                     .build(); |                     .build(); | ||||||
| 
 | 
 | ||||||
|             final Request request = getRequestBuilder() |             final Request request = getRequestBuilder() | ||||||
|                     .url(getOCContext().getBaseUri().buildUpon() |                     .url(getBaseUriBuilder() | ||||||
|                             .appendEncodedPath(mAccessTokenEndpointPath) |                             .appendEncodedPath(mAccessTokenEndpointPath) | ||||||
|                             .build() |                             .build() | ||||||
|                             .toString()) |                             .toString()) | ||||||
|                     .method("POST", requestBody) |                     .method("POST", requestBody) | ||||||
|                     .build(); |                     .build(); | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|             OCCredentials oauthCredentials = new OCBasicCredentials( |  | ||||||
|                     mClientId, |  | ||||||
|                     mClientSecret |  | ||||||
|             ); |  | ||||||
| 
 |  | ||||||
|             final Response response = getClient().newCall(request).execute(); |             final Response response = getClient().newCall(request).execute(); | ||||||
|  | 
 | ||||||
|             final String responseData = response.body().string(); |             final String responseData = response.body().string(); | ||||||
|             Log_OC.d(TAG, "OAUTH2: raw response from POST TOKEN: " + responseData); |             Log_OC.d(TAG, "OAUTH2: raw response from POST TOKEN: " + responseData); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -0,0 +1,23 @@ | |||||||
|  | package com.owncloud.android.lib.refactor.exceptions; | ||||||
|  | 
 | ||||||
|  | import android.accounts.Account; | ||||||
|  | import android.accounts.AccountsException; | ||||||
|  | 
 | ||||||
|  | public class AccountNotFoundException extends AccountsException { | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Generated - should be refreshed every time the class changes!! | ||||||
|  |      */ | ||||||
|  |     private static final long serialVersionUID = -1684392454778508693L; | ||||||
|  | 
 | ||||||
|  |     private Account mFailedAccount; | ||||||
|  | 
 | ||||||
|  |     public AccountNotFoundException(Account failedAccount, String message, Throwable cause) { | ||||||
|  |         super(message, cause); | ||||||
|  |         mFailedAccount = failedAccount; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Account getFailedAccount() { | ||||||
|  |         return mFailedAccount; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -13,6 +13,7 @@ public class PropfindOperation extends RemoteOperation { | |||||||
|     @Override |     @Override | ||||||
|     public RemoteOperationResult exec() { |     public RemoteOperationResult exec() { | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user