1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
25 package com
.owncloud
.android
.lib
.network
;
27 import java
.io
.IOException
;
28 import java
.security
.GeneralSecurityException
;
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
;
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
;
46 public class OwnCloudClientFactory
{
48 final private static String TAG
= OwnCloudClientFactory
.class.getSimpleName();
50 /** Default timeout for waiting data from the server */
51 public static final int DEFAULT_DATA_TIMEOUT
= 60000;
53 /** Default timeout for establishing a connection */
54 public static final int DEFAULT_CONNECTION_TIMEOUT
= 60000;
58 * Creates a OwnCloudClient setup for an ownCloud account
60 * Do not call this method from the main thread.
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
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);
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
);
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
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
);
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
);
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
);
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
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
);
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
);
132 * Creates a OwnCloudClient to access a URL and sets the desired parameters for ownCloud client connections.
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
138 public static OwnCloudClient
createOwnCloudClient(Uri uri
, Context context
, boolean followRedirects
) {
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
);
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
);
148 OwnCloudClient client
= new OwnCloudClient(NetworkUtils
.getMultiThreadedConnManager());
150 client
.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT
, DEFAULT_CONNECTION_TIMEOUT
);
151 client
.setBaseUri(uri
);
152 client
.setFollowRedirects(followRedirects
);