OC-1868: Remove dependences from accountType and authorities

This commit is contained in:
masensio 2013-11-08 14:41:32 +01:00
parent f297a37669
commit 629f463748
7 changed files with 185 additions and 221 deletions

View file

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="account_type">owncloud</string>
<string name="authority">org.owncloud</string>
</resources>

View file

@ -1,62 +0,0 @@
package com.owncloud.android.oc_framework;
import android.app.Application;
import android.content.Context;
public class MainApp extends Application {
private static Context mContext;
public void onCreate(){
super.onCreate();
MainApp.mContext = getApplicationContext();
}
public static Context getAppContext() {
return MainApp.mContext;
}
// Methods to obtain Strings referring app_name
// From AccountAuthenticator
// public static final String ACCOUNT_TYPE = "owncloud";
public static String getAccountType() {
return getAppContext().getResources().getString(R.string.account_type);
}
// From AccountAuthenticator
// public static final String AUTHORITY = "org.owncloud";
public static String getAuthority() {
return getAppContext().getResources().getString(R.string.authority);
}
// From AccountAuthenticator
// public static final String AUTH_TOKEN_TYPE = "org.owncloud";
public static String getAuthTokenType() {
return getAppContext().getResources().getString(R.string.authority);
}
// From AccountAuthenticator
// public static final String AUTH_TOKEN_TYPE_PASSWORD = "owncloud.password";
public static String getAuthTokenTypePass() {
return getAppContext().getResources().getString(R.string.account_type) + ".password";
}
// From AccountAuthenticator
// public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN = "owncloud.oauth2.access_token";
public static String getAuthTokenTypeAccessToken() {
return getAppContext().getResources().getString(R.string.account_type) + ".oauth2.access_token";
}
// From AccountAuthenticator
// public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN = "owncloud.oauth2.refresh_token";
public static String getAuthTokenTypeRefreshToken() {
return getAppContext().getResources().getString(R.string.account_type) + ".oauth2.refresh_token";
}
// From AccountAuthenticator
// public static final String AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE = "owncloud.saml.web_sso.session_cookie";
public static String getAuthTokenTypeSamlSessionCookie() {
return getAppContext().getResources().getString(R.string.account_type) + ".saml.web_sso.session_cookie";
}
}

View file

@ -0,0 +1,33 @@
package com.owncloud.android.oc_framework.authentication;
import android.accounts.Account;
public class AccountTypeUtils {
// Methods to obtain Strings referring account_type
public static String getAccountType(Account account) {
return account.type;
}
// public static final String AUTH_TOKEN_TYPE_PASSWORD = "owncloud.password";
public static String getAuthTokenTypePass(Account account) {
return account.type + ".password";
}
// public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN = "owncloud.oauth2.access_token";
public static String getAuthTokenTypeAccessToken(Account account) {
return account.type + ".oauth2.access_token";
}
// public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN = "owncloud.oauth2.refresh_token";
public static String getAuthTokenTypeRefreshToken(Account account) {
return account.type + ".oauth2.refresh_token";
}
// public static final String AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE = "owncloud.saml.web_sso.session_cookie";
public static String getAuthTokenTypeSamlSessionCookie(Account account) {
return account.type + ".saml.web_sso.session_cookie";
}
}

View file

@ -19,15 +19,12 @@
package com.owncloud.android.oc_framework.authentication; package com.owncloud.android.oc_framework.authentication;
import com.owncloud.android.oc_framework.MainApp;
import com.owncloud.android.oc_framework.OwnCloudVersion; import com.owncloud.android.oc_framework.OwnCloudVersion;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.accounts.AccountsException; import android.accounts.AccountsException;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class AccountUtils { public class AccountUtils {
public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php"; public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
@ -39,93 +36,93 @@ public class AccountUtils {
public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php"; public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";
public static final String STATUS_PATH = "/status.php"; public static final String STATUS_PATH = "/status.php";
/** // /**
* Can be used to get the currently selected ownCloud {@link Account} in the // * Can be used to get the currently selected ownCloud {@link Account} in the
* application preferences. // * application preferences.
* // *
* @param context The current application {@link Context} // * @param context The current application {@link Context}
* @return The ownCloud {@link Account} currently saved in preferences, or the first // * @return The ownCloud {@link Account} currently saved in preferences, or the first
* {@link Account} available, if valid (still registered in the system as ownCloud // * {@link Account} available, if valid (still registered in the system as ownCloud
* account). If none is available and valid, returns null. // * account). If none is available and valid, returns null.
*/ // */
public static Account getCurrentOwnCloudAccount(Context context) { // public static Account getCurrentOwnCloudAccount(Context context) {
Account[] ocAccounts = AccountManager.get(context).getAccountsByType( // Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
MainApp.getAccountType()); // MainApp.getAccountType());
Account defaultAccount = null; // Account defaultAccount = null;
//
SharedPreferences appPreferences = PreferenceManager // SharedPreferences appPreferences = PreferenceManager
.getDefaultSharedPreferences(context); // .getDefaultSharedPreferences(context);
String accountName = appPreferences // String accountName = appPreferences
.getString("select_oc_account", null); // .getString("select_oc_account", null);
//
// account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager // // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
if (accountName != null) { // if (accountName != null) {
for (Account account : ocAccounts) { // for (Account account : ocAccounts) {
if (account.name.equals(accountName)) { // if (account.name.equals(accountName)) {
defaultAccount = account; // defaultAccount = account;
break; // break;
} // }
} // }
} // }
//
if (defaultAccount == null && ocAccounts.length != 0) { // if (defaultAccount == null && ocAccounts.length != 0) {
// take first account as fallback // // take first account as fallback
defaultAccount = ocAccounts[0]; // defaultAccount = ocAccounts[0];
} // }
//
return defaultAccount; // return defaultAccount;
} // }
//
//
public static boolean exists(Account account, Context context) { // public static boolean exists(Account account, Context context) {
Account[] ocAccounts = AccountManager.get(context).getAccountsByType( // Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
MainApp.getAccountType()); // MainApp.getAccountType());
//
if (account != null && account.name != null) { // if (account != null && account.name != null) {
for (Account ac : ocAccounts) { // for (Account ac : ocAccounts) {
if (ac.name.equals(account.name)) { // if (ac.name.equals(account.name)) {
return true; // return true;
} // }
} // }
} // }
return false; // return false;
} // }
//
//
/** // /**
* Checks, whether or not there are any ownCloud accounts setup. // * Checks, whether or not there are any ownCloud accounts setup.
* // *
* @return true, if there is at least one account. // * @return true, if there is at least one account.
*/ // */
public static boolean accountsAreSetup(Context context) { // public static boolean accountsAreSetup(Context context) {
AccountManager accMan = AccountManager.get(context); // AccountManager accMan = AccountManager.get(context);
Account[] accounts = accMan // Account[] accounts = accMan
.getAccountsByType(MainApp.getAccountType()); // .getAccountsByType(MainApp.getAccountType());
return accounts.length > 0; // return accounts.length > 0;
} // }
//
//
public static boolean setCurrentOwnCloudAccount(Context context, String accountName) { // public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {
boolean result = false; // boolean result = false;
if (accountName != null) { // if (accountName != null) {
Account[] ocAccounts = AccountManager.get(context).getAccountsByType( // Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
MainApp.getAccountType()); // MainApp.getAccountType());
boolean found = false; // boolean found = false;
for (Account account : ocAccounts) { // for (Account account : ocAccounts) {
found = (account.name.equals(accountName)); // found = (account.name.equals(accountName));
if (found) { // if (found) {
SharedPreferences.Editor appPrefs = PreferenceManager // SharedPreferences.Editor appPrefs = PreferenceManager
.getDefaultSharedPreferences(context).edit(); // .getDefaultSharedPreferences(context).edit();
appPrefs.putString("select_oc_account", accountName); // appPrefs.putString("select_oc_account", accountName);
//
appPrefs.commit(); // appPrefs.commit();
result = true; // result = true;
break; // break;
} // }
} // }
} // }
return result; // return result;
} // }
/** /**
* *
@ -151,32 +148,32 @@ public class AccountUtils {
return null; return null;
} }
/** // /**
* Returns the proper URL path to access the WebDAV interface of an ownCloud server, // * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
* according to its version and the authorization method used. // * according to its version and the authorization method used.
* // *
* @param version Version of ownCloud server. // * @param version Version of ownCloud server.
* @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}. // * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
* @return WebDAV path for given OC version and authorization method, null if OC version is unknown. // * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
*/ // */
public static String getWebdavPath(OwnCloudVersion version, String authTokenType) { // public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
if (version != null) { // if (version != null) {
if (MainApp.getAuthTokenTypeAccessToken().equals(authTokenType)) { // if (MainApp.getAuthTokenTypeAccessToken().equals(authTokenType)) {
return ODAV_PATH; // return ODAV_PATH;
} // }
if (MainApp.getAuthTokenTypeSamlSessionCookie().equals(authTokenType)) { // if (MainApp.getAuthTokenTypeSamlSessionCookie().equals(authTokenType)) {
return SAML_SSO_PATH; // return SAML_SSO_PATH;
} // }
if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0) // if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
return WEBDAV_PATH_4_0; // return WEBDAV_PATH_4_0;
if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0 // if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
|| version.compareTo(OwnCloudVersion.owncloud_v2) >= 0) // || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
return WEBDAV_PATH_2_0; // return WEBDAV_PATH_2_0;
if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0) // if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
return WEBDAV_PATH_1_2; // return WEBDAV_PATH_1_2;
} // }
return null; // return null;
} // }
/** /**
* Constructs full url to host and webdav resource basing on host version * Constructs full url to host and webdav resource basing on host version

View file

@ -1,4 +1,3 @@
package com.owncloud.android.oc_framework.network;
/* ownCloud Android client application /* ownCloud Android client application
* Copyright (C) 2012-2013 ownCloud Inc. * Copyright (C) 2012-2013 ownCloud Inc.
* *
@ -15,7 +14,7 @@ package com.owncloud.android.oc_framework.network;
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
package com.owncloud.android.oc_framework.network;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -37,12 +36,13 @@ import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier; import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier;
import org.apache.http.conn.ssl.X509HostnameVerifier; import org.apache.http.conn.ssl.X509HostnameVerifier;
import com.owncloud.android.oc_framework.MainApp;
import com.owncloud.android.oc_framework.authentication.AccountAuthenticatorConstants; import com.owncloud.android.oc_framework.authentication.AccountAuthenticatorConstants;
import com.owncloud.android.oc_framework.authentication.AccountTypeUtils;
import com.owncloud.android.oc_framework.authentication.AccountUtils; import com.owncloud.android.oc_framework.authentication.AccountUtils;
import com.owncloud.android.oc_framework.authentication.AccountUtils.AccountNotFoundException; import com.owncloud.android.oc_framework.authentication.AccountUtils.AccountNotFoundException;
import com.owncloud.android.oc_framework.network.webdav.WebdavClient; import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture; import android.accounts.AccountManagerFuture;
@ -87,62 +87,62 @@ public class OwnCloudClientUtils {
* @throws IOException If there was some I/O error while getting the authorization token for the account. * @throws IOException If there was some I/O error while getting the authorization token for the account.
* @throws AccountNotFoundException If 'account' is unknown for the AccountManager * @throws AccountNotFoundException If 'account' is unknown for the AccountManager
*/ */
public static WebdavClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { public static WebdavClient createOwnCloudClient (Account account, Context appContext, String authorities) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
//Log_OC.d(TAG, "Creating WebdavClient associated to " + account.name); //Log_OC.d(TAG, "Creating WebdavClient associated to " + account.name);
Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
AccountManager am = AccountManager.get(appContext); AccountManager am = AccountManager.get(appContext);
boolean isOauth2 = am.getUserData(account, AccountAuthenticatorConstants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here boolean isOauth2 = am.getUserData(account, AccountAuthenticatorConstants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
boolean isSamlSso = am.getUserData(account, AccountAuthenticatorConstants.KEY_SUPPORTS_SAML_WEB_SSO) != null; boolean isSamlSso = am.getUserData(account, AccountAuthenticatorConstants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso); WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso, authorities);
if (isOauth2) { if (isOauth2) {
String accessToken = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypeAccessToken(), false); String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account), false);
client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token client.setBearerCredentials(accessToken, authorities); // TODO not assume that the access token is a bearer token
} else if (isSamlSso) { // TODO avoid a call to getUserData here } else if (isSamlSso) { // TODO avoid a call to getUserData here
String accessToken = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypeSamlSessionCookie(), false); String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account), false);
client.setSsoSessionCookie(accessToken); client.setSsoSessionCookie(accessToken, authorities);
} else { } else {
String username = account.name.substring(0, account.name.lastIndexOf('@')); String username = account.name.substring(0, account.name.lastIndexOf('@'));
//String password = am.getPassword(account); //String password = am.getPassword(account);
String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), false); String password = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account), false);
client.setBasicCredentials(username, password); client.setBasicCredentials(username, password, authorities);
} }
return client; return client;
} }
public static WebdavClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { public static WebdavClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity, String authorities) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
AccountManager am = AccountManager.get(appContext); AccountManager am = AccountManager.get(appContext);
boolean isOauth2 = am.getUserData(account, AccountAuthenticatorConstants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here boolean isOauth2 = am.getUserData(account, AccountAuthenticatorConstants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
boolean isSamlSso = am.getUserData(account, AccountAuthenticatorConstants.KEY_SUPPORTS_SAML_WEB_SSO) != null; boolean isSamlSso = am.getUserData(account, AccountAuthenticatorConstants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso); WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso, authorities);
if (isOauth2) { // TODO avoid a call to getUserData here if (isOauth2) { // TODO avoid a call to getUserData here
AccountManagerFuture<Bundle> future = am.getAuthToken(account, MainApp.getAuthTokenTypeAccessToken(), null, currentActivity, null, null); AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account), null, currentActivity, null, null);
Bundle result = future.getResult(); Bundle result = future.getResult();
String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN); String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
if (accessToken == null) throw new AuthenticatorException("WTF!"); if (accessToken == null) throw new AuthenticatorException("WTF!");
client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token client.setBearerCredentials(accessToken, authorities); // TODO not assume that the access token is a bearer token
} else if (isSamlSso) { // TODO avoid a call to getUserData here } else if (isSamlSso) { // TODO avoid a call to getUserData here
AccountManagerFuture<Bundle> future = am.getAuthToken(account, MainApp.getAuthTokenTypeSamlSessionCookie(), null, currentActivity, null, null); AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account), null, currentActivity, null, null);
Bundle result = future.getResult(); Bundle result = future.getResult();
String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN); String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
if (accessToken == null) throw new AuthenticatorException("WTF!"); if (accessToken == null) throw new AuthenticatorException("WTF!");
client.setSsoSessionCookie(accessToken); client.setSsoSessionCookie(accessToken, authorities);
} else { } else {
String username = account.name.substring(0, account.name.lastIndexOf('@')); String username = account.name.substring(0, account.name.lastIndexOf('@'));
//String password = am.getPassword(account); //String password = am.getPassword(account);
//String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), false); //String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), false);
AccountManagerFuture<Bundle> future = am.getAuthToken(account, MainApp.getAuthTokenTypePass(), null, currentActivity, null, null); AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account), null, currentActivity, null, null);
Bundle result = future.getResult(); Bundle result = future.getResult();
String password = result.getString(AccountManager.KEY_AUTHTOKEN); String password = result.getString(AccountManager.KEY_AUTHTOKEN);
client.setBasicCredentials(username, password); client.setBasicCredentials(username, password, authorities);
} }
return client; return client;
@ -155,7 +155,7 @@ public class OwnCloudClientUtils {
* @param context Android context where the WebdavClient is being created. * @param context Android context where the WebdavClient is being created.
* @return A WebdavClient object ready to be used * @return A WebdavClient object ready to be used
*/ */
public static WebdavClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects) { public static WebdavClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects, String authoritities) {
try { try {
registerAdvancedSslContext(true, context); registerAdvancedSslContext(true, context);
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
@ -165,7 +165,7 @@ public class OwnCloudClientUtils {
Log.e(TAG, "The local server truststore could not be read. Default SSL management in the system will be used for HTTPS connections", e); Log.e(TAG, "The local server truststore could not be read. Default SSL management in the system will be used for HTTPS connections", e);
} }
WebdavClient client = new WebdavClient(getMultiThreadedConnManager()); WebdavClient client = new WebdavClient(getMultiThreadedConnManager(), authoritities);
client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
client.setBaseUri(uri); client.setBaseUri(uri);

View file

@ -41,7 +41,6 @@ import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.params.CoreProtocolPNames; import org.apache.http.params.CoreProtocolPNames;
import com.owncloud.android.oc_framework.MainApp;
import com.owncloud.android.oc_framework.network.BearerAuthScheme; import com.owncloud.android.oc_framework.network.BearerAuthScheme;
import com.owncloud.android.oc_framework.network.BearerCredentials; import com.owncloud.android.oc_framework.network.BearerCredentials;
@ -65,17 +64,17 @@ public class WebdavClient extends HttpClient {
/** /**
* Constructor * Constructor
*/ */
public WebdavClient(HttpConnectionManager connectionMgr) { public WebdavClient(HttpConnectionManager connectionMgr, String authorities) {
super(connectionMgr); super(connectionMgr);
Log.d(TAG, "Creating WebdavClient"); Log.d(TAG, "Creating WebdavClient");
getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT); getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
mFollowRedirects = true; mFollowRedirects = true;
mSsoSessionCookie = null; mSsoSessionCookie = null;
mAuthTokenType = MainApp.getAuthTokenTypePass(); mAuthTokenType = authorities; // MainApp.getAuthTokenTypePass();
} }
public void setBearerCredentials(String accessToken) { public void setBearerCredentials(String accessToken, String authorities) {
AuthPolicy.registerAuthScheme(BearerAuthScheme.AUTH_POLICY, BearerAuthScheme.class); AuthPolicy.registerAuthScheme(BearerAuthScheme.AUTH_POLICY, BearerAuthScheme.class);
List<String> authPrefs = new ArrayList<String>(1); List<String> authPrefs = new ArrayList<String>(1);
@ -85,10 +84,10 @@ public class WebdavClient extends HttpClient {
mCredentials = new BearerCredentials(accessToken); mCredentials = new BearerCredentials(accessToken);
getState().setCredentials(AuthScope.ANY, mCredentials); getState().setCredentials(AuthScope.ANY, mCredentials);
mSsoSessionCookie = null; mSsoSessionCookie = null;
mAuthTokenType = MainApp.getAuthTokenTypeAccessToken(); mAuthTokenType = authorities;// MainApp.getAuthTokenTypeAccessToken();
} }
public void setBasicCredentials(String username, String password) { public void setBasicCredentials(String username, String password, String authorities) {
List<String> authPrefs = new ArrayList<String>(1); List<String> authPrefs = new ArrayList<String>(1);
authPrefs.add(AuthPolicy.BASIC); authPrefs.add(AuthPolicy.BASIC);
getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
@ -97,15 +96,15 @@ public class WebdavClient extends HttpClient {
mCredentials = new UsernamePasswordCredentials(username, password); mCredentials = new UsernamePasswordCredentials(username, password);
getState().setCredentials(AuthScope.ANY, mCredentials); getState().setCredentials(AuthScope.ANY, mCredentials);
mSsoSessionCookie = null; mSsoSessionCookie = null;
mAuthTokenType = MainApp.getAuthTokenTypePass(); mAuthTokenType = authorities; //MainApp.getAuthTokenTypePass();
} }
public void setSsoSessionCookie(String accessToken) { public void setSsoSessionCookie(String accessToken, String authorities) {
getParams().setAuthenticationPreemptive(false); getParams().setAuthenticationPreemptive(false);
getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
mSsoSessionCookie = accessToken; mSsoSessionCookie = accessToken;
mCredentials = null; mCredentials = null;
mAuthTokenType = MainApp.getAuthTokenTypeSamlSessionCookie(); mAuthTokenType = authorities; //MainApp.getAuthTokenTypeSamlSessionCookie();
} }

View file

@ -20,7 +20,6 @@ import java.io.IOException;
import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Credentials;
import com.owncloud.android.oc_framework.MainApp;
import com.owncloud.android.oc_framework.network.BearerCredentials; import com.owncloud.android.oc_framework.network.BearerCredentials;
import com.owncloud.android.oc_framework.network.OwnCloudClientUtils; import com.owncloud.android.oc_framework.network.OwnCloudClientUtils;
import com.owncloud.android.oc_framework.network.webdav.WebdavClient; import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
@ -50,6 +49,9 @@ public abstract class RemoteOperation implements Runnable {
/** ownCloud account in the remote ownCloud server to operate */ /** ownCloud account in the remote ownCloud server to operate */
private Account mAccount = null; private Account mAccount = null;
/** Authoritities */
private String mAuthorities;
/** Android Application context */ /** Android Application context */
private Context mContext = null; private Context mContext = null;
@ -83,7 +85,7 @@ public abstract class RemoteOperation implements Runnable {
* @param context Android context for the component calling the method. * @param context Android context for the component calling the method.
* @return Result of the operation. * @return Result of the operation.
*/ */
public final RemoteOperationResult execute(Account account, Context context) { public final RemoteOperationResult execute(Account account, Context context, String authorities) {
if (account == null) if (account == null)
throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account"); throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
if (context == null) if (context == null)
@ -91,11 +93,12 @@ public abstract class RemoteOperation implements Runnable {
mAccount = account; mAccount = account;
mContext = context.getApplicationContext(); mContext = context.getApplicationContext();
try { try {
mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext); mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext, authorities);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Error while trying to access to " + mAccount.name, e); Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
return new RemoteOperationResult(e); return new RemoteOperationResult(e);
} }
mAuthorities = authorities;
return run(mClient); return run(mClient);
} }
@ -215,9 +218,9 @@ public abstract class RemoteOperation implements Runnable {
if (mClient == null) { if (mClient == null) {
if (mAccount != null && mContext != null) { if (mAccount != null && mContext != null) {
if (mCallerActivity != null) { if (mCallerActivity != null) {
mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext, mCallerActivity); mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext, mCallerActivity, mAuthorities);
} else { } else {
mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext); mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext, mAuthorities);
} }
} else { } else {
throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account"); throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
@ -249,9 +252,9 @@ public abstract class RemoteOperation implements Runnable {
boolean bearerAuthorization = (cred != null && cred instanceof BearerCredentials); boolean bearerAuthorization = (cred != null && cred instanceof BearerCredentials);
boolean samlBasedSsoAuthorization = (cred == null && ssoSessionCookie != null); boolean samlBasedSsoAuthorization = (cred == null && ssoSessionCookie != null);
if (bearerAuthorization) { if (bearerAuthorization) {
am.invalidateAuthToken(MainApp.getAccountType(), ((BearerCredentials)cred).getAccessToken()); am.invalidateAuthToken(mAccount.type, ((BearerCredentials)cred).getAccessToken());
} else if (samlBasedSsoAuthorization ) { } else if (samlBasedSsoAuthorization ) {
am.invalidateAuthToken(MainApp.getAccountType(), ssoSessionCookie); am.invalidateAuthToken(mAccount.type, ssoSessionCookie);
} else { } else {
am.clearPassword(mAccount); am.clearPassword(mAccount);
} }