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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.authentication
;
23 import android
.accounts
.*;
24 import android
.content
.Context
;
25 import android
.content
.Intent
;
26 import android
.os
.Bundle
;
27 import android
.util
.Log
;
31 * Authenticator for ownCloud accounts.
33 * Controller class accessed from the system AccountManager, providing integration of ownCloud accounts with the Android system.
35 * TODO - better separation in operations for OAuth-capable and regular ownCloud accounts.
36 * TODO - review completeness
38 * @author David A. Velasco
40 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
42 * Is used by android system to assign accounts to authenticators. Should be
43 * used by application and all extensions.
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";
52 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
53 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
54 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
55 public static final String KEY_ACCOUNT
= "account";
57 * Value under this key should handle path to webdav php script. Will be
58 * removed and usage should be replaced by combining
59 * {@link com.owncloud.android.authentication.AuthenticatorActivity.KEY_OC_BASE_URL} and
60 * {@link com.owncloud.android.utils.OwnCloudVersion}
64 public static final String KEY_OC_URL
= "oc_url";
66 * Version should be 3 numbers separated by dot so it can be parsed by
67 * {@link com.owncloud.android.utils.OwnCloudVersion}
69 public static final String KEY_OC_VERSION
= "oc_version";
71 * Base url should point to owncloud installation without trailing / ie:
72 * http://server/path or https://owncloud.server
74 public static final String KEY_OC_BASE_URL
= "oc_base_url";
76 * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.
78 public static final String KEY_SUPPORTS_OAUTH2
= "oc_supports_oauth2";
80 private static final String TAG
= AccountAuthenticator
.class.getSimpleName();
82 private Context mContext
;
84 public AccountAuthenticator(Context context
) {
93 public Bundle
addAccount(AccountAuthenticatorResponse response
,
94 String accountType
, String authTokenType
,
95 String
[] requiredFeatures
, Bundle options
)
96 throws NetworkErrorException
{
97 Log
.i(TAG
, "Adding account with type " + accountType
98 + " and auth token " + authTokenType
);
100 validateAccountType(accountType
);
101 } catch (AuthenticatorException e
) {
102 Log
.e(TAG
, "Failed to validate account type " + accountType
+ ": "
105 return e
.getFailureBundle();
107 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
108 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
109 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
110 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
111 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
112 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_CREATE
);
114 setIntentFlags(intent
);
116 final Bundle bundle
= new Bundle();
117 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
125 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
126 Account account
, Bundle options
) throws NetworkErrorException
{
128 validateAccountType(account
.type
);
129 } catch (AuthenticatorException e
) {
130 Log
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
133 return e
.getFailureBundle();
135 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
136 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
138 intent
.putExtra(KEY_ACCOUNT
, account
);
139 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
141 setIntentFlags(intent
);
143 Bundle resultBundle
= new Bundle();
144 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
149 public Bundle
editProperties(AccountAuthenticatorResponse response
,
150 String accountType
) {
158 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
159 Account account
, String authTokenType
, Bundle options
)
160 throws NetworkErrorException
{
161 /// validate parameters
163 validateAccountType(account
.type
);
164 validateAuthTokenType(authTokenType
);
165 } catch (AuthenticatorException e
) {
166 Log
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
169 return e
.getFailureBundle();
172 /// check if required token is stored
173 final AccountManager am
= AccountManager
.get(mContext
);
175 if (authTokenType
.equals(AUTH_TOKEN_TYPE_PASSWORD
)) {
176 accessToken
= am
.getPassword(account
);
178 accessToken
= am
.peekAuthToken(account
, authTokenType
);
180 if (accessToken
!= null
) {
181 final Bundle result
= new Bundle();
182 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
183 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, ACCOUNT_TYPE
);
184 result
.putString(AccountManager
.KEY_AUTHTOKEN
, accessToken
);
188 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
189 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
190 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
191 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
192 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
193 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, account
);
194 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
197 final Bundle bundle
= new Bundle();
198 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
203 public String
getAuthTokenLabel(String authTokenType
) {
208 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
209 Account account
, String
[] features
) throws NetworkErrorException
{
210 final Bundle result
= new Bundle();
211 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, true
);
216 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
217 Account account
, String authTokenType
, Bundle options
)
218 throws NetworkErrorException
{
219 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
220 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
222 intent
.putExtra(KEY_ACCOUNT
, account
);
223 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
224 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
225 setIntentFlags(intent
);
227 final Bundle bundle
= new Bundle();
228 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
233 public Bundle
getAccountRemovalAllowed(
234 AccountAuthenticatorResponse response
, Account account
)
235 throws NetworkErrorException
{
236 return super.getAccountRemovalAllowed(response
, account
);
239 private void setIntentFlags(Intent intent
) {
240 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
241 //intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
242 //intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); // incompatible with the authorization code grant in OAuth
243 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
244 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
247 private void validateAccountType(String type
)
248 throws UnsupportedAccountTypeException
{
249 if (!type
.equals(ACCOUNT_TYPE
)) {
250 throw new UnsupportedAccountTypeException();
254 private void validateAuthTokenType(String authTokenType
)
255 throws UnsupportedAuthTokenTypeException
{
256 if (!authTokenType
.equals(AUTH_TOKEN_TYPE
) &&
257 !authTokenType
.equals(AUTH_TOKEN_TYPE_PASSWORD
) &&
258 !authTokenType
.equals(AUTH_TOKEN_TYPE_ACCESS_TOKEN
) &&
259 !authTokenType
.equals(AUTH_TOKEN_TYPE_REFRESH_TOKEN
) ) {
260 throw new UnsupportedAuthTokenTypeException();
264 public static class AuthenticatorException
extends Exception
{
265 private static final long serialVersionUID
= 1L;
266 private Bundle mFailureBundle
;
268 public AuthenticatorException(int code
, String errorMsg
) {
269 mFailureBundle
= new Bundle();
270 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
272 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
275 public Bundle
getFailureBundle() {
276 return mFailureBundle
;
280 public static class UnsupportedAccountTypeException
extends
281 AuthenticatorException
{
282 private static final long serialVersionUID
= 1L;
284 public UnsupportedAccountTypeException() {
285 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
286 "Unsupported account type");
290 public static class UnsupportedAuthTokenTypeException
extends
291 AuthenticatorException
{
292 private static final long serialVersionUID
= 1L;
294 public UnsupportedAuthTokenTypeException() {
295 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
296 "Unsupported auth token type");
300 public static class UnsupportedFeaturesException
extends
301 AuthenticatorException
{
302 public static final long serialVersionUID
= 1L;
304 public UnsupportedFeaturesException() {
305 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
306 "Unsupported features");
310 public static class AccessDeniedException
extends AuthenticatorException
{
311 public AccessDeniedException(int code
, String errorMsg
) {
312 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
315 private static final long serialVersionUID
= 1L;