b90deedb7e0ed617fa1b30a6b439ded21c7957a3
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / network / webdav / OwnCloudClientFactory.java
1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 *
23 */
24
25 package com.owncloud.android.oc_framework.network.webdav;
26
27 import java.io.IOException;
28 import java.security.GeneralSecurityException;
29
30 import com.owncloud.android.oc_framework.accounts.AccountTypeUtils;
31 import com.owncloud.android.oc_framework.accounts.AccountUtils;
32 import com.owncloud.android.oc_framework.accounts.OwnCloudAccount;
33 import com.owncloud.android.oc_framework.accounts.AccountUtils.AccountNotFoundException;
34 import com.owncloud.android.oc_framework.network.NetworkUtils;
35
36 import android.accounts.Account;
37 import android.accounts.AccountManager;
38 import android.accounts.AccountManagerFuture;
39 import android.accounts.AuthenticatorException;
40 import android.accounts.OperationCanceledException;
41 import android.app.Activity;
42 import android.content.Context;
43 import android.net.Uri;
44 import android.os.Bundle;
45 import android.util.Log;
46
47 public class OwnCloudClientFactory {
48
49 final private static String TAG = OwnCloudClientFactory.class.getSimpleName();
50
51 /** Default timeout for waiting data from the server */
52 public static final int DEFAULT_DATA_TIMEOUT = 60000;
53
54 /** Default timeout for establishing a connection */
55 public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
56
57
58 /**
59 * Creates a WebdavClient setup for an ownCloud account
60 *
61 * Do not call this method from the main thread.
62 *
63 * @param account The ownCloud account
64 * @param appContext Android application context
65 * @return A WebdavClient object ready to be used
66 * @throws AuthenticatorException If the authenticator failed to get the authorization token for the account.
67 * @throws OperationCanceledException If the authenticator operation was cancelled while getting the authorization token for the account.
68 * @throws IOException If there was some I/O error while getting the authorization token for the account.
69 * @throws AccountNotFoundException If 'account' is unknown for the AccountManager
70 */
71 public static WebdavClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
72 //Log_OC.d(TAG, "Creating WebdavClient associated to " + account.name);
73
74 Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
75 AccountManager am = AccountManager.get(appContext);
76 boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
77 boolean isSamlSso = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
78 WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso);
79 if (isOauth2) {
80 String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), false);
81 client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token
82
83 } else if (isSamlSso) { // TODO avoid a call to getUserData here
84 String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), false);
85 client.setSsoSessionCookie(accessToken);
86
87 } else {
88 String username = account.name.substring(0, account.name.lastIndexOf('@'));
89 //String password = am.getPassword(account);
90 String password = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), false);
91 client.setBasicCredentials(username, password);
92 }
93
94 return client;
95 }
96
97
98 public static WebdavClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
99 Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
100 AccountManager am = AccountManager.get(appContext);
101 boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
102 boolean isSamlSso = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
103 WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso);
104
105 if (isOauth2) { // TODO avoid a call to getUserData here
106 AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), null, currentActivity, null, null);
107 Bundle result = future.getResult();
108 String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
109 if (accessToken == null) throw new AuthenticatorException("WTF!");
110 client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token
111
112 } else if (isSamlSso) { // TODO avoid a call to getUserData here
113 AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), null, currentActivity, null, null);
114 Bundle result = future.getResult();
115 String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
116 if (accessToken == null) throw new AuthenticatorException("WTF!");
117 client.setSsoSessionCookie(accessToken);
118
119 } else {
120 String username = account.name.substring(0, account.name.lastIndexOf('@'));
121 //String password = am.getPassword(account);
122 //String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), false);
123 AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), null, currentActivity, null, null);
124 Bundle result = future.getResult();
125 String password = result.getString(AccountManager.KEY_AUTHTOKEN);
126 client.setBasicCredentials(username, password);
127 }
128
129 return client;
130 }
131
132 /**
133 * Creates a WebdavClient to access a URL and sets the desired parameters for ownCloud client connections.
134 *
135 * @param uri URL to the ownCloud server
136 * @param context Android context where the WebdavClient is being created.
137 * @return A WebdavClient object ready to be used
138 */
139 public static WebdavClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects) {
140 try {
141 NetworkUtils.registerAdvancedSslContext(true, context);
142 } catch (GeneralSecurityException e) {
143 Log.e(TAG, "Advanced SSL Context could not be loaded. Default SSL management in the system will be used for HTTPS connections", e);
144
145 } catch (IOException e) {
146 Log.e(TAG, "The local server truststore could not be read. Default SSL management in the system will be used for HTTPS connections", e);
147 }
148
149 WebdavClient client = new WebdavClient(NetworkUtils.getMultiThreadedConnManager());
150
151 client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
152 client.setBaseUri(uri);
153 client.setFollowRedirects(followRedirects);
154
155 return client;
156 }
157
158
159 }