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
;
28 * Authenticator for ownCloud accounts.
30 * Controller class accessed from the system AccountManager, providing integration of ownCloud accounts with the Android system.
32 * TODO - better separation in operations for OAuth-capable and regular ownCloud accounts.
33 * TODO - review completeness
35 * @author David A. Velasco
37 public class AccountAuthenticator
extends AbstractAccountAuthenticator
{
40 * Is used by android system to assign accounts to authenticators. Should be
41 * used by application and all extensions.
43 public static final String ACCOUNT_TYPE
= "owncloud";
44 public static final String AUTHORITY
= "org.owncloud";
45 public static final String AUTH_TOKEN_TYPE
= "org.owncloud";
46 public static final String AUTH_TOKEN_TYPE_PASSWORD
= "owncloud.password";
47 public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN
= "owncloud.oauth2.access_token";
48 public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN
= "owncloud.oauth2.refresh_token";
49 public static final String AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE
= "owncloud.saml.web_sso.session_cookie";
51 public static final String KEY_AUTH_TOKEN_TYPE
= "authTokenType";
52 public static final String KEY_REQUIRED_FEATURES
= "requiredFeatures";
53 public static final String KEY_LOGIN_OPTIONS
= "loginOptions";
54 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 * Flag signaling if the ownCloud server can be accessed with session cookies from SAML-based web single-sign-on.
82 public static final String KEY_SUPPORTS_SAML_WEB_SSO
= "oc_supports_saml_web_sso";
84 private static final String TAG
= AccountAuthenticator
.class.getSimpleName();
86 private Context mContext
;
88 public AccountAuthenticator(Context context
) {
97 public Bundle
addAccount(AccountAuthenticatorResponse response
,
98 String accountType
, String authTokenType
,
99 String
[] requiredFeatures
, Bundle options
)
100 throws NetworkErrorException
{
101 Log_OC
.i(TAG
, "Adding account with type " + accountType
102 + " and auth token " + authTokenType
);
104 validateAccountType(accountType
);
105 } catch (AuthenticatorException e
) {
106 Log_OC
.e(TAG
, "Failed to validate account type " + accountType
+ ": "
109 return e
.getFailureBundle();
111 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
112 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
113 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
114 intent
.putExtra(KEY_REQUIRED_FEATURES
, requiredFeatures
);
115 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
116 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_CREATE
);
118 setIntentFlags(intent
);
120 final Bundle bundle
= new Bundle();
121 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
129 public Bundle
confirmCredentials(AccountAuthenticatorResponse response
,
130 Account account
, Bundle options
) throws NetworkErrorException
{
132 validateAccountType(account
.type
);
133 } catch (AuthenticatorException e
) {
134 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
137 return e
.getFailureBundle();
139 Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
140 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
142 intent
.putExtra(KEY_ACCOUNT
, account
);
143 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
145 setIntentFlags(intent
);
147 Bundle resultBundle
= new Bundle();
148 resultBundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
153 public Bundle
editProperties(AccountAuthenticatorResponse response
,
154 String accountType
) {
162 public Bundle
getAuthToken(AccountAuthenticatorResponse response
,
163 Account account
, String authTokenType
, Bundle options
)
164 throws NetworkErrorException
{
165 /// validate parameters
167 validateAccountType(account
.type
);
168 validateAuthTokenType(authTokenType
);
169 } catch (AuthenticatorException e
) {
170 Log_OC
.e(TAG
, "Failed to validate account type " + account
.type
+ ": "
173 return e
.getFailureBundle();
176 /// check if required token is stored
177 final AccountManager am
= AccountManager
.get(mContext
);
179 if (authTokenType
.equals(AUTH_TOKEN_TYPE_PASSWORD
)) {
180 accessToken
= am
.getPassword(account
);
182 accessToken
= am
.peekAuthToken(account
, authTokenType
);
184 if (accessToken
!= null
) {
185 final Bundle result
= new Bundle();
186 result
.putString(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
);
187 result
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, ACCOUNT_TYPE
);
188 result
.putString(AccountManager
.KEY_AUTHTOKEN
, accessToken
);
192 /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
193 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
194 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
, response
);
195 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
196 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
197 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, account
);
198 intent
.putExtra(AuthenticatorActivity
.EXTRA_ENFORCED_UPDATE
, true
);
199 intent
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
202 final Bundle bundle
= new Bundle();
203 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
208 public String
getAuthTokenLabel(String authTokenType
) {
213 public Bundle
hasFeatures(AccountAuthenticatorResponse response
,
214 Account account
, String
[] features
) throws NetworkErrorException
{
215 final Bundle result
= new Bundle();
216 result
.putBoolean(AccountManager
.KEY_BOOLEAN_RESULT
, true
);
221 public Bundle
updateCredentials(AccountAuthenticatorResponse response
,
222 Account account
, String authTokenType
, Bundle options
)
223 throws NetworkErrorException
{
224 final Intent intent
= new Intent(mContext
, AuthenticatorActivity
.class);
225 intent
.putExtra(AccountManager
.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE
,
227 intent
.putExtra(KEY_ACCOUNT
, account
);
228 intent
.putExtra(KEY_AUTH_TOKEN_TYPE
, authTokenType
);
229 intent
.putExtra(KEY_LOGIN_OPTIONS
, options
);
230 setIntentFlags(intent
);
232 final Bundle bundle
= new Bundle();
233 bundle
.putParcelable(AccountManager
.KEY_INTENT
, intent
);
238 public Bundle
getAccountRemovalAllowed(
239 AccountAuthenticatorResponse response
, Account account
)
240 throws NetworkErrorException
{
241 return super.getAccountRemovalAllowed(response
, account
);
244 private void setIntentFlags(Intent intent
) {
245 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
246 intent
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
247 intent
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
250 private void validateAccountType(String type
)
251 throws UnsupportedAccountTypeException
{
252 if (!type
.equals(ACCOUNT_TYPE
)) {
253 throw new UnsupportedAccountTypeException();
257 private void validateAuthTokenType(String authTokenType
)
258 throws UnsupportedAuthTokenTypeException
{
259 if (!authTokenType
.equals(AUTH_TOKEN_TYPE
) &&
260 !authTokenType
.equals(AUTH_TOKEN_TYPE_PASSWORD
) &&
261 !authTokenType
.equals(AUTH_TOKEN_TYPE_ACCESS_TOKEN
) &&
262 !authTokenType
.equals(AUTH_TOKEN_TYPE_REFRESH_TOKEN
) &&
263 !authTokenType
.equals(AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE
)) {
264 throw new UnsupportedAuthTokenTypeException();
268 public static class AuthenticatorException
extends Exception
{
269 private static final long serialVersionUID
= 1L;
270 private Bundle mFailureBundle
;
272 public AuthenticatorException(int code
, String errorMsg
) {
273 mFailureBundle
= new Bundle();
274 mFailureBundle
.putInt(AccountManager
.KEY_ERROR_CODE
, code
);
276 .putString(AccountManager
.KEY_ERROR_MESSAGE
, errorMsg
);
279 public Bundle
getFailureBundle() {
280 return mFailureBundle
;
284 public static class UnsupportedAccountTypeException
extends
285 AuthenticatorException
{
286 private static final long serialVersionUID
= 1L;
288 public UnsupportedAccountTypeException() {
289 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
290 "Unsupported account type");
294 public static class UnsupportedAuthTokenTypeException
extends
295 AuthenticatorException
{
296 private static final long serialVersionUID
= 1L;
298 public UnsupportedAuthTokenTypeException() {
299 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
300 "Unsupported auth token type");
304 public static class UnsupportedFeaturesException
extends
305 AuthenticatorException
{
306 public static final long serialVersionUID
= 1L;
308 public UnsupportedFeaturesException() {
309 super(AccountManager
.ERROR_CODE_UNSUPPORTED_OPERATION
,
310 "Unsupported features");
314 public static class AccessDeniedException
extends AuthenticatorException
{
315 public AccessDeniedException(int code
, String errorMsg
) {
316 super(AccountManager
.ERROR_CODE_INVALID_RESPONSE
, "Access Denied");
319 private static final long serialVersionUID
= 1L;