2 * ownCloud Android client application
4 * @author Bartek Przybylski
5 * @author David A. Velasco
7 * Copyright (C) 2012 Bartek Przybylski
8 * Copyright (C) 2015 ownCloud Inc.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 package com
.owncloud
.android
.authentication
;
26 import android
.accounts
.Account
;
27 import android
.accounts
.AccountManager
;
28 import android
.app
.Dialog
;
29 import android
.content
.ComponentName
;
30 import android
.content
.Context
;
31 import android
.content
.Intent
;
32 import android
.content
.ServiceConnection
;
33 import android
.content
.SharedPreferences
;
34 import android
.graphics
.Rect
;
35 import android
.graphics
.drawable
.Drawable
;
36 import android
.net
.Uri
;
37 import android
.net
.http
.SslError
;
38 import android
.os
.Bundle
;
39 import android
.os
.Handler
;
40 import android
.os
.IBinder
;
41 import android
.preference
.PreferenceManager
;
42 import android
.support
.v4
.app
.DialogFragment
;
43 import android
.support
.v4
.app
.Fragment
;
44 import android
.support
.v4
.app
.FragmentManager
;
45 import android
.support
.v4
.app
.FragmentTransaction
;
46 import android
.text
.Editable
;
47 import android
.text
.InputType
;
48 import android
.text
.TextWatcher
;
49 import android
.view
.KeyEvent
;
50 import android
.view
.MotionEvent
;
51 import android
.view
.View
;
52 import android
.view
.View
.OnFocusChangeListener
;
53 import android
.view
.View
.OnTouchListener
;
54 import android
.view
.inputmethod
.EditorInfo
;
55 import android
.webkit
.HttpAuthHandler
;
56 import android
.webkit
.SslErrorHandler
;
57 import android
.webkit
.WebView
;
58 import android
.widget
.Button
;
59 import android
.widget
.CheckBox
;
60 import android
.widget
.EditText
;
61 import android
.widget
.TextView
;
62 import android
.widget
.TextView
.OnEditorActionListener
;
63 import android
.widget
.Toast
;
65 import com
.owncloud
.android
.MainApp
;
66 import com
.owncloud
.android
.R
;
67 import com
.owncloud
.android
.authentication
.SsoWebViewClient
.SsoWebViewClientListener
;
68 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentials
;
69 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentialsFactory
;
70 import com
.owncloud
.android
.lib
.common
.accounts
.AccountTypeUtils
;
71 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.AccountNotFoundException
;
72 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.Constants
;
73 import com
.owncloud
.android
.lib
.common
.network
.CertificateCombinedException
;
74 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
75 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
76 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
77 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
78 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
79 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
80 import com
.owncloud
.android
.lib
.resources
.users
.GetRemoteUserNameOperation
;
81 import com
.owncloud
.android
.operations
.DetectAuthenticationMethodOperation
.AuthenticationMethod
;
82 import com
.owncloud
.android
.operations
.GetServerInfoOperation
;
83 import com
.owncloud
.android
.operations
.OAuth2GetAccessToken
;
84 import com
.owncloud
.android
.services
.OperationsService
;
85 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
86 import com
.owncloud
.android
.ui
.dialog
.CredentialsDialogFragment
;
87 import com
.owncloud
.android
.ui
.dialog
.IndeterminateProgressDialog
;
88 import com
.owncloud
.android
.ui
.dialog
.SamlWebViewDialog
;
89 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
;
90 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
.OnSslUntrustedCertListener
;
91 import com
.owncloud
.android
.utils
.DisplayUtils
;
93 import java
.security
.cert
.X509Certificate
;
97 * This Activity is used to add an ownCloud account to the App
99 public class AuthenticatorActivity
extends AccountAuthenticatorActivity
100 implements OnRemoteOperationListener
, OnFocusChangeListener
, OnEditorActionListener
,
101 SsoWebViewClientListener
, OnSslUntrustedCertListener
,
102 AuthenticatorAsyncTask
.OnAuthenticatorTaskListener
{
104 private static final String TAG
= AuthenticatorActivity
.class.getSimpleName();
106 public static final String EXTRA_ACTION
= "ACTION";
107 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
109 private static final String KEY_AUTH_TOKEN_TYPE
= "AUTH_TOKEN_TYPE";
111 private static final String KEY_HOST_URL_TEXT
= "HOST_URL_TEXT";
112 private static final String KEY_OC_VERSION
= "OC_VERSION";
113 private static final String KEY_SERVER_VALID
= "SERVER_VALID";
114 private static final String KEY_SERVER_CHECKED
= "SERVER_CHECKED";
115 private static final String KEY_SERVER_STATUS_TEXT
= "SERVER_STATUS_TEXT";
116 private static final String KEY_SERVER_STATUS_ICON
= "SERVER_STATUS_ICON";
117 private static final String KEY_IS_SSL_CONN
= "IS_SSL_CONN";
118 private static final String KEY_PASSWORD_EXPOSED
= "PASSWORD_VISIBLE";
119 private static final String KEY_AUTH_STATUS_TEXT
= "AUTH_STATUS_TEXT";
120 private static final String KEY_AUTH_STATUS_ICON
= "AUTH_STATUS_ICON";
121 private static final String KEY_SERVER_AUTH_METHOD
= "SERVER_AUTH_METHOD";
122 private static final String KEY_WAITING_FOR_OP_ID
= "WAITING_FOR_OP_ID";
123 private static final String KEY_AUTH_TOKEN
= "AUTH_TOKEN";
125 private static final String AUTH_ON
= "on";
126 private static final String AUTH_OPTIONAL
= "optional";
128 public static final byte ACTION_CREATE
= 0;
129 public static final byte ACTION_UPDATE_TOKEN
= 1; // requested by the user
130 public static final byte ACTION_UPDATE_EXPIRED_TOKEN
= 2; // detected by the app
132 private static final String UNTRUSTED_CERT_DIALOG_TAG
= "UNTRUSTED_CERT_DIALOG";
133 private static final String SAML_DIALOG_TAG
= "SAML_DIALOG";
134 private static final String WAIT_DIALOG_TAG
= "WAIT_DIALOG";
135 private static final String CREDENTIALS_DIALOG_TAG
= "CREDENTIALS_DIALOG";
136 private static final String KEY_AUTH_IS_FIRST_ATTEMPT_TAG
= "KEY_AUTH_IS_FIRST_ATTEMPT";
138 private static final String KEY_USERNAME
= "USERNAME";
139 private static final String KEY_PASSWORD
= "PASSWORD";
140 private static final String KEY_ASYNC_TASK_IN_PROGRESS
= "AUTH_IN_PROGRESS";
142 /// parameters from EXTRAs in starter Intent
143 private byte mAction
;
144 private Account mAccount
;
145 private String mAuthTokenType
;
148 /// activity-level references / state
149 private final Handler mHandler
= new Handler();
150 private ServiceConnection mOperationsServiceConnection
= null
;
151 private OperationsServiceBinder mOperationsServiceBinder
= null
;
152 private AccountManager mAccountMgr
;
153 private Uri mNewCapturedUriFromOAuth2Redirection
;
156 /// Server PRE-Fragment elements
157 private EditText mHostUrlInput
;
158 private View mRefreshButton
;
159 private TextView mServerStatusView
;
161 private TextWatcher mHostUrlInputWatcher
;
162 private int mServerStatusText
= 0, mServerStatusIcon
= 0;
164 private boolean mServerIsChecked
= false
;
165 private boolean mServerIsValid
= false
;
166 private boolean mPendingAutoCheck
= false
;
168 private GetServerInfoOperation
.ServerInfo mServerInfo
=
169 new GetServerInfoOperation
.ServerInfo();
172 /// Authentication PRE-Fragment elements
173 private CheckBox mOAuth2Check
;
174 private TextView mOAuthAuthEndpointText
;
175 private TextView mOAuthTokenEndpointText
;
176 private EditText mUsernameInput
;
177 private EditText mPasswordInput
;
178 private View mOkButton
;
179 private View mCenteredRefreshButton
;
180 private TextView mAuthStatusView
;
182 private int mAuthStatusText
= 0, mAuthStatusIcon
= 0;
184 private String mAuthToken
= "";
185 private AuthenticatorAsyncTask mAsyncTask
;
187 private boolean mIsFirstAuthAttempt
;
189 /// Identifier of operation in progress which result shouldn't be lost
190 private long mWaitingForOpId
= Long
.MAX_VALUE
;
192 private final String BASIC_TOKEN_TYPE
= AccountTypeUtils
.getAuthTokenTypePass(
193 MainApp
.getAccountType());
194 private final String OAUTH_TOKEN_TYPE
= AccountTypeUtils
.getAuthTokenTypeAccessToken(
195 MainApp
.getAccountType());
196 private final String SAML_TOKEN_TYPE
=
197 AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType());
203 * IMPORTANT ENTRY POINT 1: activity is shown to the user
206 protected void onCreate(Bundle savedInstanceState
) {
207 //Log_OC.wtf(TAG, "onCreate init");
208 super.onCreate(savedInstanceState
);
210 // Workaround, for fixing a problem with Android Library Suppor v7 19
211 //getWindow().requestFeature(Window.FEATURE_NO_TITLE);
212 if (getSupportActionBar() != null
) {
213 getSupportActionBar().hide();
215 getSupportActionBar().setDisplayHomeAsUpEnabled(false
);
216 getSupportActionBar().setDisplayShowHomeEnabled(false
);
217 getSupportActionBar().setDisplayShowTitleEnabled(false
);
220 mIsFirstAuthAttempt
= true
;
222 // bind to Operations Service
223 mOperationsServiceConnection
= new OperationsServiceConnection();
224 if (!bindService(new Intent(this, OperationsService
.class),
225 mOperationsServiceConnection
,
226 Context
.BIND_AUTO_CREATE
)) {
228 R
.string
.error_cant_bind_to_operations_service
,
234 /// init activity state
235 mAccountMgr
= AccountManager
.get(this);
236 mNewCapturedUriFromOAuth2Redirection
= null
;
239 mAction
= getIntent().getByteExtra(EXTRA_ACTION
, ACTION_CREATE
);
240 mAccount
= getIntent().getExtras().getParcelable(EXTRA_ACCOUNT
);
241 if (savedInstanceState
== null
) {
244 mAuthTokenType
= savedInstanceState
.getString(KEY_AUTH_TOKEN_TYPE
);
245 mWaitingForOpId
= savedInstanceState
.getLong(KEY_WAITING_FOR_OP_ID
);
246 mIsFirstAuthAttempt
= savedInstanceState
.getBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG
);
249 /// load user interface
250 setContentView(R
.layout
.account_setup
);
252 /// initialize general UI elements
255 mOkButton
= findViewById(R
.id
.buttonOK
);
256 mOkButton
.setOnClickListener(new View
.OnClickListener() {
259 public void onClick(View v
) {
264 mCenteredRefreshButton
= findViewById(R
.id
.centeredRefreshButton
);
265 mCenteredRefreshButton
.setOnClickListener(new View
.OnClickListener() {
268 public void onClick(View v
) {
274 /// initialize block to be moved to single Fragment to check server and get info about it
275 initServerPreFragment(savedInstanceState
);
277 /// initialize block to be moved to single Fragment to retrieve and validate credentials
278 initAuthorizationPreFragment(savedInstanceState
);
280 //Log_OC.wtf(TAG, "onCreate end");
283 private void initAuthTokenType() {
285 getIntent().getExtras().getString(AccountAuthenticator
.KEY_AUTH_TOKEN_TYPE
);
286 if (mAuthTokenType
== null
) {
287 if (mAccount
!= null
) {
288 boolean oAuthRequired
=
289 (mAccountMgr
.getUserData(mAccount
, Constants
.KEY_SUPPORTS_OAUTH2
) != null
);
290 boolean samlWebSsoRequired
= (
291 mAccountMgr
.getUserData(
292 mAccount
, Constants
.KEY_SUPPORTS_SAML_WEB_SSO
295 mAuthTokenType
= chooseAuthTokenType(oAuthRequired
, samlWebSsoRequired
);
298 boolean oAuthSupported
= AUTH_ON
.equals(getString(R
.string
.auth_method_oauth2
));
299 boolean samlWebSsoSupported
=
300 AUTH_ON
.equals(getString(R
.string
.auth_method_saml_web_sso
));
301 mAuthTokenType
= chooseAuthTokenType(oAuthSupported
, samlWebSsoSupported
);
306 private String
chooseAuthTokenType(boolean oauth
, boolean saml
) {
308 return SAML_TOKEN_TYPE
;
310 return OAUTH_TOKEN_TYPE
;
312 return BASIC_TOKEN_TYPE
;
318 * Configures elements in the user interface under direct control of the Activity.
320 private void initOverallUi() {
322 /// step 1 - load and process relevant inputs (resources, intent, savedInstanceState)
323 boolean isWelcomeLinkVisible
= getResources().getBoolean(R
.bool
.show_welcome_link
);
325 String instructionsMessageText
= null
;
326 if (mAction
== ACTION_UPDATE_EXPIRED_TOKEN
) {
327 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType())
328 .equals(mAuthTokenType
)) {
329 instructionsMessageText
= getString(R
.string
.auth_expired_oauth_token_toast
);
331 } else if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType())
332 .equals(mAuthTokenType
)) {
333 instructionsMessageText
= getString(R
.string
.auth_expired_saml_sso_token_toast
);
336 instructionsMessageText
= getString(R
.string
.auth_expired_basic_auth_toast
);
340 /// step 2 - set properties of UI elements (text, visibility, enabled...)
341 Button welcomeLink
= (Button
) findViewById(R
.id
.welcome_link
);
342 welcomeLink
.setVisibility(isWelcomeLinkVisible ? View
.VISIBLE
: View
.GONE
);
344 String
.format(getString(R
.string
.auth_register
), getString(R
.string
.app_name
)));
346 TextView instructionsView
= (TextView
) findViewById(R
.id
.instructions_message
);
347 if (instructionsMessageText
!= null
) {
348 instructionsView
.setVisibility(View
.VISIBLE
);
349 instructionsView
.setText(instructionsMessageText
);
351 instructionsView
.setVisibility(View
.GONE
);
358 * @param savedInstanceState Saved activity state, as in {{@link #onCreate(Bundle)}
360 private void initServerPreFragment(Bundle savedInstanceState
) {
362 /// step 1 - load and process relevant inputs (resources, intent, savedInstanceState)
363 boolean isUrlInputAllowed
= getResources().getBoolean(R
.bool
.show_server_url_input
);
364 if (savedInstanceState
== null
) {
365 if (mAccount
!= null
) {
366 mServerInfo
.mBaseUrl
= mAccountMgr
.getUserData(mAccount
, Constants
.KEY_OC_BASE_URL
);
367 // TODO do next in a setter for mBaseUrl
368 mServerInfo
.mIsSslConn
= mServerInfo
.mBaseUrl
.startsWith("https://");
369 mServerInfo
.mVersion
= AccountUtils
.getServerVersion(mAccount
);
371 mServerInfo
.mBaseUrl
= getString(R
.string
.server_url
).trim();
372 mServerInfo
.mIsSslConn
= mServerInfo
.mBaseUrl
.startsWith("https://");
375 mServerStatusText
= savedInstanceState
.getInt(KEY_SERVER_STATUS_TEXT
);
376 mServerStatusIcon
= savedInstanceState
.getInt(KEY_SERVER_STATUS_ICON
);
378 mServerIsValid
= savedInstanceState
.getBoolean(KEY_SERVER_VALID
);
379 mServerIsChecked
= savedInstanceState
.getBoolean(KEY_SERVER_CHECKED
);
382 mServerInfo
.mIsSslConn
= savedInstanceState
.getBoolean(KEY_IS_SSL_CONN
);
383 mServerInfo
.mBaseUrl
= savedInstanceState
.getString(KEY_HOST_URL_TEXT
);
384 String ocVersion
= savedInstanceState
.getString(KEY_OC_VERSION
);
385 if (ocVersion
!= null
) {
386 mServerInfo
.mVersion
= new OwnCloudVersion(ocVersion
);
388 mServerInfo
.mAuthMethod
= AuthenticationMethod
.valueOf(
389 savedInstanceState
.getString(KEY_SERVER_AUTH_METHOD
));
393 /// step 2 - set properties of UI elements (text, visibility, enabled...)
394 mHostUrlInput
= (EditText
) findViewById(R
.id
.hostUrlInput
);
395 // Convert IDN to Unicode
396 mHostUrlInput
.setText(DisplayUtils
.convertIdn(mServerInfo
.mBaseUrl
, false
));
397 if (mAction
!= ACTION_CREATE
) {
398 /// lock things that should not change
399 mHostUrlInput
.setEnabled(false
);
400 mHostUrlInput
.setFocusable(false
);
402 if (isUrlInputAllowed
) {
403 mRefreshButton
= findViewById(R
.id
.embeddedRefreshButton
);
405 findViewById(R
.id
.hostUrlFrame
).setVisibility(View
.GONE
);
406 mRefreshButton
= findViewById(R
.id
.centeredRefreshButton
);
408 showRefreshButton(mServerIsChecked
&& !mServerIsValid
&&
409 mWaitingForOpId
> Integer
.MAX_VALUE
);
410 mServerStatusView
= (TextView
) findViewById(R
.id
.server_status_text
);
413 /// step 3 - bind some listeners and options
414 mHostUrlInput
.setImeOptions(EditorInfo
.IME_ACTION_NEXT
);
415 mHostUrlInput
.setOnEditorActionListener(this);
417 /// step 4 - create listeners that will be bound at onResume
418 mHostUrlInputWatcher
= new TextWatcher() {
421 public void afterTextChanged(Editable s
) {
422 if (mOkButton
.isEnabled() &&
423 !mServerInfo
.mBaseUrl
.equals(
424 normalizeUrl(s
.toString(), mServerInfo
.mIsSslConn
))) {
425 mOkButton
.setEnabled(false
);
430 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
434 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {
435 if (mAuthStatusIcon
!= 0) {
436 Log_OC
.d(TAG
, "onTextChanged: hiding authentication status");
445 // TODO find out if this is really necessary, or if it can done in a different way
446 findViewById(R
.id
.scroll
).setOnTouchListener(new OnTouchListener() {
448 public boolean onTouch(View view
, MotionEvent event
) {
449 if (event
.getAction() == MotionEvent
.ACTION_DOWN
) {
451 AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(
452 MainApp
.getAccountType()
453 ).equals(mAuthTokenType
) &&
454 mHostUrlInput
.hasFocus()
464 /// step 4 - mark automatic check to be started when OperationsService is ready
465 mPendingAutoCheck
= (savedInstanceState
== null
&&
466 (mAction
!= ACTION_CREATE
|| !isUrlInputAllowed
));
472 * @param savedInstanceState Saved activity state, as in {{@link #onCreate(Bundle)}
474 private void initAuthorizationPreFragment(Bundle savedInstanceState
) {
476 /// step 0 - get UI elements in layout
477 mOAuth2Check
= (CheckBox
) findViewById(R
.id
.oauth_onOff_check
);
478 mOAuthAuthEndpointText
= (TextView
)findViewById(R
.id
.oAuthEntryPoint_1
);
479 mOAuthTokenEndpointText
= (TextView
)findViewById(R
.id
.oAuthEntryPoint_2
);
480 mUsernameInput
= (EditText
) findViewById(R
.id
.account_username
);
481 mPasswordInput
= (EditText
) findViewById(R
.id
.account_password
);
482 mAuthStatusView
= (TextView
) findViewById(R
.id
.auth_status_text
);
484 /// step 1 - load and process relevant inputs (resources, intent, savedInstanceState)
485 String presetUserName
= null
;
486 boolean isPasswordExposed
= false
;
487 if (savedInstanceState
== null
) {
488 if (mAccount
!= null
) {
489 presetUserName
= mAccount
.name
.substring(0, mAccount
.name
.lastIndexOf('@'));
493 isPasswordExposed
= savedInstanceState
.getBoolean(KEY_PASSWORD_EXPOSED
, false
);
494 mAuthStatusText
= savedInstanceState
.getInt(KEY_AUTH_STATUS_TEXT
);
495 mAuthStatusIcon
= savedInstanceState
.getInt(KEY_AUTH_STATUS_ICON
);
496 mAuthToken
= savedInstanceState
.getString(KEY_AUTH_TOKEN
);
499 /// step 2 - set properties of UI elements (text, visibility, enabled...)
500 mOAuth2Check
.setChecked(
501 AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType())
502 .equals(mAuthTokenType
));
503 if (presetUserName
!= null
) {
504 mUsernameInput
.setText(presetUserName
);
506 if (mAction
!= ACTION_CREATE
) {
507 mUsernameInput
.setEnabled(false
);
508 mUsernameInput
.setFocusable(false
);
510 mPasswordInput
.setText(""); // clean password to avoid social hacking
511 if (isPasswordExposed
) {
514 updateAuthenticationPreFragmentVisibility();
516 mOkButton
.setEnabled(mServerIsValid
);
519 /// step 3 - bind listeners
520 // bindings for password input field
521 mPasswordInput
.setOnFocusChangeListener(this);
522 mPasswordInput
.setImeOptions(EditorInfo
.IME_ACTION_DONE
);
523 mPasswordInput
.setOnEditorActionListener(this);
524 mPasswordInput
.setOnTouchListener(new RightDrawableOnTouchListener() {
526 public boolean onDrawableTouch(final MotionEvent event
) {
527 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
528 AuthenticatorActivity
.this.onViewPasswordClick();
538 * Changes the visibility of input elements depending on
539 * the current authorization method.
541 private void updateAuthenticationPreFragmentVisibility () {
542 if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).
543 equals(mAuthTokenType
)) {
544 // SAML-based web Single Sign On
545 mOAuth2Check
.setVisibility(View
.GONE
);
546 mOAuthAuthEndpointText
.setVisibility(View
.GONE
);
547 mOAuthTokenEndpointText
.setVisibility(View
.GONE
);
548 mUsernameInput
.setVisibility(View
.GONE
);
549 mPasswordInput
.setVisibility(View
.GONE
);
552 if (mAction
== ACTION_CREATE
&&
553 AUTH_OPTIONAL
.equals(getString(R
.string
.auth_method_oauth2
))) {
554 mOAuth2Check
.setVisibility(View
.VISIBLE
);
556 mOAuth2Check
.setVisibility(View
.GONE
);
559 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType()).
560 equals(mAuthTokenType
)) {
561 // OAuth 2 authorization
563 mOAuthAuthEndpointText
.setVisibility(View
.VISIBLE
);
564 mOAuthTokenEndpointText
.setVisibility(View
.VISIBLE
);
565 mUsernameInput
.setVisibility(View
.GONE
);
566 mPasswordInput
.setVisibility(View
.GONE
);
569 // basic HTTP authorization
570 mOAuthAuthEndpointText
.setVisibility(View
.GONE
);
571 mOAuthTokenEndpointText
.setVisibility(View
.GONE
);
572 mUsernameInput
.setVisibility(View
.VISIBLE
);
573 mPasswordInput
.setVisibility(View
.VISIBLE
);
581 * Saves relevant state before {@link #onPause()}
583 * Do NOT save {@link #mNewCapturedUriFromOAuth2Redirection}; it keeps a temporal flag,
584 * intended to defer the processing of the redirection caught in
585 * {@link #onNewIntent(Intent)} until {@link #onResume()}
587 * See {@link super#onSaveInstanceState(Bundle)}
590 protected void onSaveInstanceState(Bundle outState
) {
591 //Log_OC.wtf(TAG, "onSaveInstanceState init" );
592 super.onSaveInstanceState(outState
);
595 outState
.putString(KEY_AUTH_TOKEN_TYPE
, mAuthTokenType
);
596 outState
.putLong(KEY_WAITING_FOR_OP_ID
, mWaitingForOpId
);
598 /// Server PRE-fragment state
599 outState
.putInt(KEY_SERVER_STATUS_TEXT
, mServerStatusText
);
600 outState
.putInt(KEY_SERVER_STATUS_ICON
, mServerStatusIcon
);
601 outState
.putBoolean(KEY_SERVER_CHECKED
, mServerIsChecked
);
602 outState
.putBoolean(KEY_SERVER_VALID
, mServerIsValid
);
603 outState
.putBoolean(KEY_IS_SSL_CONN
, mServerInfo
.mIsSslConn
);
604 outState
.putString(KEY_HOST_URL_TEXT
, mServerInfo
.mBaseUrl
);
605 if (mServerInfo
.mVersion
!= null
) {
606 outState
.putString(KEY_OC_VERSION
, mServerInfo
.mVersion
.getVersion());
608 outState
.putString(KEY_SERVER_AUTH_METHOD
, mServerInfo
.mAuthMethod
.name());
610 /// Authentication PRE-fragment state
611 outState
.putBoolean(KEY_PASSWORD_EXPOSED
, isPasswordVisible());
612 outState
.putInt(KEY_AUTH_STATUS_ICON
, mAuthStatusIcon
);
613 outState
.putInt(KEY_AUTH_STATUS_TEXT
, mAuthStatusText
);
614 outState
.putString(KEY_AUTH_TOKEN
, mAuthToken
);
617 outState
.putBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG
, mIsFirstAuthAttempt
);
619 /// AsyncTask (User and password)
620 outState
.putString(KEY_USERNAME
, mUsernameInput
.getText().toString());
621 outState
.putString(KEY_PASSWORD
, mPasswordInput
.getText().toString());
623 if (mAsyncTask
!= null
) {
624 mAsyncTask
.cancel(true
);
625 outState
.putBoolean(KEY_ASYNC_TASK_IN_PROGRESS
, true
);
627 outState
.putBoolean(KEY_ASYNC_TASK_IN_PROGRESS
, false
);
631 //Log_OC.wtf(TAG, "onSaveInstanceState end" );
635 public void onRestoreInstanceState(Bundle savedInstanceState
) {
636 super.onRestoreInstanceState(savedInstanceState
);
639 boolean inProgress
= savedInstanceState
.getBoolean(KEY_ASYNC_TASK_IN_PROGRESS
);
641 String username
= savedInstanceState
.getString(KEY_USERNAME
);
642 String password
= savedInstanceState
.getString(KEY_PASSWORD
);
644 OwnCloudCredentials credentials
= null
;
645 if (BASIC_TOKEN_TYPE
.equals(mAuthTokenType
)) {
646 credentials
= OwnCloudCredentialsFactory
.newBasicCredentials(username
, password
);
648 } else if (OAUTH_TOKEN_TYPE
.equals(mAuthTokenType
)) {
649 credentials
= OwnCloudCredentialsFactory
.newBearerCredentials(mAuthToken
);
652 accessRootFolder(credentials
);
657 * The redirection triggered by the OAuth authentication server as response to the
658 * GET AUTHORIZATION request is caught here.
660 * To make this possible, this activity needs to be qualified with android:launchMode =
661 * "singleTask" in the AndroidManifest.xml file.
664 protected void onNewIntent (Intent intent
) {
665 Log_OC
.d(TAG
, "onNewIntent()");
666 Uri data
= intent
.getData();
667 if (data
!= null
&& data
.toString().startsWith(getString(R
.string
.oauth2_redirect_uri
))) {
668 mNewCapturedUriFromOAuth2Redirection
= data
;
674 * The redirection triggered by the OAuth authentication server as response to the
675 * GET AUTHORIZATION, and deferred in {@link #onNewIntent(Intent)}, is processed here.
678 protected void onResume() {
681 // bound here to avoid spurious changes triggered by Android on device rotations
682 mHostUrlInput
.setOnFocusChangeListener(this);
683 mHostUrlInput
.addTextChangedListener(mHostUrlInputWatcher
);
685 if (mNewCapturedUriFromOAuth2Redirection
!= null
) {
686 getOAuth2AccessTokenFromCapturedRedirection();
689 if (mOperationsServiceBinder
!= null
) {
690 doOnResumeAndBound();
697 protected void onPause() {
698 if (mOperationsServiceBinder
!= null
) {
699 mOperationsServiceBinder
.removeOperationListener(this);
702 mHostUrlInput
.removeTextChangedListener(mHostUrlInputWatcher
);
703 mHostUrlInput
.setOnFocusChangeListener(null
);
709 protected void onDestroy() {
711 mHostUrlInputWatcher
= null
;
713 if (mOperationsServiceConnection
!= null
) {
714 unbindService(mOperationsServiceConnection
);
715 mOperationsServiceBinder
= null
;
722 * Parses the redirection with the response to the GET AUTHORIZATION request to the
723 * oAuth server and requests for the access token (GET ACCESS TOKEN)
725 private void getOAuth2AccessTokenFromCapturedRedirection() {
726 /// Parse data from OAuth redirection
727 String queryParameters
= mNewCapturedUriFromOAuth2Redirection
.getQuery();
728 mNewCapturedUriFromOAuth2Redirection
= null
;
730 /// Showing the dialog with instructions for the user.
731 IndeterminateProgressDialog dialog
=
732 IndeterminateProgressDialog
.newInstance(R
.string
.auth_getting_authorization
, true
);
733 dialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
735 /// GET ACCESS TOKEN to the oAuth server
736 Intent getServerInfoIntent
= new Intent();
737 getServerInfoIntent
.setAction(OperationsService
.ACTION_OAUTH2_GET_ACCESS_TOKEN
);
739 getServerInfoIntent
.putExtra(
740 OperationsService
.EXTRA_SERVER_URL
,
741 mOAuthTokenEndpointText
.getText().toString().trim());
743 getServerInfoIntent
.putExtra(
744 OperationsService
.EXTRA_OAUTH2_QUERY_PARAMETERS
,
747 if (mOperationsServiceBinder
!= null
) {
748 //Log_OC.wtf(TAG, "getting access token..." );
749 mWaitingForOpId
= mOperationsServiceBinder
.queueNewOperation(getServerInfoIntent
);
756 * Handles the change of focus on the text inputs for the server URL and the password
758 public void onFocusChange(View view
, boolean hasFocus
) {
759 if (view
.getId() == R
.id
.hostUrlInput
) {
761 onUrlInputFocusLost();
764 showRefreshButton(false
);
767 } else if (view
.getId() == R
.id
.account_password
) {
768 onPasswordFocusChanged(hasFocus
);
774 * Handles changes in focus on the text input for the server URL.
776 * IMPORTANT ENTRY POINT 2: When (!hasFocus), user wrote the server URL and changed to
777 * other field. The operation to check the existence of the server in the entered URL is
780 * When hasFocus: user 'comes back' to write again the server URL.
782 private void onUrlInputFocusLost() {
783 if (!mServerInfo
.mBaseUrl
.equals(
784 normalizeUrl(mHostUrlInput
.getText().toString(), mServerInfo
.mIsSslConn
))) {
785 // check server again only if the user changed something in the field
788 mOkButton
.setEnabled(mServerIsValid
);
789 showRefreshButton(!mServerIsValid
);
794 private void checkOcServer() {
795 String uri
= mHostUrlInput
.getText().toString().trim();
796 mServerIsValid
= false
;
797 mServerIsChecked
= false
;
798 mOkButton
.setEnabled(false
);
799 mServerInfo
= new GetServerInfoOperation
.ServerInfo();
800 showRefreshButton(false
);
802 if (uri
.length() != 0) {
803 // Handle internationalized domain names
804 uri
= DisplayUtils
.convertIdn(uri
, true
);
806 mServerStatusText
= R
.string
.auth_testing_connection
;
807 mServerStatusIcon
= R
.drawable
.progress_small
;
810 Intent getServerInfoIntent
= new Intent();
811 getServerInfoIntent
.setAction(OperationsService
.ACTION_GET_SERVER_INFO
);
812 getServerInfoIntent
.putExtra(
813 OperationsService
.EXTRA_SERVER_URL
,
814 normalizeUrlSuffix(uri
)
816 if (mOperationsServiceBinder
!= null
) {
817 mWaitingForOpId
= mOperationsServiceBinder
.queueNewOperation(getServerInfoIntent
);
819 Log_OC
.wtf(TAG
, "Server check tried with OperationService unbound!" );
823 mServerStatusText
= 0;
824 mServerStatusIcon
= 0;
831 * Handles changes in focus on the text input for the password (basic authorization).
833 * When (hasFocus), the button to toggle password visibility is shown.
835 * When (!hasFocus), the button is made invisible and the password is hidden.
837 * @param hasFocus 'True' if focus is received, 'false' if is lost
839 private void onPasswordFocusChanged(boolean hasFocus
) {
841 showViewPasswordButton();
844 hidePasswordButton();
849 private void showViewPasswordButton() {
850 int drawable
= R
.drawable
.ic_view
;
851 if (isPasswordVisible()) {
852 drawable
= R
.drawable
.ic_hide
;
854 mPasswordInput
.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawable
, 0);
857 private boolean isPasswordVisible() {
858 return ((mPasswordInput
.getInputType() & InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
) ==
859 InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
);
862 private void hidePasswordButton() {
863 mPasswordInput
.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
866 private void showPassword() {
867 mPasswordInput
.setInputType(
868 InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
870 showViewPasswordButton();
873 private void hidePassword() {
874 mPasswordInput
.setInputType(
875 InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_PASSWORD
877 showViewPasswordButton();
881 * Checks the credentials of the user in the root of the ownCloud server
882 * before creating a new local account.
884 * For basic authorization, a check of existence of the root folder is
887 * For OAuth, starts the flow to get an access token; the credentials test
888 * is postponed until it is available.
890 * IMPORTANT ENTRY POINT 4
892 public void onOkClick() {
893 // this check should be unnecessary
894 if (mServerInfo
.mVersion
== null
||
895 !mServerInfo
.mVersion
.isVersionValid() ||
896 mServerInfo
.mBaseUrl
== null
||
897 mServerInfo
.mBaseUrl
.length() == 0) {
898 mServerStatusIcon
= R
.drawable
.common_error
;
899 mServerStatusText
= R
.string
.auth_wtf_reenter_URL
;
901 mOkButton
.setEnabled(false
);
905 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType()).
906 equals(mAuthTokenType
)) {
908 startOauthorization();
909 } else if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).
910 equals(mAuthTokenType
)) {
912 startSamlBasedFederatedSingleSignOnAuthorization();
914 checkBasicAuthorization();
920 * Tests the credentials entered by the user performing a check of existence on
921 * the root folder of the ownCloud server.
923 private void checkBasicAuthorization() {
924 /// get basic credentials entered by user
925 String username
= mUsernameInput
.getText().toString();
926 String password
= mPasswordInput
.getText().toString();
928 /// be gentle with the user
929 IndeterminateProgressDialog dialog
=
930 IndeterminateProgressDialog
.newInstance(R
.string
.auth_trying_to_login
, true
);
931 dialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
933 /// validate credentials accessing the root folder
934 OwnCloudCredentials credentials
= OwnCloudCredentialsFactory
.newBasicCredentials(username
,
936 accessRootFolder(credentials
);
939 private void accessRootFolder(OwnCloudCredentials credentials
) {
940 mAsyncTask
= new AuthenticatorAsyncTask(this);
941 Object
[] params
= { mServerInfo
.mBaseUrl
, credentials
};
942 mAsyncTask
.execute(params
);
947 * Starts the OAuth 'grant type' flow to get an access token, with
948 * a GET AUTHORIZATION request to the BUILT-IN authorization server.
950 private void startOauthorization() {
951 // be gentle with the user
952 mAuthStatusIcon
= R
.drawable
.progress_small
;
953 mAuthStatusText
= R
.string
.oauth_login_connection
;
956 // GET AUTHORIZATION request
957 Uri uri
= Uri
.parse(mOAuthAuthEndpointText
.getText().toString().trim());
958 Uri
.Builder uriBuilder
= uri
.buildUpon();
959 uriBuilder
.appendQueryParameter(
960 OAuth2Constants
.KEY_RESPONSE_TYPE
, getString(R
.string
.oauth2_response_type
)
962 uriBuilder
.appendQueryParameter(
963 OAuth2Constants
.KEY_REDIRECT_URI
, getString(R
.string
.oauth2_redirect_uri
)
965 uriBuilder
.appendQueryParameter(
966 OAuth2Constants
.KEY_CLIENT_ID
, getString(R
.string
.oauth2_client_id
)
968 uriBuilder
.appendQueryParameter(
969 OAuth2Constants
.KEY_SCOPE
, getString(R
.string
.oauth2_scope
)
971 uri
= uriBuilder
.build();
972 Log_OC
.d(TAG
, "Starting browser to view " + uri
.toString());
973 Intent i
= new Intent(Intent
.ACTION_VIEW
, uri
);
979 * Starts the Web Single Sign On flow to get access to the root folder
982 private void startSamlBasedFederatedSingleSignOnAuthorization() {
983 /// be gentle with the user
984 mAuthStatusIcon
= R
.drawable
.progress_small
;
985 mAuthStatusText
= R
.string
.auth_connecting_auth_server
;
988 /// Show SAML-based SSO web dialog
989 String targetUrl
= mServerInfo
.mBaseUrl
990 + AccountUtils
.getWebdavPath(mServerInfo
.mVersion
, mAuthTokenType
);
991 SamlWebViewDialog dialog
= SamlWebViewDialog
.newInstance(targetUrl
, targetUrl
);
992 dialog
.show(getSupportFragmentManager(), SAML_DIALOG_TAG
);
996 * Callback method invoked when a RemoteOperation executed by this Activity finishes.
998 * Dispatches the operation flow to the right method.
1001 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1003 if (operation
instanceof GetServerInfoOperation
) {
1004 if (operation
.hashCode() == mWaitingForOpId
) {
1005 onGetServerInfoFinish(result
);
1006 } // else nothing ; only the last check operation is considered;
1007 // multiple can be started if the user amends a URL quickly
1009 } else if (operation
instanceof OAuth2GetAccessToken
) {
1010 onGetOAuthAccessTokenFinish(result
);
1012 } else if (operation
instanceof GetRemoteUserNameOperation
) {
1013 onGetUserNameFinish(result
);
1018 private void onGetUserNameFinish(RemoteOperationResult result
) {
1019 mWaitingForOpId
= Long
.MAX_VALUE
;
1020 if (result
.isSuccess()) {
1021 boolean success
= false
;
1022 String username
= (String
) result
.getData().get(0);
1024 if ( mAction
== ACTION_CREATE
) {
1025 mUsernameInput
.setText(username
);
1026 success
= createAccount(result
);
1029 if (!mUsernameInput
.getText().toString().equals(username
)) {
1030 // fail - not a new account, but an existing one; disallow
1031 result
= new RemoteOperationResult(ResultCode
.ACCOUNT_NOT_THE_SAME
);
1033 updateAuthStatusIconAndText(result
);
1035 Log_OC
.d(TAG
, result
.getLogMessage());
1038 updateAccountAuthentication();
1041 } catch (AccountNotFoundException e
) {
1042 Log_OC
.e(TAG
, "Account " + mAccount
+ " was removed!", e
);
1043 Toast
.makeText(this, R
.string
.auth_account_does_not_exist
,
1044 Toast
.LENGTH_SHORT
).show();
1053 updateStatusIconFailUserName();
1055 Log_OC
.e(TAG
, "Access to user name failed: " + result
.getLogMessage());
1061 * Processes the result of the server check performed when the user finishes the enter of the
1064 * @param result Result of the check.
1066 private void onGetServerInfoFinish(RemoteOperationResult result
) {
1067 /// update activity state
1068 mServerIsChecked
= true
;
1069 mWaitingForOpId
= Long
.MAX_VALUE
;
1071 // update server status, but don't show it yet
1072 updateServerStatusIconAndText(result
);
1074 if (result
.isSuccess()) {
1076 // 1. connection succeeded, and we know if it's SSL or not
1077 // 2. server is installed
1078 // 3. we got the server version
1079 // 4. we got the authentication method required by the server
1080 mServerInfo
= (GetServerInfoOperation
.ServerInfo
) (result
.getData().get(0));
1082 if (!authSupported(mServerInfo
.mAuthMethod
)) {
1084 updateServerStatusIconNoRegularAuth(); // overrides updateServerStatusIconAndText()
1085 mServerIsValid
= false
;
1088 mServerIsValid
= true
;
1092 mServerIsValid
= false
;
1096 showRefreshButton(!mServerIsValid
);
1098 mOkButton
.setEnabled(mServerIsValid
);
1100 /// very special case (TODO: move to a common place for all the remote operations)
1101 if (result
.getCode() == ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
) {
1102 showUntrustedCertDialog(result
);
1107 private boolean authSupported(AuthenticationMethod authMethod
) {
1108 return (( BASIC_TOKEN_TYPE
.equals(mAuthTokenType
) &&
1109 AuthenticationMethod
.BASIC_HTTP_AUTH
.equals(authMethod
) ) ||
1110 ( OAUTH_TOKEN_TYPE
.equals(mAuthTokenType
) &&
1111 AuthenticationMethod
.BEARER_TOKEN
.equals(authMethod
)) ||
1112 ( SAML_TOKEN_TYPE
.equals(mAuthTokenType
) &&
1113 AuthenticationMethod
.SAML_WEB_SSO
.equals(authMethod
))
1118 // TODO remove, if possible
1119 private String
normalizeUrl(String url
, boolean sslWhenUnprefixed
) {
1120 if (url
!= null
&& url
.length() > 0) {
1122 if (!url
.toLowerCase().startsWith("http://") &&
1123 !url
.toLowerCase().startsWith("https://")) {
1124 if (sslWhenUnprefixed
) {
1125 url
= "https://" + url
;
1127 url
= "http://" + url
;
1131 url
= normalizeUrlSuffix(url
);
1133 return (url
!= null ? url
: "");
1137 private String
normalizeUrlSuffix(String url
) {
1138 if (url
.endsWith("/")) {
1139 url
= url
.substring(0, url
.length() - 1);
1141 url
= trimUrlWebdav(url
);
1146 // TODO remove, if possible
1147 private String
trimUrlWebdav(String url
){
1148 if(url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_4_0_AND_LATER
)){
1149 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_4_0_AND_LATER
.length());
1156 * Chooses the right icon and text to show to the user for the received operation result.
1158 * @param result Result of a remote operation performed in this activity
1160 private void updateServerStatusIconAndText(RemoteOperationResult result
) {
1161 mServerStatusIcon
= R
.drawable
.common_error
; // the most common case in the switch below
1163 switch (result
.getCode()) {
1165 mServerStatusIcon
= R
.drawable
.ic_lock
;
1166 mServerStatusText
= R
.string
.auth_secure_connection
;
1171 if (mHostUrlInput
.getText().toString().trim().toLowerCase().startsWith("http://") ) {
1172 mServerStatusText
= R
.string
.auth_connection_established
;
1173 mServerStatusIcon
= R
.drawable
.ic_ok
;
1175 mServerStatusText
= R
.string
.auth_nossl_plain_ok_title
;
1176 mServerStatusIcon
= R
.drawable
.ic_lock_open
;
1180 case NO_NETWORK_CONNECTION
:
1181 mServerStatusIcon
= R
.drawable
.no_network
;
1182 mServerStatusText
= R
.string
.auth_no_net_conn_title
;
1185 case SSL_RECOVERABLE_PEER_UNVERIFIED
:
1186 mServerStatusText
= R
.string
.auth_ssl_unverified_server_title
;
1188 case BAD_OC_VERSION
:
1189 mServerStatusText
= R
.string
.auth_bad_oc_version_title
;
1191 case WRONG_CONNECTION
:
1192 mServerStatusText
= R
.string
.auth_wrong_connection_title
;
1195 mServerStatusText
= R
.string
.auth_timeout_title
;
1197 case INCORRECT_ADDRESS
:
1198 mServerStatusText
= R
.string
.auth_incorrect_address_title
;
1201 mServerStatusText
= R
.string
.auth_ssl_general_error_title
;
1204 mServerStatusText
= R
.string
.auth_unauthorized
;
1206 case HOST_NOT_AVAILABLE
:
1207 mServerStatusText
= R
.string
.auth_unknown_host_title
;
1209 case INSTANCE_NOT_CONFIGURED
:
1210 mServerStatusText
= R
.string
.auth_not_configured_title
;
1212 case FILE_NOT_FOUND
:
1213 mServerStatusText
= R
.string
.auth_incorrect_path_title
;
1216 mServerStatusText
= R
.string
.auth_oauth_error
;
1218 case OAUTH2_ERROR_ACCESS_DENIED
:
1219 mServerStatusText
= R
.string
.auth_oauth_error_access_denied
;
1221 case UNHANDLED_HTTP_CODE
:
1223 mServerStatusText
= R
.string
.auth_unknown_error_title
;
1225 case OK_REDIRECT_TO_NON_SECURE_CONNECTION
:
1226 mServerStatusIcon
= R
.drawable
.ic_lock_open
;
1227 mServerStatusText
= R
.string
.auth_redirect_non_secure_connection_title
;
1230 mServerStatusText
= 0;
1231 mServerStatusIcon
= 0;
1237 * Chooses the right icon and text to show to the user for the received operation result.
1239 * @param result Result of a remote operation performed in this activity
1241 private void updateAuthStatusIconAndText(RemoteOperationResult result
) {
1242 mAuthStatusIcon
= R
.drawable
.common_error
; // the most common case in the switch below
1244 switch (result
.getCode()) {
1246 mAuthStatusIcon
= R
.drawable
.ic_lock
;
1247 mAuthStatusText
= R
.string
.auth_secure_connection
;
1252 if (mHostUrlInput
.getText().toString().trim().toLowerCase().startsWith("http://") ) {
1253 mAuthStatusText
= R
.string
.auth_connection_established
;
1254 mAuthStatusIcon
= R
.drawable
.ic_ok
;
1256 mAuthStatusText
= R
.string
.auth_nossl_plain_ok_title
;
1257 mAuthStatusIcon
= R
.drawable
.ic_lock_open
;
1261 case NO_NETWORK_CONNECTION
:
1262 mAuthStatusIcon
= R
.drawable
.no_network
;
1263 mAuthStatusText
= R
.string
.auth_no_net_conn_title
;
1266 case SSL_RECOVERABLE_PEER_UNVERIFIED
:
1267 mAuthStatusText
= R
.string
.auth_ssl_unverified_server_title
;
1269 case BAD_OC_VERSION
:
1270 mAuthStatusText
= R
.string
.auth_bad_oc_version_title
;
1272 case WRONG_CONNECTION
:
1273 mAuthStatusText
= R
.string
.auth_wrong_connection_title
;
1276 mAuthStatusText
= R
.string
.auth_timeout_title
;
1278 case INCORRECT_ADDRESS
:
1279 mAuthStatusText
= R
.string
.auth_incorrect_address_title
;
1282 mAuthStatusText
= R
.string
.auth_ssl_general_error_title
;
1285 mAuthStatusText
= R
.string
.auth_unauthorized
;
1287 case HOST_NOT_AVAILABLE
:
1288 mAuthStatusText
= R
.string
.auth_unknown_host_title
;
1290 case INSTANCE_NOT_CONFIGURED
:
1291 mAuthStatusText
= R
.string
.auth_not_configured_title
;
1293 case FILE_NOT_FOUND
:
1294 mAuthStatusText
= R
.string
.auth_incorrect_path_title
;
1297 mAuthStatusText
= R
.string
.auth_oauth_error
;
1299 case OAUTH2_ERROR_ACCESS_DENIED
:
1300 mAuthStatusText
= R
.string
.auth_oauth_error_access_denied
;
1302 case ACCOUNT_NOT_NEW
:
1303 mAuthStatusText
= R
.string
.auth_account_not_new
;
1305 case ACCOUNT_NOT_THE_SAME
:
1306 mAuthStatusText
= R
.string
.auth_account_not_the_same
;
1308 case UNHANDLED_HTTP_CODE
:
1310 mAuthStatusText
= R
.string
.auth_unknown_error_title
;
1313 mAuthStatusText
= 0;
1314 mAuthStatusIcon
= 0;
1319 private void updateStatusIconFailUserName(){
1320 mAuthStatusIcon
= R
.drawable
.common_error
;
1321 mAuthStatusText
= R
.string
.auth_fail_get_user_name
;
1324 private void updateServerStatusIconNoRegularAuth(){
1325 mServerStatusIcon
= R
.drawable
.common_error
;
1326 mServerStatusText
= R
.string
.auth_can_not_auth_against_server
;
1330 * Processes the result of the request for and access token send
1331 * to an OAuth authorization server.
1333 * @param result Result of the operation.
1335 private void onGetOAuthAccessTokenFinish(RemoteOperationResult result
) {
1336 mWaitingForOpId
= Long
.MAX_VALUE
;
1337 dismissDialog(WAIT_DIALOG_TAG
);
1339 if (result
.isSuccess()) {
1340 /// be gentle with the user
1341 IndeterminateProgressDialog dialog
=
1342 IndeterminateProgressDialog
.newInstance(R
.string
.auth_trying_to_login
, true
);
1343 dialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
1345 /// time to test the retrieved access token on the ownCloud server
1346 @SuppressWarnings("unchecked")
1347 Map
<String
, String
> tokens
= (Map
<String
, String
>)(result
.getData().get(0));
1348 mAuthToken
= tokens
.get(OAuth2Constants
.KEY_ACCESS_TOKEN
);
1349 Log_OC
.d(TAG
, "Got ACCESS TOKEN: " + mAuthToken
);
1351 /// validate token accessing to root folder / getting session
1352 OwnCloudCredentials credentials
= OwnCloudCredentialsFactory
.newBearerCredentials(
1354 accessRootFolder(credentials
);
1357 updateAuthStatusIconAndText(result
);
1359 Log_OC
.d(TAG
, "Access failed: " + result
.getLogMessage());
1365 * Processes the result of the access check performed to try the user credentials.
1367 * Creates a new account through the AccountManager.
1369 * @param result Result of the operation.
1372 public void onAuthenticatorTaskCallback(RemoteOperationResult result
) {
1373 mWaitingForOpId
= Long
.MAX_VALUE
;
1374 dismissDialog(WAIT_DIALOG_TAG
);
1376 if (result
.isSuccess()) {
1377 Log_OC
.d(TAG
, "Successful access - time to save the account");
1379 boolean success
= false
;
1381 if (mAction
== ACTION_CREATE
) {
1382 success
= createAccount(result
);
1386 updateAccountAuthentication();
1389 } catch (AccountNotFoundException e
) {
1390 Log_OC
.e(TAG
, "Account " + mAccount
+ " was removed!", e
);
1391 Toast
.makeText(this, R
.string
.auth_account_does_not_exist
,
1392 Toast
.LENGTH_SHORT
).show();
1401 } else if (result
.isServerFail() || result
.isException()) {
1402 /// server errors or exceptions in authorization take to requiring a new check of
1404 mServerIsChecked
= true
;
1405 mServerIsValid
= false
;
1406 mServerInfo
= new GetServerInfoOperation
.ServerInfo();
1408 // update status icon and text
1409 updateServerStatusIconAndText(result
);
1411 mAuthStatusIcon
= 0;
1412 mAuthStatusText
= 0;
1415 // update input controls state
1416 showRefreshButton(true
);
1417 mOkButton
.setEnabled(false
);
1419 // very special case (TODO: move to a common place for all the remote operations)
1420 if (result
.getCode() == ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
) {
1421 showUntrustedCertDialog(result
);
1424 } else { // authorization fail due to client side - probably wrong credentials
1425 updateAuthStatusIconAndText(result
);
1427 Log_OC
.d(TAG
, "Access failed: " + result
.getLogMessage());
1435 * Updates the authentication token.
1437 * Sets the proper response so that the AccountAuthenticator that started this activity
1438 * saves a new authorization token for mAccount.
1440 * Kills the session kept by OwnCloudClientManager so that a new one will created with
1441 * the new credentials when needed.
1443 private void updateAccountAuthentication() throws AccountNotFoundException
{
1445 Bundle response
= new Bundle();
1446 response
.putString(AccountManager
.KEY_ACCOUNT_NAME
, mAccount
.name
);
1447 response
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, mAccount
.type
);
1449 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType()).
1450 equals(mAuthTokenType
)) {
1451 response
.putString(AccountManager
.KEY_AUTHTOKEN
, mAuthToken
);
1452 // the next line is necessary, notifications are calling directly to the
1453 // AuthenticatorActivity to update, without AccountManager intervention
1454 mAccountMgr
.setAuthToken(mAccount
, mAuthTokenType
, mAuthToken
);
1456 } else if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).
1457 equals(mAuthTokenType
)) {
1459 response
.putString(AccountManager
.KEY_AUTHTOKEN
, mAuthToken
);
1460 // the next line is necessary; by now, notifications are calling directly to the
1461 // AuthenticatorActivity to update, without AccountManager intervention
1462 mAccountMgr
.setAuthToken(mAccount
, mAuthTokenType
, mAuthToken
);
1465 response
.putString(AccountManager
.KEY_AUTHTOKEN
, mPasswordInput
.getText().toString());
1466 mAccountMgr
.setPassword(mAccount
, mPasswordInput
.getText().toString());
1468 setAccountAuthenticatorResult(response
);
1474 * Creates a new account through the Account Authenticator that started this activity.
1476 * This makes the account permanent.
1478 * TODO Decide how to name the OAuth accounts
1480 private boolean createAccount(RemoteOperationResult authResult
) {
1481 /// create and save new ownCloud account
1482 boolean isOAuth
= AccountTypeUtils
.
1483 getAuthTokenTypeAccessToken(MainApp
.getAccountType()).equals(mAuthTokenType
);
1484 boolean isSaml
= AccountTypeUtils
.
1485 getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).equals(mAuthTokenType
);
1487 String lastPermanentLocation
= authResult
.getLastPermanentLocation();
1488 if (lastPermanentLocation
!= null
) {
1489 mServerInfo
.mBaseUrl
= AccountUtils
.trimWebdavSuffix(lastPermanentLocation
);
1492 Uri uri
= Uri
.parse(mServerInfo
.mBaseUrl
);
1493 String username
= mUsernameInput
.getText().toString().trim();
1495 username
= "OAuth_user" + (new java
.util
.Random(System
.currentTimeMillis())).nextLong();
1497 String accountName
= com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.
1498 buildAccountName(uri
, username
);
1499 Account newAccount
= new Account(accountName
, MainApp
.getAccountType());
1500 if (AccountUtils
.exists(newAccount
, getApplicationContext())) {
1501 // fail - not a new account, but an existing one; disallow
1502 RemoteOperationResult result
= new RemoteOperationResult(ResultCode
.ACCOUNT_NOT_NEW
);
1503 updateAuthStatusIconAndText(result
);
1505 Log_OC
.d(TAG
, result
.getLogMessage());
1509 mAccount
= newAccount
;
1511 if (isOAuth
|| isSaml
) {
1512 // with external authorizations, the password is never input in the app
1513 mAccountMgr
.addAccountExplicitly(mAccount
, "", null
);
1515 mAccountMgr
.addAccountExplicitly(
1516 mAccount
, mPasswordInput
.getText().toString(), null
1520 // include account version with the new account
1521 mAccountMgr
.setUserData(
1523 Constants
.KEY_OC_ACCOUNT_VERSION
,
1524 Integer
.toString(AccountUtils
.ACCOUNT_VERSION
)
1527 /// add the new account as default in preferences, if there is none already
1528 Account defaultAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
1529 if (defaultAccount
== null
) {
1530 SharedPreferences
.Editor editor
= PreferenceManager
1531 .getDefaultSharedPreferences(this).edit();
1532 editor
.putString("select_oc_account", accountName
);
1536 /// prepare result to return to the Authenticator
1537 // TODO check again what the Authenticator makes with it; probably has the same
1538 // effect as addAccountExplicitly, but it's not well done
1539 final Intent intent
= new Intent();
1540 intent
.putExtra(AccountManager
.KEY_ACCOUNT_TYPE
, MainApp
.getAccountType());
1541 intent
.putExtra(AccountManager
.KEY_ACCOUNT_NAME
, mAccount
.name
);
1542 intent
.putExtra(AccountManager
.KEY_USERDATA
, username
);
1543 if (isOAuth
|| isSaml
) {
1544 mAccountMgr
.setAuthToken(mAccount
, mAuthTokenType
, mAuthToken
);
1546 /// add user data to the new account; TODO probably can be done in the last parameter
1547 // addAccountExplicitly, or in KEY_USERDATA
1548 mAccountMgr
.setUserData(
1549 mAccount
, Constants
.KEY_OC_VERSION
, mServerInfo
.mVersion
.getVersion()
1551 mAccountMgr
.setUserData(
1552 mAccount
, Constants
.KEY_OC_BASE_URL
, mServerInfo
.mBaseUrl
1556 mAccountMgr
.setUserData(mAccount
, Constants
.KEY_SUPPORTS_SAML_WEB_SSO
, "TRUE");
1557 } else if (isOAuth
) {
1558 mAccountMgr
.setUserData(mAccount
, Constants
.KEY_SUPPORTS_OAUTH2
, "TRUE");
1561 setAccountAuthenticatorResult(intent
.getExtras());
1562 setResult(RESULT_OK
, intent
);
1570 * Starts and activity to open the 'new account' page in the ownCloud web site
1572 * @param view 'Account register' button
1574 public void onRegisterClick(View view
) {
1575 Intent register
= new Intent(
1576 Intent
.ACTION_VIEW
, Uri
.parse(getString(R
.string
.welcome_link_url
))
1578 setResult(RESULT_CANCELED
);
1579 startActivity(register
);
1584 * Updates the content and visibility state of the icon and text associated
1585 * to the last check on the ownCloud server.
1588 private void showServerStatus() {
1589 if (mServerStatusIcon
== 0 && mServerStatusText
== 0) {
1590 mServerStatusView
.setVisibility(View
.INVISIBLE
);
1593 mServerStatusView
.setText(mServerStatusText
);
1594 mServerStatusView
.setCompoundDrawablesWithIntrinsicBounds(mServerStatusIcon
, 0, 0, 0);
1595 mServerStatusView
.setVisibility(View
.VISIBLE
);
1602 * Updates the content and visibility state of the icon and text associated
1603 * to the interactions with the OAuth authorization server.
1605 private void showAuthStatus() {
1606 if (mAuthStatusIcon
== 0 && mAuthStatusText
== 0) {
1607 mAuthStatusView
.setVisibility(View
.INVISIBLE
);
1610 mAuthStatusView
.setText(mAuthStatusText
);
1611 mAuthStatusView
.setCompoundDrawablesWithIntrinsicBounds(mAuthStatusIcon
, 0, 0, 0);
1612 mAuthStatusView
.setVisibility(View
.VISIBLE
);
1617 private void showRefreshButton (boolean show
) {
1619 mRefreshButton
.setVisibility(View
.VISIBLE
);
1621 mRefreshButton
.setVisibility(View
.GONE
);
1626 * Called when the eye icon in the password field is clicked.
1628 * Toggles the visibility of the password in the field.
1630 public void onViewPasswordClick() {
1631 int selectionStart
= mPasswordInput
.getSelectionStart();
1632 int selectionEnd
= mPasswordInput
.getSelectionEnd();
1633 if (isPasswordVisible()) {
1638 mPasswordInput
.setSelection(selectionStart
, selectionEnd
);
1643 * Called when the checkbox for OAuth authorization is clicked.
1645 * Hides or shows the input fields for user & password.
1647 * @param view 'View password' 'button'
1649 public void onCheckClick(View view
) {
1650 CheckBox oAuth2Check
= (CheckBox
)view
;
1651 if (oAuth2Check
.isChecked()) {
1652 mAuthTokenType
= OAUTH_TOKEN_TYPE
;
1654 mAuthTokenType
= BASIC_TOKEN_TYPE
;
1656 updateAuthenticationPreFragmentVisibility();
1661 * Called when the 'action' button in an IME is pressed ('enter' in software keyboard).
1663 * Used to trigger the authentication check when the user presses 'enter' after writing the
1664 * password, or to throw the server test when the only field on screen is the URL input field.
1667 public boolean onEditorAction(TextView inputField
, int actionId
, KeyEvent event
) {
1668 if (actionId
== EditorInfo
.IME_ACTION_DONE
&& inputField
!= null
&&
1669 inputField
.equals(mPasswordInput
)) {
1670 if (mOkButton
.isEnabled()) {
1671 mOkButton
.performClick();
1674 } else if (actionId
== EditorInfo
.IME_ACTION_NEXT
&& inputField
!= null
&&
1675 inputField
.equals(mHostUrlInput
)) {
1676 if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).
1677 equals(mAuthTokenType
)) {
1681 return false
; // always return false to grant that the software keyboard is hidden anyway
1685 private abstract static class RightDrawableOnTouchListener
implements OnTouchListener
{
1687 private int fuzz
= 75;
1693 public boolean onTouch(View view
, MotionEvent event
) {
1694 Drawable rightDrawable
= null
;
1695 if (view
instanceof TextView
) {
1696 Drawable
[] drawables
= ((TextView
)view
).getCompoundDrawables();
1697 if (drawables
.length
> 2) {
1698 rightDrawable
= drawables
[2];
1701 if (rightDrawable
!= null
) {
1702 final int x
= (int) event
.getX();
1703 final int y
= (int) event
.getY();
1704 final Rect bounds
= rightDrawable
.getBounds();
1705 if ( x
>= (view
.getRight() - bounds
.width() - fuzz
) &&
1706 x
<= (view
.getRight() - view
.getPaddingRight() + fuzz
) &&
1707 y
>= (view
.getPaddingTop() - fuzz
) &&
1708 y
<= (view
.getHeight() - view
.getPaddingBottom()) + fuzz
) {
1710 return onDrawableTouch(event
);
1716 public abstract boolean onDrawableTouch(final MotionEvent event
);
1720 private void getRemoteUserNameOperation(String sessionCookie
) {
1722 Intent getUserNameIntent
= new Intent();
1723 getUserNameIntent
.setAction(OperationsService
.ACTION_GET_USER_NAME
);
1724 getUserNameIntent
.putExtra(OperationsService
.EXTRA_SERVER_URL
, mServerInfo
.mBaseUrl
);
1725 getUserNameIntent
.putExtra(OperationsService
.EXTRA_COOKIE
, sessionCookie
);
1727 if (mOperationsServiceBinder
!= null
) {
1728 mWaitingForOpId
= mOperationsServiceBinder
.queueNewOperation(getUserNameIntent
);
1734 public void onSsoFinished(String sessionCookie
) {
1735 if (sessionCookie
!= null
&& sessionCookie
.length() > 0) {
1736 Log_OC
.d(TAG
, "Successful SSO - time to save the account");
1737 mAuthToken
= sessionCookie
;
1738 getRemoteUserNameOperation(sessionCookie
);
1739 Fragment fd
= getSupportFragmentManager().findFragmentByTag(SAML_DIALOG_TAG
);
1740 if (fd
!= null
&& fd
instanceof DialogFragment
) {
1741 Dialog d
= ((DialogFragment
)fd
).getDialog();
1742 if (d
!= null
&& d
.isShowing()) {
1749 Log_OC
.d(TAG
, "SSO failed");
1755 public boolean onTouchEvent(MotionEvent event
) {
1756 if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).
1757 equals(mAuthTokenType
) &&
1758 mHostUrlInput
.hasFocus() && event
.getAction() == MotionEvent
.ACTION_DOWN
) {
1761 return super.onTouchEvent(event
);
1766 * Show untrusted cert dialog
1768 public void showUntrustedCertDialog(
1769 X509Certificate x509Certificate
, SslError error
, SslErrorHandler handler
1771 // Show a dialog with the certificate info
1772 SslUntrustedCertDialog dialog
;
1773 if (x509Certificate
== null
) {
1774 dialog
= SslUntrustedCertDialog
.newInstanceForEmptySslError(error
, handler
);
1776 dialog
= SslUntrustedCertDialog
.
1777 newInstanceForFullSslError(x509Certificate
, error
, handler
);
1779 FragmentManager fm
= getSupportFragmentManager();
1780 FragmentTransaction ft
= fm
.beginTransaction();
1781 ft
.addToBackStack(null
);
1782 dialog
.show(ft
, UNTRUSTED_CERT_DIALOG_TAG
);
1787 * Show untrusted cert dialog
1789 private void showUntrustedCertDialog(RemoteOperationResult result
) {
1790 // Show a dialog with the certificate info
1791 SslUntrustedCertDialog dialog
= SslUntrustedCertDialog
.
1792 newInstanceForFullSslError((CertificateCombinedException
)result
.getException());
1793 FragmentManager fm
= getSupportFragmentManager();
1794 FragmentTransaction ft
= fm
.beginTransaction();
1795 ft
.addToBackStack(null
);
1796 dialog
.show(ft
, UNTRUSTED_CERT_DIALOG_TAG
);
1801 * Called from SslValidatorDialog when a new server certificate was correctly saved.
1803 public void onSavedCertificate() {
1804 Fragment fd
= getSupportFragmentManager().findFragmentByTag(SAML_DIALOG_TAG
);
1806 // if SAML dialog is not shown,
1807 // the SslDialog was shown due to an SSL error in the server check
1813 * Called from SslValidatorDialog when a new server certificate could not be saved
1814 * when the user requested it.
1817 public void onFailedSavingCertificate() {
1818 dismissDialog(SAML_DIALOG_TAG
);
1819 Toast
.makeText(this, R
.string
.ssl_validator_not_saved
, Toast
.LENGTH_LONG
).show();
1823 public void onCancelCertificate() {
1824 dismissDialog(SAML_DIALOG_TAG
);
1828 private void doOnResumeAndBound() {
1829 //Log_OC.wtf(TAG, "registering to listen for operation callbacks" );
1830 mOperationsServiceBinder
.addOperationListener(AuthenticatorActivity
.this, mHandler
);
1831 if (mWaitingForOpId
<= Integer
.MAX_VALUE
) {
1832 mOperationsServiceBinder
.dispatchResultIfFinished((int)mWaitingForOpId
, this);
1835 if (mPendingAutoCheck
) {
1841 private void dismissDialog(String dialogTag
){
1842 Fragment frag
= getSupportFragmentManager().findFragmentByTag(dialogTag
);
1843 if (frag
!= null
&& frag
instanceof DialogFragment
) {
1844 DialogFragment dialog
= (DialogFragment
) frag
;
1851 * Implements callback methods for service binding.
1853 private class OperationsServiceConnection
implements ServiceConnection
{
1856 public void onServiceConnected(ComponentName component
, IBinder service
) {
1857 if (component
.equals(
1858 new ComponentName(AuthenticatorActivity
.this, OperationsService
.class)
1860 mOperationsServiceBinder
= (OperationsServiceBinder
) service
;
1862 doOnResumeAndBound();
1869 public void onServiceDisconnected(ComponentName component
) {
1870 if (component
.equals(
1871 new ComponentName(AuthenticatorActivity
.this, OperationsService
.class)
1873 Log_OC
.e(TAG
, "Operations service crashed");
1874 mOperationsServiceBinder
= null
;
1881 * Create and show dialog for request authentication to the user
1882 * @param webView Web view to emebd into the authentication dialog.
1883 * @param handler Object responsible for catching and recovering HTTP authentication fails.
1885 public void createAuthenticationDialog(WebView webView
, HttpAuthHandler handler
) {
1887 // Show a dialog with the certificate info
1888 CredentialsDialogFragment dialog
=
1889 CredentialsDialogFragment
.newInstanceForCredentials(webView
, handler
);
1890 FragmentManager fm
= getSupportFragmentManager();
1891 FragmentTransaction ft
= fm
.beginTransaction();
1892 ft
.addToBackStack(null
);
1893 dialog
.setCancelable(false
);
1894 dialog
.show(ft
, CREDENTIALS_DIALOG_TAG
);
1896 if (!mIsFirstAuthAttempt
) {
1898 getApplicationContext(),
1899 getText(R
.string
.saml_authentication_wrong_pass
),
1903 mIsFirstAuthAttempt
= false
;
1908 * For retrieving the clicking on authentication cancel button
1910 public void doNegativeAuthenticatioDialogClick(){
1911 mIsFirstAuthAttempt
= true
;