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
.authenticator
;
22 import com
.owncloud
.android
.ui
.activity
.AuthenticatorActivity
;
24 import android
.accounts
.*;
25 import android
.content
.Context
;
26 import android
.content
.Intent
;
27 import android
.os
.Bundle
;
28 import android
.util
.Log
;
30 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
32 * Is used by android system to assign accounts to authenticators. Should be
33 * used by application and all extensions.
35 public static final String ACCOUNT_TYPE
= "owncloud";
36 public static final String AUTHORITY
= "org.owncloud";
37 public static final String AUTH_TOKEN_TYPE
= "org.owncloud";
38 public static final String AUTH_TOKEN_TYPE_PASSWORD
= "owncloud.password";
39 public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN
= "owncloud.oauth2.access_token";
40 public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN
= "owncloud.oauth2.refresh_token";
42 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
43 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
44 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
45 public static final String KEY_ACCOUNT
= "account";
47 * Value under this key should handle path to webdav php script. Will be
48 * removed and usage should be replaced by combining
49 * {@link com.owncloud.android.authenticator.AuthenticatorActivity.KEY_OC_BASE_URL} and
50 * {@link com.owncloud.android.utils.OwnCloudVersion}
54 public static final String KEY_OC_URL
= "oc_url";
56 * Version should be 3 numbers separated by dot so it can be parsed by
57 * {@link com.owncloud.android.utils.OwnCloudVersion}
59 public static final String KEY_OC_VERSION
= "oc_version";
61 * Base url should point to owncloud installation without trailing / ie:
62 * http://server/path or https://owncloud.server
64 public static final String KEY_OC_BASE_URL
= "oc_base_url";
66 * Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.
68 public static final String KEY_SUPPORTS_OAUTH2
= "oc_supports_oauth2";
70 private static final String TAG
= AccountAuthenticator
.class.getSimpleName();
72 private Context mContext
;
74 public AccountAuthenticator(Context context
) {
83 public Bundle
addAccount(AccountAuthenticatorResponse response
,
84 String accountType
, String authTokenType
,
85 String
[] requiredFeatures
, Bundle options
)
86 throws NetworkErrorException
{
87 Log
.i(TAG
, "Adding account with type " + accountType
88 + " and auth token " + authTokenType
);
90 validateAccountType(accountType
);
91 } catch (AuthenticatorException e
) {
92 Log
.e(TAG
, "Failed to validate account type " + accountType
+ ": "
95 return e
.getFailureBundle();
97 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
98 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
99 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
100 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
101 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
102 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_CREATE
);
104 setIntentFlags(intent
);
106 final Bundle bundle
= new Bundle();
107 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
115 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
116 Account account
, Bundle options
) throws NetworkErrorException
{
118 validateAccountType(account
.type
);
119 } catch (AuthenticatorException e
) {
120 Log
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
123 return e
.getFailureBundle();
125 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
126 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
128 intent
.putExtra(KEY_ACCOUNT
, account
);
129 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
131 setIntentFlags(intent
);
133 Bundle resultBundle
= new Bundle();
134 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
139 public Bundle
editProperties(AccountAuthenticatorResponse response
,
140 String accountType
) {
148 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
149 Account account
, String authTokenType
, Bundle options
)
150 throws NetworkErrorException
{
151 /// validate parameters
153 validateAccountType(account
.type
);
154 validateAuthTokenType(authTokenType
);
155 } catch (AuthenticatorException e
) {
156 Log
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
159 return e
.getFailureBundle();
162 /// check if required token is stored
163 final AccountManager am
= AccountManager
.get(mContext
);
165 if (authTokenType
.equals(AUTH_TOKEN_TYPE_PASSWORD
)) {
166 accessToken
= am
.getPassword(account
);
168 accessToken
= am
.peekAuthToken(account
, authTokenType
);
170 if (accessToken
!= null
) {
171 final Bundle result
= new Bundle();
172 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
173 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, ACCOUNT_TYPE
);
174 result
.putString(AccountManager
.KEY_AUTHTOKEN
, accessToken
);
178 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
179 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
180 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
181 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
182 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
183 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, account
);
184 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
187 final Bundle bundle
= new Bundle();
188 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
193 public String
getAuthTokenLabel(String authTokenType
) {
198 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
199 Account account
, String
[] features
) throws NetworkErrorException
{
200 final Bundle result
= new Bundle();
201 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, true
);
206 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
207 Account account
, String authTokenType
, Bundle options
)
208 throws NetworkErrorException
{
209 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
210 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
212 intent
.putExtra(KEY_ACCOUNT
, account
);
213 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
214 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
215 setIntentFlags(intent
);
217 final Bundle bundle
= new Bundle();
218 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
223 public Bundle
getAccountRemovalAllowed(
224 AccountAuthenticatorResponse response
, Account account
)
225 throws NetworkErrorException
{
226 return super.getAccountRemovalAllowed(response
, account
);
229 private void setIntentFlags(Intent intent
) {
230 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
231 //intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
232 //intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); // incompatible with the authorization code grant in OAuth
233 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
234 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
237 private void validateAccountType(String type
)
238 throws UnsupportedAccountTypeException
{
239 if (!type
.equals(ACCOUNT_TYPE
)) {
240 throw new UnsupportedAccountTypeException();
244 private void validateAuthTokenType(String authTokenType
)
245 throws UnsupportedAuthTokenTypeException
{
246 if (!authTokenType
.equals(AUTH_TOKEN_TYPE
) &&
247 !authTokenType
.equals(AUTH_TOKEN_TYPE_PASSWORD
) &&
248 !authTokenType
.equals(AUTH_TOKEN_TYPE_ACCESS_TOKEN
) &&
249 !authTokenType
.equals(AUTH_TOKEN_TYPE_REFRESH_TOKEN
) ) {
250 throw new UnsupportedAuthTokenTypeException();
254 public static class AuthenticatorException
extends Exception
{
255 private static final long serialVersionUID
= 1L;
256 private Bundle mFailureBundle
;
258 public AuthenticatorException(int code
, String errorMsg
) {
259 mFailureBundle
= new Bundle();
260 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
262 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
265 public Bundle
getFailureBundle() {
266 return mFailureBundle
;
270 public static class UnsupportedAccountTypeException
extends
271 AuthenticatorException
{
272 private static final long serialVersionUID
= 1L;
274 public UnsupportedAccountTypeException() {
275 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
276 "Unsupported account type");
280 public static class UnsupportedAuthTokenTypeException
extends
281 AuthenticatorException
{
282 private static final long serialVersionUID
= 1L;
284 public UnsupportedAuthTokenTypeException() {
285 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
286 "Unsupported auth token type");
290 public static class UnsupportedFeaturesException
extends
291 AuthenticatorException
{
292 public static final long serialVersionUID
= 1L;
294 public UnsupportedFeaturesException() {
295 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
296 "Unsupported features");
300 public static class AccessDeniedException
extends AuthenticatorException
{
301 public AccessDeniedException(int code
, String errorMsg
) {
302 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
305 private static final long serialVersionUID
= 1L;