daea819c389d19a7f0a5477afb63267d8468d4b1
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / lib / network / 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.lib.network;
26
27 import java.io.IOException;
28 import java.security.GeneralSecurityException;
29
30 import com.owncloud.android.lib.accounts.AccountTypeUtils;
31 import com.owncloud.android.lib.accounts.AccountUtils;
32 import com.owncloud.android.lib.accounts.OwnCloudAccount;
33 import com.owncloud.android.lib.accounts.AccountUtils.AccountNotFoundException;
34
35 import android.accounts.Account;
36 import android.accounts.AccountManager;
37 import android.accounts.AccountManagerFuture;
38 import android.accounts.AuthenticatorException;
39 import android.accounts.OperationCanceledException;
40 import android.app.Activity;
41 import android.content.Context;
42 import android.net.Uri;
43 import android.os.Bundle;
44 import android.util.Log;
45
46 public class OwnCloudClientFactory {
47
48 final private static String TAG = OwnCloudClientFactory.class.getSimpleName();
49
50 /** Default timeout for waiting data from the server */
51 public static final int DEFAULT_DATA_TIMEOUT = 60000;
52
53 /** Default timeout for establishing a connection */
54 public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
55
56
57 /**
58 * Creates a OwnCloudClient setup for an ownCloud account
59 *
60 * Do not call this method from the main thread.
61 *
62 * @param account The ownCloud account
63 * @param appContext Android application context
64 * @return A OwnCloudClient object ready to be used
65 * @throws AuthenticatorException If the authenticator failed to get the authorization token for the account.
66 * @throws OperationCanceledException If the authenticator operation was cancelled while getting the authorization token for the account.
67 * @throws IOException If there was some I/O error while getting the authorization token for the account.
68 * @throws AccountNotFoundException If 'account' is unknown for the AccountManager
69 */
70 public static OwnCloudClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
71 //Log_OC.d(TAG, "Creating OwnCloudClient associated to " + account.name);
72
73 Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
74 AccountManager am = AccountManager.get(appContext);
75 boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
76 boolean isSamlSso = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
77 OwnCloudClient client = createOwnCloudClient(uri, appContext, !isSamlSso);
78 if (isOauth2) {
79 String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), false);
80 client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token
81
82 } else if (isSamlSso) { // TODO avoid a call to getUserData here
83 String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), false);
84 client.setSsoSessionCookie(accessToken);
85
86 } else {
87 String username = account.name.substring(0, account.name.lastIndexOf('@'));
88 //String password = am.getPassword(account);
89 String password = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), false);
90 client.setBasicCredentials(username, password);
91 }
92
93 return client;
94 }
95
96
97 public static OwnCloudClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
98 Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
99 AccountManager am = AccountManager.get(appContext);
100 boolean isOauth2 = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
101 boolean isSamlSso = am.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
102 OwnCloudClient client = createOwnCloudClient(uri, appContext, !isSamlSso);
103
104 if (isOauth2) { // TODO avoid a call to getUserData here
105 AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(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.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token
110
111 } else if (isSamlSso) { // TODO avoid a call to getUserData here
112 AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), null, currentActivity, null, null);
113 Bundle result = future.getResult();
114 String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
115 if (accessToken == null) throw new AuthenticatorException("WTF!");
116 client.setSsoSessionCookie(accessToken);
117
118 } else {
119 String username = account.name.substring(0, account.name.lastIndexOf('@'));
120 //String password = am.getPassword(account);
121 //String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), false);
122 AccountManagerFuture<Bundle> future = am.getAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), null, currentActivity, null, null);
123 Bundle result = future.getResult();
124 String password = result.getString(AccountManager.KEY_AUTHTOKEN);
125 client.setBasicCredentials(username, password);
126 }
127
128 return client;
129 }
130
131 /**
132 * Creates a OwnCloudClient to access a URL and sets the desired parameters for ownCloud client connections.
133 *
134 * @param uri URL to the ownCloud server
135 * @param context Android context where the OwnCloudClient is being created.
136 * @return A OwnCloudClient object ready to be used
137 */
138 public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects) {
139 try {
140 NetworkUtils.registerAdvancedSslContext(true, context);
141 } catch (GeneralSecurityException e) {
142 Log.e(TAG, "Advanced SSL Context could not be loaded. Default SSL management in the system will be used for HTTPS connections", e);
143
144 } catch (IOException e) {
145 Log.e(TAG, "The local server truststore could not be read. Default SSL management in the system will be used for HTTPS connections", e);
146 }
147
148 OwnCloudClient client = new OwnCloudClient(NetworkUtils.getMultiThreadedConnManager());
149
150 client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
151 client.setBaseUri(uri);
152 client.setFollowRedirects(followRedirects);
153
154 return client;
155 }
156
157
158 }