5c41edb059336d1eb65d4b3b1aaa035f3237dace
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / network / webdav / OwnCloudClientFactory.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17 package com.owncloud.android.oc_framework.network.webdav;
18
19 import java.io.IOException;
20 import java.security.GeneralSecurityException;
21
22 import com.owncloud.android.oc_framework.accounts.AccountTypeUtils;
23 import com.owncloud.android.oc_framework.accounts.AccountUtils;
24 import com.owncloud.android.oc_framework.accounts.OwnCloudAccount;
25 import com.owncloud.android.oc_framework.accounts.AccountUtils.AccountNotFoundException;
26 import com.owncloud.android.oc_framework.network.NetworkUtils;
27
28 import android.accounts.Account;
29 import android.accounts.AccountManager;
30 import android.accounts.AccountManagerFuture;
31 import android.accounts.AuthenticatorException;
32 import android.accounts.OperationCanceledException;
33 import android.app.Activity;
34 import android.content.Context;
35 import android.net.Uri;
36 import android.os.Bundle;
37 import android.util.Log;
38
39 public class OwnCloudClientFactory {
40
41 final private static String TAG = OwnCloudClientFactory.class.getSimpleName();
42
43 /** Default timeout for waiting data from the server */
44 public static final int DEFAULT_DATA_TIMEOUT = 60000;
45
46 /** Default timeout for establishing a connection */
47 public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
48
49
50 /**
51 * Creates a WebdavClient setup for an ownCloud account
52 *
53 * Do not call this method from the main thread.
54 *
55 * @param account The ownCloud account
56 * @param appContext Android application context
57 * @return A WebdavClient object ready to be used
58 * @throws AuthenticatorException If the authenticator failed to get the authorization token for the account.
59 * @throws OperationCanceledException If the authenticator operation was cancelled while getting the authorization token for the account.
60 * @throws IOException If there was some I/O error while getting the authorization token for the account.
61 * @throws AccountNotFoundException If 'account' is unknown for the AccountManager
62 */
63 public static WebdavClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
64 //Log_OC.d(TAG, "Creating WebdavClient associated to " + account.name);
65
66 Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
67 AccountManager am = AccountManager.get(appContext);
68 boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
69 boolean isSamlSso = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
70 WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso);
71 if (isOauth2) {
72 String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), false);
73 client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token
74
75 } else if (isSamlSso) { // TODO avoid a call to getUserData here
76 String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), false);
77 client.setSsoSessionCookie(accessToken);
78
79 } else {
80 String username = account.name.substring(0, account.name.lastIndexOf('@'));
81 //String password = am.getPassword(account);
82 String password = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), false);
83 client.setBasicCredentials(username, password);
84 }
85
86 return client;
87 }
88
89
90 public static WebdavClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
91 Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
92 AccountManager am = AccountManager.get(appContext);
93 boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
94 boolean isSamlSso = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
95 WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso);
96
97 if (isOauth2) { // TODO avoid a call to getUserData here
98 AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), null, currentActivity, null, null);
99 Bundle result = future.getResult();
100 String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
101 if (accessToken == null) throw new AuthenticatorException("WTF!");
102 client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token
103
104 } else if (isSamlSso) { // TODO avoid a call to getUserData here
105 AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), null, currentActivity, null, null);
106 Bundle result = future.getResult();
107 String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
108 if (accessToken == null) throw new AuthenticatorException("WTF!");
109 client.setSsoSessionCookie(accessToken);
110
111 } else {
112 String username = account.name.substring(0, account.name.lastIndexOf('@'));
113 //String password = am.getPassword(account);
114 //String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), false);
115 AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), null, currentActivity, null, null);
116 Bundle result = future.getResult();
117 String password = result.getString(AccountManager.KEY_AUTHTOKEN);
118 client.setBasicCredentials(username, password);
119 }
120
121 return client;
122 }
123
124 /**
125 * Creates a WebdavClient to access a URL and sets the desired parameters for ownCloud client connections.
126 *
127 * @param uri URL to the ownCloud server
128 * @param context Android context where the WebdavClient is being created.
129 * @return A WebdavClient object ready to be used
130 */
131 public static WebdavClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects) {
132 try {
133 NetworkUtils.registerAdvancedSslContext(true, context);
134 } catch (GeneralSecurityException e) {
135 Log.e(TAG, "Advanced SSL Context could not be loaded. Default SSL management in the system will be used for HTTPS connections", e);
136
137 } catch (IOException e) {
138 Log.e(TAG, "The local server truststore could not be read. Default SSL management in the system will be used for HTTPS connections", e);
139 }
140
141 WebdavClient client = new WebdavClient(NetworkUtils.getMultiThreadedConnManager());
142
143 client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
144 client.setBaseUri(uri);
145 client.setFollowRedirects(followRedirects);
146
147 return client;
148 }
149
150
151 }