X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/48f13c8adc5c4b9bc4ca96bf13939a7d7cfae562..7d84fd0c9f15227bc65a2ae00a74e1cfd8f8d33b:/src/com/owncloud/android/network/OwnCloudClientUtils.java diff --git a/src/com/owncloud/android/network/OwnCloudClientUtils.java b/src/com/owncloud/android/network/OwnCloudClientUtils.java index a76cd4a2..c4462188 100644 --- a/src/com/owncloud/android/network/OwnCloudClientUtils.java +++ b/src/com/owncloud/android/network/OwnCloudClientUtils.java @@ -34,22 +34,28 @@ import javax.net.ssl.TrustManager; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier; +import org.apache.http.conn.ssl.X509HostnameVerifier; import com.owncloud.android.AccountUtils; import com.owncloud.android.authenticator.AccountAuthenticator; -import com.owncloud.android.utils.OwnCloudVersion; import eu.alefzero.webdav.WebdavClient; import android.accounts.Account; import android.accounts.AccountManager; +import android.accounts.AccountManagerFuture; +import android.accounts.AuthenticatorException; +import android.accounts.OperationCanceledException; +import android.app.Activity; import android.content.Context; import android.net.Uri; +import android.os.Bundle; import android.util.Log; public class OwnCloudClientUtils { - final private static String TAG = "OwnCloudClientFactory"; + final private static String TAG = OwnCloudClientUtils.class.getSimpleName(); /** Default timeout for waiting data from the server */ public static final int DEFAULT_DATA_TIMEOUT = 60000; @@ -63,53 +69,68 @@ public class OwnCloudClientUtils { private static Protocol mDefaultHttpsProtocol = null; private static AdvancedSslSocketFactory mAdvancedSslSocketFactory = null; + + private static X509HostnameVerifier mHostnameVerifier = null; /** * Creates a WebdavClient setup for an ownCloud account * - * @param account The ownCloud account - * @param context The application context - * @return A WebdavClient object ready to be used + * Do not call this method from the main thread. + * + * @param account The ownCloud account + * @param appContext Android application context + * @return A WebdavClient object ready to be used + * @throws AuthenticatorException If the authenticator failed to get the authorization token for the account. + * @throws OperationCanceledException If the authenticator operation was cancelled 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. */ - public static WebdavClient createOwnCloudClient (Account account, Context context) { - Log.d(TAG, "Creating WebdavClient associated to " + account.name); + public static WebdavClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException { + //Log.d(TAG, "Creating WebdavClient associated to " + account.name); - String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL); - OwnCloudVersion ownCloudVersion = new OwnCloudVersion(AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_VERSION)); - String webDavPath = AccountUtils.getWebdavPath(ownCloudVersion); + Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); + WebdavClient client = createOwnCloudClient(uri, appContext); + AccountManager am = AccountManager.get(appContext); + if (am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null) { // TODO avoid a call to getUserData here + String accessToken = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, false); + client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token - WebdavClient client = createOwnCloudClient(Uri.parse(baseUrl + webDavPath), context); - - String username = account.name.substring(0, account.name.lastIndexOf('@')); - String password = AccountManager.get(context).getPassword(account); - //String password = am.blockingGetAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE, true); - - client.setCredentials(username, password); + } else { + String username = account.name.substring(0, account.name.lastIndexOf('@')); + //String password = am.getPassword(account); + String password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD, false); + client.setBasicCredentials(username, password); + } return client; } - /** - * Creates a WebdavClient to try a new account before saving it - * - * @param uri URL to the ownCloud server - * @param username User name - * @param password User password - * @param context Android context where the WebdavClient is being created. - * @return A WebdavClient object ready to be used - */ - public static WebdavClient createOwnCloudClient(Uri uri, String username, String password, Context context) { - Log.d(TAG, "Creating WebdavClient for " + username + "@" + uri); - - WebdavClient client = createOwnCloudClient(uri, context); - - client.setCredentials(username, password); + public static WebdavClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException { + Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); + WebdavClient client = createOwnCloudClient(uri, appContext); + AccountManager am = AccountManager.get(appContext); + if (am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null) { // TODO avoid a call to getUserData here + AccountManagerFuture future = am.getAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, null, currentActivity, null, null); + Bundle result = future.getResult(); + String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN); + //String accessToken = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, false); + if (accessToken == null) throw new AuthenticatorException("WTF!"); + client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token + + } else { + String username = account.name.substring(0, account.name.lastIndexOf('@')); + //String password = am.getPassword(account); + //String password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD, false); + AccountManagerFuture future = am.getAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD, null, currentActivity, null, null); + Bundle result = future.getResult(); + String password = result.getString(AccountManager.KEY_AUTHTOKEN); + client.setBasicCredentials(username, password); + } return client; } - + /** * Creates a WebdavClient to access a URL and sets the desired parameters for ownCloud client connections. @@ -119,7 +140,7 @@ public class OwnCloudClientUtils { * @return A WebdavClient object ready to be used */ public static WebdavClient createOwnCloudClient(Uri uri, Context context) { - Log.d(TAG, "Creating WebdavClient for " + uri); + //Log.d(TAG, "Creating WebdavClient for " + uri); //allowSelfsignedCertificates(true); try { @@ -191,7 +212,7 @@ public class OwnCloudClientUtils { } } - private static AdvancedSslSocketFactory getAdvancedSslSocketFactory(Context context) throws GeneralSecurityException, IOException { + public static AdvancedSslSocketFactory getAdvancedSslSocketFactory(Context context) throws GeneralSecurityException, IOException { if (mAdvancedSslSocketFactory == null) { KeyStore trustStore = getKnownServersStore(context); AdvancedX509TrustManager trustMgr = new AdvancedX509TrustManager(trustStore); @@ -200,17 +221,8 @@ public class OwnCloudClientUtils { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, tms, null); - /*} catch (KeyStoreException e) { - e.printStackTrace(); - - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - - } catch (KeyManagementException e) { - e.printStackTrace(); - - }*/ - mAdvancedSslSocketFactory = new AdvancedSslSocketFactory(sslContext, null); // TODO HOST NAME VERIFIER + mHostnameVerifier = new BrowserCompatHostnameVerifier(); + mAdvancedSslSocketFactory = new AdvancedSslSocketFactory(sslContext, trustMgr, mHostnameVerifier); } return mAdvancedSslSocketFactory; } @@ -280,4 +292,5 @@ public class OwnCloudClientUtils { return mConnManager; } + }