1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.authentication
;
21 import android
.accounts
.*;
22 import android
.content
.Context
;
23 import android
.content
.Intent
;
24 import android
.os
.Bundle
;
25 import com
.owncloud
.android
.Log_OC
;
26 import com
.owncloud
.android
.MainApp
;
29 * Authenticator for ownCloud accounts.
31 * Controller class accessed from the system AccountManager, providing integration of ownCloud accounts with the Android system.
33 * TODO - better separation in operations for OAuth-capable and regular ownCloud accounts.
34 * TODO - review completeness
36 * @author David A. Velasco
38 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
41 * Is used by android system to assign accounts to authenticators. Should be
42 * used by application and all extensions.
44 /* These constants are now in MainApp
45 public static final String ACCOUNT_TYPE = "owncloud";
46 public static final String AUTHORITY = "org.owncloud";
47 public static final String AUTH_TOKEN_TYPE = "org.owncloud";
48 public static final String AUTH_TOKEN_TYPE_PASSWORD = "owncloud.password";
49 public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN = "owncloud.oauth2.access_token";
50 public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN = "owncloud.oauth2.refresh_token";
51 public static final String AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE = "owncloud.saml.web_sso.session_cookie";
53 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
54 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
55 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
56 public static final String KEY_ACCOUNT
= "account";
59 * Value under this key should handle path to webdav php script. Will be
60 * removed and usage should be replaced by combining
61 * {@link com.owncloud.android.authentication.AuthenticatorActivity.KEY_OC_BASE_URL} and
62 * {@link com.owncloud.android.utils.OwnCloudVersion}
66 public static final String KEY_OC_URL
= "oc_url";
68 * Version should be 3 numbers separated by dot so it can be parsed by
69 * {@link com.owncloud.android.utils.OwnCloudVersion}
71 public static final String KEY_OC_VERSION
= "oc_version";
73 * Base url should point to owncloud installation without trailing / ie:
74 * http://server/path or https://owncloud.server
76 public static final String KEY_OC_BASE_URL
= "oc_base_url";
78 * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.
80 public static final String KEY_SUPPORTS_OAUTH2
= "oc_supports_oauth2";
82 * Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on.
84 public static final String KEY_SUPPORTS_SAML_WEB_SSO
= "oc_supports_saml_web_sso";
86 private static final String TAG
= AccountAuthenticator
.class.getSimpleName();
88 private Context mContext
;
90 public AccountAuthenticator(Context context
) {
99 public Bundle
addAccount(AccountAuthenticatorResponse response
,
100 String accountType
, String authTokenType
,
101 String
[] requiredFeatures
, Bundle options
)
102 throws NetworkErrorException
{
103 Log_OC
.i(TAG
, "Adding account with type " + accountType
104 + " and auth token " + authTokenType
);
106 validateAccountType(accountType
);
107 } catch (AuthenticatorException e
) {
108 Log_OC
.e(TAG
, "Failed to validate account type " + accountType
+ ": "
111 return e
.getFailureBundle();
113 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
114 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
115 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
116 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
117 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
118 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_CREATE
);
120 setIntentFlags(intent
);
122 final Bundle bundle
= new Bundle();
123 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
131 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
132 Account account
, Bundle options
) throws NetworkErrorException
{
134 validateAccountType(account
.type
);
135 } catch (AuthenticatorException e
) {
136 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
139 return e
.getFailureBundle();
141 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
142 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
144 intent
.putExtra(KEY_ACCOUNT
, account
);
145 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
147 setIntentFlags(intent
);
149 Bundle resultBundle
= new Bundle();
150 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
155 public Bundle
editProperties(AccountAuthenticatorResponse response
,
156 String accountType
) {
164 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
165 Account account
, String authTokenType
, Bundle options
)
166 throws NetworkErrorException
{
167 /// validate parameters
169 validateAccountType(account
.type
);
170 validateAuthTokenType(authTokenType
);
171 } catch (AuthenticatorException e
) {
172 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
175 return e
.getFailureBundle();
178 /// check if required token is stored
179 final AccountManager am
= AccountManager
.get(mContext
);
181 if (authTokenType
.equals(MainApp
.getAuthTokenTypePass())) {
182 accessToken
= am
.getPassword(account
);
184 accessToken
= am
.peekAuthToken(account
, authTokenType
);
186 if (accessToken
!= null
) {
187 final Bundle result
= new Bundle();
188 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
189 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, MainApp
.getAccountType());
190 result
.putString(AccountManager
.KEY_AUTHTOKEN
, accessToken
);
194 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
195 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
196 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
197 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
198 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
199 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, account
);
200 intent
.putExtra(AuthenticatorActivity
.EXTRA_ENFORCED_UPDATE
, true
);
201 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
204 final Bundle bundle
= new Bundle();
205 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
210 public String
getAuthTokenLabel(String authTokenType
) {
215 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
216 Account account
, String
[] features
) throws NetworkErrorException
{
217 final Bundle result
= new Bundle();
218 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, true
);
223 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
224 Account account
, String authTokenType
, Bundle options
)
225 throws NetworkErrorException
{
226 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
227 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
229 intent
.putExtra(KEY_ACCOUNT
, account
);
230 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
231 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
232 setIntentFlags(intent
);
234 final Bundle bundle
= new Bundle();
235 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
240 public Bundle
getAccountRemovalAllowed(
241 AccountAuthenticatorResponse response
, Account account
)
242 throws NetworkErrorException
{
243 return super.getAccountRemovalAllowed(response
, account
);
246 private void setIntentFlags(Intent intent
) {
247 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
248 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
249 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
252 private void validateAccountType(String type
)
253 throws UnsupportedAccountTypeException
{
254 if (!type
.equals(MainApp
.getAccountType())) {
255 throw new UnsupportedAccountTypeException();
259 private void validateAuthTokenType(String authTokenType
)
260 throws UnsupportedAuthTokenTypeException
{
261 if (!authTokenType
.equals(MainApp
.getAuthTokenType()) &&
262 !authTokenType
.equals(MainApp
.getAuthTokenTypePass()) &&
263 !authTokenType
.equals(MainApp
.getAuthTokenTypeAccessToken()) &&
264 !authTokenType
.equals(MainApp
.getAuthTokenTypeRefreshToken()) &&
265 !authTokenType
.equals(MainApp
.getAuthTokenTypeSamlSessionCookie())) {
266 throw new UnsupportedAuthTokenTypeException();
270 public static class AuthenticatorException
extends Exception
{
271 private static final long serialVersionUID
= 1L;
272 private Bundle mFailureBundle
;
274 public AuthenticatorException(int code
, String errorMsg
) {
275 mFailureBundle
= new Bundle();
276 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
278 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
281 public Bundle
getFailureBundle() {
282 return mFailureBundle
;
286 public static class UnsupportedAccountTypeException
extends
287 AuthenticatorException
{
288 private static final long serialVersionUID
= 1L;
290 public UnsupportedAccountTypeException() {
291 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
292 "Unsupported account type");
296 public static class UnsupportedAuthTokenTypeException
extends
297 AuthenticatorException
{
298 private static final long serialVersionUID
= 1L;
300 public UnsupportedAuthTokenTypeException() {
301 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
302 "Unsupported auth token type");
306 public static class UnsupportedFeaturesException
extends
307 AuthenticatorException
{
308 public static final long serialVersionUID
= 1L;
310 public UnsupportedFeaturesException() {
311 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
312 "Unsupported features");
316 public static class AccessDeniedException
extends AuthenticatorException
{
317 public AccessDeniedException(int code
, String errorMsg
) {
318 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
321 private static final long serialVersionUID
= 1L;