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
) {
273 mOkButton
= findViewById(R
.id
.buttonOK
);
275 /// initialize block to be moved to single Fragment to check server and get info about it
276 initServerPreFragment(savedInstanceState
);
278 /// initialize block to be moved to single Fragment to retrieve and validate credentials
279 initAuthorizationPreFragment(savedInstanceState
);
281 //Log_OC.wtf(TAG, "onCreate end");
284 private void initAuthTokenType() {
286 getIntent().getExtras().getString(AccountAuthenticator
.KEY_AUTH_TOKEN_TYPE
);
287 if (mAuthTokenType
== null
) {
288 if (mAccount
!= null
) {
289 boolean oAuthRequired
=
290 (mAccountMgr
.getUserData(mAccount
, Constants
.KEY_SUPPORTS_OAUTH2
) != null
);
291 boolean samlWebSsoRequired
= (
292 mAccountMgr
.getUserData(
293 mAccount
, Constants
.KEY_SUPPORTS_SAML_WEB_SSO
296 mAuthTokenType
= chooseAuthTokenType(oAuthRequired
, samlWebSsoRequired
);
299 boolean oAuthSupported
= AUTH_ON
.equals(getString(R
.string
.auth_method_oauth2
));
300 boolean samlWebSsoSupported
=
301 AUTH_ON
.equals(getString(R
.string
.auth_method_saml_web_sso
));
302 mAuthTokenType
= chooseAuthTokenType(oAuthSupported
, samlWebSsoSupported
);
307 private String
chooseAuthTokenType(boolean oauth
, boolean saml
) {
309 return SAML_TOKEN_TYPE
;
311 return OAUTH_TOKEN_TYPE
;
313 return BASIC_TOKEN_TYPE
;
319 * Configures elements in the user interface under direct control of the Activity.
321 private void initOverallUi() {
323 /// step 1 - load and process relevant inputs (resources, intent, savedInstanceState)
324 boolean isWelcomeLinkVisible
= getResources().getBoolean(R
.bool
.show_welcome_link
);
326 String instructionsMessageText
= null
;
327 if (mAction
== ACTION_UPDATE_EXPIRED_TOKEN
) {
328 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType())
329 .equals(mAuthTokenType
)) {
330 instructionsMessageText
= getString(R
.string
.auth_expired_oauth_token_toast
);
332 } else if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType())
333 .equals(mAuthTokenType
)) {
334 instructionsMessageText
= getString(R
.string
.auth_expired_saml_sso_token_toast
);
337 instructionsMessageText
= getString(R
.string
.auth_expired_basic_auth_toast
);
341 /// step 2 - set properties of UI elements (text, visibility, enabled...)
342 Button welcomeLink
= (Button
) findViewById(R
.id
.welcome_link
);
343 welcomeLink
.setVisibility(isWelcomeLinkVisible ? View
.VISIBLE
: View
.GONE
);
345 String
.format(getString(R
.string
.auth_register
), getString(R
.string
.app_name
)));
347 TextView instructionsView
= (TextView
) findViewById(R
.id
.instructions_message
);
348 if (instructionsMessageText
!= null
) {
349 instructionsView
.setVisibility(View
.VISIBLE
);
350 instructionsView
.setText(instructionsMessageText
);
352 instructionsView
.setVisibility(View
.GONE
);
359 * @param savedInstanceState Saved activity state, as in {{@link #onCreate(Bundle)}
361 private void initServerPreFragment(Bundle savedInstanceState
) {
363 /// step 1 - load and process relevant inputs (resources, intent, savedInstanceState)
364 boolean isUrlInputAllowed
= getResources().getBoolean(R
.bool
.show_server_url_input
);
365 if (savedInstanceState
== null
) {
366 if (mAccount
!= null
) {
367 mServerInfo
.mBaseUrl
= mAccountMgr
.getUserData(mAccount
, Constants
.KEY_OC_BASE_URL
);
368 // TODO do next in a setter for mBaseUrl
369 mServerInfo
.mIsSslConn
= mServerInfo
.mBaseUrl
.startsWith("https://");
370 mServerInfo
.mVersion
= AccountUtils
.getServerVersion(mAccount
);
372 mServerInfo
.mBaseUrl
= getString(R
.string
.server_url
).trim();
373 mServerInfo
.mIsSslConn
= mServerInfo
.mBaseUrl
.startsWith("https://");
376 mServerStatusText
= savedInstanceState
.getInt(KEY_SERVER_STATUS_TEXT
);
377 mServerStatusIcon
= savedInstanceState
.getInt(KEY_SERVER_STATUS_ICON
);
379 mServerIsValid
= savedInstanceState
.getBoolean(KEY_SERVER_VALID
);
380 mServerIsChecked
= savedInstanceState
.getBoolean(KEY_SERVER_CHECKED
);
383 mServerInfo
.mIsSslConn
= savedInstanceState
.getBoolean(KEY_IS_SSL_CONN
);
384 mServerInfo
.mBaseUrl
= savedInstanceState
.getString(KEY_HOST_URL_TEXT
);
385 String ocVersion
= savedInstanceState
.getString(KEY_OC_VERSION
);
386 if (ocVersion
!= null
) {
387 mServerInfo
.mVersion
= new OwnCloudVersion(ocVersion
);
389 mServerInfo
.mAuthMethod
= AuthenticationMethod
.valueOf(
390 savedInstanceState
.getString(KEY_SERVER_AUTH_METHOD
));
394 /// step 2 - set properties of UI elements (text, visibility, enabled...)
395 mHostUrlInput
= (EditText
) findViewById(R
.id
.hostUrlInput
);
396 // Convert IDN to Unicode
397 mHostUrlInput
.setText(DisplayUtils
.convertIdn(mServerInfo
.mBaseUrl
, false
));
398 if (mAction
!= ACTION_CREATE
) {
399 /// lock things that should not change
400 mHostUrlInput
.setEnabled(false
);
401 mHostUrlInput
.setFocusable(false
);
403 if (isUrlInputAllowed
) {
404 mRefreshButton
= findViewById(R
.id
.embeddedRefreshButton
);
406 findViewById(R
.id
.hostUrlFrame
).setVisibility(View
.GONE
);
407 mRefreshButton
= findViewById(R
.id
.centeredRefreshButton
);
409 showRefreshButton(mServerIsChecked
&& !mServerIsValid
&&
410 mWaitingForOpId
> Integer
.MAX_VALUE
);
411 mServerStatusView
= (TextView
) findViewById(R
.id
.server_status_text
);
414 /// step 3 - bind some listeners and options
415 mHostUrlInput
.setImeOptions(EditorInfo
.IME_ACTION_NEXT
);
416 mHostUrlInput
.setOnEditorActionListener(this);
418 /// step 4 - create listeners that will be bound at onResume
419 mHostUrlInputWatcher
= new TextWatcher() {
422 public void afterTextChanged(Editable s
) {
423 if (mOkButton
.isEnabled() &&
424 !mServerInfo
.mBaseUrl
.equals(
425 normalizeUrl(s
.toString(), mServerInfo
.mIsSslConn
))) {
426 mOkButton
.setEnabled(false
);
431 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
435 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {
436 if (mAuthStatusIcon
!= 0) {
437 Log_OC
.d(TAG
, "onTextChanged: hiding authentication status");
446 // TODO find out if this is really necessary, or if it can done in a different way
447 findViewById(R
.id
.scroll
).setOnTouchListener(new OnTouchListener() {
449 public boolean onTouch(View view
, MotionEvent event
) {
450 if (event
.getAction() == MotionEvent
.ACTION_DOWN
) {
452 AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(
453 MainApp
.getAccountType()
454 ).equals(mAuthTokenType
) &&
455 mHostUrlInput
.hasFocus()
465 /// step 4 - mark automatic check to be started when OperationsService is ready
466 mPendingAutoCheck
= (savedInstanceState
== null
&&
467 (mAction
!= ACTION_CREATE
|| !isUrlInputAllowed
));
473 * @param savedInstanceState Saved activity state, as in {{@link #onCreate(Bundle)}
475 private void initAuthorizationPreFragment(Bundle savedInstanceState
) {
477 /// step 0 - get UI elements in layout
478 mOAuth2Check
= (CheckBox
) findViewById(R
.id
.oauth_onOff_check
);
479 mOAuthAuthEndpointText
= (TextView
)findViewById(R
.id
.oAuthEntryPoint_1
);
480 mOAuthTokenEndpointText
= (TextView
)findViewById(R
.id
.oAuthEntryPoint_2
);
481 mUsernameInput
= (EditText
) findViewById(R
.id
.account_username
);
482 mPasswordInput
= (EditText
) findViewById(R
.id
.account_password
);
483 mAuthStatusView
= (TextView
) findViewById(R
.id
.auth_status_text
);
485 /// step 1 - load and process relevant inputs (resources, intent, savedInstanceState)
486 String presetUserName
= null
;
487 boolean isPasswordExposed
= false
;
488 if (savedInstanceState
== null
) {
489 if (mAccount
!= null
) {
490 presetUserName
= mAccount
.name
.substring(0, mAccount
.name
.lastIndexOf('@'));
494 isPasswordExposed
= savedInstanceState
.getBoolean(KEY_PASSWORD_EXPOSED
, false
);
495 mAuthStatusText
= savedInstanceState
.getInt(KEY_AUTH_STATUS_TEXT
);
496 mAuthStatusIcon
= savedInstanceState
.getInt(KEY_AUTH_STATUS_ICON
);
497 mAuthToken
= savedInstanceState
.getString(KEY_AUTH_TOKEN
);
500 /// step 2 - set properties of UI elements (text, visibility, enabled...)
501 mOAuth2Check
.setChecked(
502 AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType())
503 .equals(mAuthTokenType
));
504 if (presetUserName
!= null
) {
505 mUsernameInput
.setText(presetUserName
);
507 if (mAction
!= ACTION_CREATE
) {
508 mUsernameInput
.setEnabled(false
);
509 mUsernameInput
.setFocusable(false
);
511 mPasswordInput
.setText(""); // clean password to avoid social hacking
512 if (isPasswordExposed
) {
515 updateAuthenticationPreFragmentVisibility();
517 mOkButton
.setEnabled(mServerIsValid
);
520 /// step 3 - bind listeners
521 // bindings for password input field
522 mPasswordInput
.setOnFocusChangeListener(this);
523 mPasswordInput
.setImeOptions(EditorInfo
.IME_ACTION_DONE
);
524 mPasswordInput
.setOnEditorActionListener(this);
525 mPasswordInput
.setOnTouchListener(new RightDrawableOnTouchListener() {
527 public boolean onDrawableTouch(final MotionEvent event
) {
528 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
529 AuthenticatorActivity
.this.onViewPasswordClick();
539 * Changes the visibility of input elements depending on
540 * the current authorization method.
542 private void updateAuthenticationPreFragmentVisibility () {
543 if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).
544 equals(mAuthTokenType
)) {
545 // SAML-based web Single Sign On
546 mOAuth2Check
.setVisibility(View
.GONE
);
547 mOAuthAuthEndpointText
.setVisibility(View
.GONE
);
548 mOAuthTokenEndpointText
.setVisibility(View
.GONE
);
549 mUsernameInput
.setVisibility(View
.GONE
);
550 mPasswordInput
.setVisibility(View
.GONE
);
553 if (mAction
== ACTION_CREATE
&&
554 AUTH_OPTIONAL
.equals(getString(R
.string
.auth_method_oauth2
))) {
555 mOAuth2Check
.setVisibility(View
.VISIBLE
);
557 mOAuth2Check
.setVisibility(View
.GONE
);
560 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType()).
561 equals(mAuthTokenType
)) {
562 // OAuth 2 authorization
564 mOAuthAuthEndpointText
.setVisibility(View
.VISIBLE
);
565 mOAuthTokenEndpointText
.setVisibility(View
.VISIBLE
);
566 mUsernameInput
.setVisibility(View
.GONE
);
567 mPasswordInput
.setVisibility(View
.GONE
);
570 // basic HTTP authorization
571 mOAuthAuthEndpointText
.setVisibility(View
.GONE
);
572 mOAuthTokenEndpointText
.setVisibility(View
.GONE
);
573 mUsernameInput
.setVisibility(View
.VISIBLE
);
574 mPasswordInput
.setVisibility(View
.VISIBLE
);
582 * Saves relevant state before {@link #onPause()}
584 * Do NOT save {@link #mNewCapturedUriFromOAuth2Redirection}; it keeps a temporal flag,
585 * intended to defer the processing of the redirection caught in
586 * {@link #onNewIntent(Intent)} until {@link #onResume()}
588 * See {@link super#onSaveInstanceState(Bundle)}
591 protected void onSaveInstanceState(Bundle outState
) {
592 //Log_OC.wtf(TAG, "onSaveInstanceState init" );
593 super.onSaveInstanceState(outState
);
596 outState
.putString(KEY_AUTH_TOKEN_TYPE
, mAuthTokenType
);
597 outState
.putLong(KEY_WAITING_FOR_OP_ID
, mWaitingForOpId
);
599 /// Server PRE-fragment state
600 outState
.putInt(KEY_SERVER_STATUS_TEXT
, mServerStatusText
);
601 outState
.putInt(KEY_SERVER_STATUS_ICON
, mServerStatusIcon
);
602 outState
.putBoolean(KEY_SERVER_CHECKED
, mServerIsChecked
);
603 outState
.putBoolean(KEY_SERVER_VALID
, mServerIsValid
);
604 outState
.putBoolean(KEY_IS_SSL_CONN
, mServerInfo
.mIsSslConn
);
605 outState
.putString(KEY_HOST_URL_TEXT
, mServerInfo
.mBaseUrl
);
606 if (mServerInfo
.mVersion
!= null
) {
607 outState
.putString(KEY_OC_VERSION
, mServerInfo
.mVersion
.getVersion());
609 outState
.putString(KEY_SERVER_AUTH_METHOD
, mServerInfo
.mAuthMethod
.name());
611 /// Authentication PRE-fragment state
612 outState
.putBoolean(KEY_PASSWORD_EXPOSED
, isPasswordVisible());
613 outState
.putInt(KEY_AUTH_STATUS_ICON
, mAuthStatusIcon
);
614 outState
.putInt(KEY_AUTH_STATUS_TEXT
, mAuthStatusText
);
615 outState
.putString(KEY_AUTH_TOKEN
, mAuthToken
);
618 outState
.putBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG
, mIsFirstAuthAttempt
);
620 /// AsyncTask (User and password)
621 outState
.putString(KEY_USERNAME
, mUsernameInput
.getText().toString());
622 outState
.putString(KEY_PASSWORD
, mPasswordInput
.getText().toString());
624 if (mAsyncTask
!= null
) {
625 mAsyncTask
.cancel(true
);
626 outState
.putBoolean(KEY_ASYNC_TASK_IN_PROGRESS
, true
);
628 outState
.putBoolean(KEY_ASYNC_TASK_IN_PROGRESS
, false
);
632 //Log_OC.wtf(TAG, "onSaveInstanceState end" );
636 public void onRestoreInstanceState(Bundle savedInstanceState
) {
637 super.onRestoreInstanceState(savedInstanceState
);
640 boolean inProgress
= savedInstanceState
.getBoolean(KEY_ASYNC_TASK_IN_PROGRESS
);
642 String username
= savedInstanceState
.getString(KEY_USERNAME
);
643 String password
= savedInstanceState
.getString(KEY_PASSWORD
);
645 OwnCloudCredentials credentials
= null
;
646 if (BASIC_TOKEN_TYPE
.equals(mAuthTokenType
)) {
647 credentials
= OwnCloudCredentialsFactory
.newBasicCredentials(username
, password
);
649 } else if (OAUTH_TOKEN_TYPE
.equals(mAuthTokenType
)) {
650 credentials
= OwnCloudCredentialsFactory
.newBearerCredentials(mAuthToken
);
653 accessRootFolder(credentials
);
658 * The redirection triggered by the OAuth authentication server as response to the
659 * GET AUTHORIZATION request is caught here.
661 * To make this possible, this activity needs to be qualified with android:launchMode =
662 * "singleTask" in the AndroidManifest.xml file.
665 protected void onNewIntent (Intent intent
) {
666 Log_OC
.d(TAG
, "onNewIntent()");
667 Uri data
= intent
.getData();
668 if (data
!= null
&& data
.toString().startsWith(getString(R
.string
.oauth2_redirect_uri
))) {
669 mNewCapturedUriFromOAuth2Redirection
= data
;
675 * The redirection triggered by the OAuth authentication server as response to the
676 * GET AUTHORIZATION, and deferred in {@link #onNewIntent(Intent)}, is processed here.
679 protected void onResume() {
682 // bound here to avoid spurious changes triggered by Android on device rotations
683 mHostUrlInput
.setOnFocusChangeListener(this);
684 mHostUrlInput
.addTextChangedListener(mHostUrlInputWatcher
);
686 if (mNewCapturedUriFromOAuth2Redirection
!= null
) {
687 getOAuth2AccessTokenFromCapturedRedirection();
690 if (mOperationsServiceBinder
!= null
) {
691 doOnResumeAndBound();
698 protected void onPause() {
699 if (mOperationsServiceBinder
!= null
) {
700 mOperationsServiceBinder
.removeOperationListener(this);
703 mHostUrlInput
.removeTextChangedListener(mHostUrlInputWatcher
);
704 mHostUrlInput
.setOnFocusChangeListener(null
);
710 protected void onDestroy() {
712 mHostUrlInputWatcher
= null
;
714 if (mOperationsServiceConnection
!= null
) {
715 unbindService(mOperationsServiceConnection
);
716 mOperationsServiceBinder
= null
;
723 * Parses the redirection with the response to the GET AUTHORIZATION request to the
724 * oAuth server and requests for the access token (GET ACCESS TOKEN)
726 private void getOAuth2AccessTokenFromCapturedRedirection() {
727 /// Parse data from OAuth redirection
728 String queryParameters
= mNewCapturedUriFromOAuth2Redirection
.getQuery();
729 mNewCapturedUriFromOAuth2Redirection
= null
;
731 /// Showing the dialog with instructions for the user.
732 IndeterminateProgressDialog dialog
=
733 IndeterminateProgressDialog
.newInstance(R
.string
.auth_getting_authorization
, true
);
734 dialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
736 /// GET ACCESS TOKEN to the oAuth server
737 Intent getServerInfoIntent
= new Intent();
738 getServerInfoIntent
.setAction(OperationsService
.ACTION_OAUTH2_GET_ACCESS_TOKEN
);
740 getServerInfoIntent
.putExtra(
741 OperationsService
.EXTRA_SERVER_URL
,
742 mOAuthTokenEndpointText
.getText().toString().trim());
744 getServerInfoIntent
.putExtra(
745 OperationsService
.EXTRA_OAUTH2_QUERY_PARAMETERS
,
748 if (mOperationsServiceBinder
!= null
) {
749 //Log_OC.wtf(TAG, "getting access token..." );
750 mWaitingForOpId
= mOperationsServiceBinder
.queueNewOperation(getServerInfoIntent
);
757 * Handles the change of focus on the text inputs for the server URL and the password
759 public void onFocusChange(View view
, boolean hasFocus
) {
760 if (view
.getId() == R
.id
.hostUrlInput
) {
762 onUrlInputFocusLost();
765 showRefreshButton(false
);
768 } else if (view
.getId() == R
.id
.account_password
) {
769 onPasswordFocusChanged(hasFocus
);
775 * Handles changes in focus on the text input for the server URL.
777 * IMPORTANT ENTRY POINT 2: When (!hasFocus), user wrote the server URL and changed to
778 * other field. The operation to check the existence of the server in the entered URL is
781 * When hasFocus: user 'comes back' to write again the server URL.
783 private void onUrlInputFocusLost() {
784 if (!mServerInfo
.mBaseUrl
.equals(
785 normalizeUrl(mHostUrlInput
.getText().toString(), mServerInfo
.mIsSslConn
))) {
786 // check server again only if the user changed something in the field
789 mOkButton
.setEnabled(mServerIsValid
);
790 showRefreshButton(!mServerIsValid
);
795 private void checkOcServer() {
796 String uri
= mHostUrlInput
.getText().toString().trim();
797 mServerIsValid
= false
;
798 mServerIsChecked
= false
;
799 mOkButton
.setEnabled(false
);
800 mServerInfo
= new GetServerInfoOperation
.ServerInfo();
801 showRefreshButton(false
);
803 if (uri
.length() != 0) {
804 uri
= stripIndexPhpOrAppsFiles(uri
, mHostUrlInput
);
806 // Handle internationalized domain names
807 uri
= DisplayUtils
.convertIdn(uri
, true
);
809 mServerStatusText
= R
.string
.auth_testing_connection
;
810 mServerStatusIcon
= R
.drawable
.progress_small
;
813 Intent getServerInfoIntent
= new Intent();
814 getServerInfoIntent
.setAction(OperationsService
.ACTION_GET_SERVER_INFO
);
815 getServerInfoIntent
.putExtra(
816 OperationsService
.EXTRA_SERVER_URL
,
817 normalizeUrlSuffix(uri
)
819 if (mOperationsServiceBinder
!= null
) {
820 mWaitingForOpId
= mOperationsServiceBinder
.queueNewOperation(getServerInfoIntent
);
822 Log_OC
.wtf(TAG
, "Server check tried with OperationService unbound!" );
826 mServerStatusText
= 0;
827 mServerStatusIcon
= 0;
834 * Handles changes in focus on the text input for the password (basic authorization).
836 * When (hasFocus), the button to toggle password visibility is shown.
838 * When (!hasFocus), the button is made invisible and the password is hidden.
840 * @param hasFocus 'True' if focus is received, 'false' if is lost
842 private void onPasswordFocusChanged(boolean hasFocus
) {
844 showViewPasswordButton();
847 hidePasswordButton();
852 private void showViewPasswordButton() {
853 int drawable
= R
.drawable
.ic_view
;
854 if (isPasswordVisible()) {
855 drawable
= R
.drawable
.ic_hide
;
857 mPasswordInput
.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawable
, 0);
860 private boolean isPasswordVisible() {
861 return ((mPasswordInput
.getInputType() & InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
) ==
862 InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
);
865 private void hidePasswordButton() {
866 mPasswordInput
.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
869 private void showPassword() {
870 mPasswordInput
.setInputType(
871 InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
873 showViewPasswordButton();
876 private void hidePassword() {
877 mPasswordInput
.setInputType(
878 InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_PASSWORD
880 showViewPasswordButton();
884 * Checks the credentials of the user in the root of the ownCloud server
885 * before creating a new local account.
887 * For basic authorization, a check of existence of the root folder is
890 * For OAuth, starts the flow to get an access token; the credentials test
891 * is postponed until it is available.
893 * IMPORTANT ENTRY POINT 4
895 public void onOkClick() {
896 // this check should be unnecessary
897 if (mServerInfo
.mVersion
== null
||
898 !mServerInfo
.mVersion
.isVersionValid() ||
899 mServerInfo
.mBaseUrl
== null
||
900 mServerInfo
.mBaseUrl
.length() == 0) {
901 mServerStatusIcon
= R
.drawable
.common_error
;
902 mServerStatusText
= R
.string
.auth_wtf_reenter_URL
;
904 mOkButton
.setEnabled(false
);
908 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType()).
909 equals(mAuthTokenType
)) {
911 startOauthorization();
912 } else if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).
913 equals(mAuthTokenType
)) {
915 startSamlBasedFederatedSingleSignOnAuthorization();
917 checkBasicAuthorization();
923 * Tests the credentials entered by the user performing a check of existence on
924 * the root folder of the ownCloud server.
926 private void checkBasicAuthorization() {
927 /// get basic credentials entered by user
928 String username
= mUsernameInput
.getText().toString();
929 String password
= mPasswordInput
.getText().toString();
931 /// be gentle with the user
932 IndeterminateProgressDialog dialog
=
933 IndeterminateProgressDialog
.newInstance(R
.string
.auth_trying_to_login
, true
);
934 dialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
936 /// validate credentials accessing the root folder
937 OwnCloudCredentials credentials
= OwnCloudCredentialsFactory
.newBasicCredentials(username
,
939 accessRootFolder(credentials
);
942 private void accessRootFolder(OwnCloudCredentials credentials
) {
943 mAsyncTask
= new AuthenticatorAsyncTask(this);
944 Object
[] params
= { mServerInfo
.mBaseUrl
, credentials
};
945 mAsyncTask
.execute(params
);
950 * Starts the OAuth 'grant type' flow to get an access token, with
951 * a GET AUTHORIZATION request to the BUILT-IN authorization server.
953 private void startOauthorization() {
954 // be gentle with the user
955 mAuthStatusIcon
= R
.drawable
.progress_small
;
956 mAuthStatusText
= R
.string
.oauth_login_connection
;
959 // GET AUTHORIZATION request
960 Uri uri
= Uri
.parse(mOAuthAuthEndpointText
.getText().toString().trim());
961 Uri
.Builder uriBuilder
= uri
.buildUpon();
962 uriBuilder
.appendQueryParameter(
963 OAuth2Constants
.KEY_RESPONSE_TYPE
, getString(R
.string
.oauth2_response_type
)
965 uriBuilder
.appendQueryParameter(
966 OAuth2Constants
.KEY_REDIRECT_URI
, getString(R
.string
.oauth2_redirect_uri
)
968 uriBuilder
.appendQueryParameter(
969 OAuth2Constants
.KEY_CLIENT_ID
, getString(R
.string
.oauth2_client_id
)
971 uriBuilder
.appendQueryParameter(
972 OAuth2Constants
.KEY_SCOPE
, getString(R
.string
.oauth2_scope
)
974 uri
= uriBuilder
.build();
975 Log_OC
.d(TAG
, "Starting browser to view " + uri
.toString());
976 Intent i
= new Intent(Intent
.ACTION_VIEW
, uri
);
982 * Starts the Web Single Sign On flow to get access to the root folder
985 private void startSamlBasedFederatedSingleSignOnAuthorization() {
986 /// be gentle with the user
987 mAuthStatusIcon
= R
.drawable
.progress_small
;
988 mAuthStatusText
= R
.string
.auth_connecting_auth_server
;
991 /// Show SAML-based SSO web dialog
992 String targetUrl
= mServerInfo
.mBaseUrl
993 + AccountUtils
.getWebdavPath(mServerInfo
.mVersion
, mAuthTokenType
);
994 SamlWebViewDialog dialog
= SamlWebViewDialog
.newInstance(targetUrl
, targetUrl
);
995 dialog
.show(getSupportFragmentManager(), SAML_DIALOG_TAG
);
999 * Callback method invoked when a RemoteOperation executed by this Activity finishes.
1001 * Dispatches the operation flow to the right method.
1004 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
1006 if (operation
instanceof GetServerInfoOperation
) {
1007 if (operation
.hashCode() == mWaitingForOpId
) {
1008 onGetServerInfoFinish(result
);
1009 } // else nothing ; only the last check operation is considered;
1010 // multiple can be started if the user amends a URL quickly
1012 } else if (operation
instanceof OAuth2GetAccessToken
) {
1013 onGetOAuthAccessTokenFinish(result
);
1015 } else if (operation
instanceof GetRemoteUserNameOperation
) {
1016 onGetUserNameFinish(result
);
1021 private void onGetUserNameFinish(RemoteOperationResult result
) {
1022 mWaitingForOpId
= Long
.MAX_VALUE
;
1023 if (result
.isSuccess()) {
1024 boolean success
= false
;
1025 String username
= (String
) result
.getData().get(0);
1027 if ( mAction
== ACTION_CREATE
) {
1028 mUsernameInput
.setText(username
);
1029 success
= createAccount(result
);
1032 if (!mUsernameInput
.getText().toString().equals(username
)) {
1033 // fail - not a new account, but an existing one; disallow
1034 result
= new RemoteOperationResult(ResultCode
.ACCOUNT_NOT_THE_SAME
);
1036 updateAuthStatusIconAndText(result
);
1038 Log_OC
.d(TAG
, result
.getLogMessage());
1041 updateAccountAuthentication();
1044 } catch (AccountNotFoundException e
) {
1045 Log_OC
.e(TAG
, "Account " + mAccount
+ " was removed!", e
);
1046 Toast
.makeText(this, R
.string
.auth_account_does_not_exist
,
1047 Toast
.LENGTH_SHORT
).show();
1056 updateStatusIconFailUserName();
1058 Log_OC
.e(TAG
, "Access to user name failed: " + result
.getLogMessage());
1064 * Processes the result of the server check performed when the user finishes the enter of the
1067 * @param result Result of the check.
1069 private void onGetServerInfoFinish(RemoteOperationResult result
) {
1070 /// update activity state
1071 mServerIsChecked
= true
;
1072 mWaitingForOpId
= Long
.MAX_VALUE
;
1074 // update server status, but don't show it yet
1075 updateServerStatusIconAndText(result
);
1077 if (result
.isSuccess()) {
1079 // 1. connection succeeded, and we know if it's SSL or not
1080 // 2. server is installed
1081 // 3. we got the server version
1082 // 4. we got the authentication method required by the server
1083 mServerInfo
= (GetServerInfoOperation
.ServerInfo
) (result
.getData().get(0));
1085 if (!authSupported(mServerInfo
.mAuthMethod
)) {
1087 updateServerStatusIconNoRegularAuth(); // overrides updateServerStatusIconAndText()
1088 mServerIsValid
= false
;
1091 mServerIsValid
= true
;
1095 mServerIsValid
= false
;
1099 showRefreshButton(!mServerIsValid
);
1101 mOkButton
.setEnabled(mServerIsValid
);
1103 /// very special case (TODO: move to a common place for all the remote operations)
1104 if (result
.getCode() == ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
) {
1105 showUntrustedCertDialog(result
);
1110 private boolean authSupported(AuthenticationMethod authMethod
) {
1111 return (( BASIC_TOKEN_TYPE
.equals(mAuthTokenType
) &&
1112 AuthenticationMethod
.BASIC_HTTP_AUTH
.equals(authMethod
) ) ||
1113 ( OAUTH_TOKEN_TYPE
.equals(mAuthTokenType
) &&
1114 AuthenticationMethod
.BEARER_TOKEN
.equals(authMethod
)) ||
1115 ( SAML_TOKEN_TYPE
.equals(mAuthTokenType
) &&
1116 AuthenticationMethod
.SAML_WEB_SSO
.equals(authMethod
))
1121 // TODO remove, if possible
1122 private String
normalizeUrl(String url
, boolean sslWhenUnprefixed
) {
1123 if (url
!= null
&& url
.length() > 0) {
1125 if (!url
.toLowerCase().startsWith("http://") &&
1126 !url
.toLowerCase().startsWith("https://")) {
1127 if (sslWhenUnprefixed
) {
1128 url
= "https://" + url
;
1130 url
= "http://" + url
;
1134 url
= normalizeUrlSuffix(url
);
1136 return (url
!= null ? url
: "");
1140 private String
normalizeUrlSuffix(String url
) {
1141 if (url
.endsWith("/")) {
1142 url
= url
.substring(0, url
.length() - 1);
1144 url
= trimUrlWebdav(url
);
1148 private String
stripIndexPhpOrAppsFiles(String url
, EditText mHostUrlInput
) {
1149 if (url
.endsWith("/index.php")) {
1150 url
= url
.substring(0, url
.lastIndexOf("/index.php"));
1151 mHostUrlInput
.setText(url
);
1152 } else if (url
.contains("/index.php/apps/")) {
1153 url
= url
.substring(0, url
.lastIndexOf("/index.php/apps/"));
1154 mHostUrlInput
.setText(url
);
1160 // TODO remove, if possible
1161 private String
trimUrlWebdav(String url
){
1162 if(url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_4_0_AND_LATER
)){
1163 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_4_0_AND_LATER
.length());
1170 * Chooses the right icon and text to show to the user for the received operation result.
1172 * @param result Result of a remote operation performed in this activity
1174 private void updateServerStatusIconAndText(RemoteOperationResult result
) {
1175 mServerStatusIcon
= R
.drawable
.common_error
; // the most common case in the switch below
1177 switch (result
.getCode()) {
1179 mServerStatusIcon
= R
.drawable
.ic_lock
;
1180 mServerStatusText
= R
.string
.auth_secure_connection
;
1185 if (mHostUrlInput
.getText().toString().trim().toLowerCase().startsWith("http://") ) {
1186 mServerStatusText
= R
.string
.auth_connection_established
;
1187 mServerStatusIcon
= R
.drawable
.ic_ok
;
1189 mServerStatusText
= R
.string
.auth_nossl_plain_ok_title
;
1190 mServerStatusIcon
= R
.drawable
.ic_lock_open
;
1194 case NO_NETWORK_CONNECTION
:
1195 mServerStatusIcon
= R
.drawable
.no_network
;
1196 mServerStatusText
= R
.string
.auth_no_net_conn_title
;
1199 case SSL_RECOVERABLE_PEER_UNVERIFIED
:
1200 mServerStatusText
= R
.string
.auth_ssl_unverified_server_title
;
1202 case BAD_OC_VERSION
:
1203 mServerStatusText
= R
.string
.auth_bad_oc_version_title
;
1205 case WRONG_CONNECTION
:
1206 mServerStatusText
= R
.string
.auth_wrong_connection_title
;
1209 mServerStatusText
= R
.string
.auth_timeout_title
;
1211 case INCORRECT_ADDRESS
:
1212 mServerStatusText
= R
.string
.auth_incorrect_address_title
;
1215 mServerStatusText
= R
.string
.auth_ssl_general_error_title
;
1218 mServerStatusText
= R
.string
.auth_unauthorized
;
1220 case HOST_NOT_AVAILABLE
:
1221 mServerStatusText
= R
.string
.auth_unknown_host_title
;
1223 case INSTANCE_NOT_CONFIGURED
:
1224 mServerStatusText
= R
.string
.auth_not_configured_title
;
1226 case FILE_NOT_FOUND
:
1227 mServerStatusText
= R
.string
.auth_incorrect_path_title
;
1230 mServerStatusText
= R
.string
.auth_oauth_error
;
1232 case OAUTH2_ERROR_ACCESS_DENIED
:
1233 mServerStatusText
= R
.string
.auth_oauth_error_access_denied
;
1235 case UNHANDLED_HTTP_CODE
:
1237 mServerStatusText
= R
.string
.auth_unknown_error_title
;
1239 case OK_REDIRECT_TO_NON_SECURE_CONNECTION
:
1240 mServerStatusIcon
= R
.drawable
.ic_lock_open
;
1241 mServerStatusText
= R
.string
.auth_redirect_non_secure_connection_title
;
1244 mServerStatusText
= 0;
1245 mServerStatusIcon
= 0;
1251 * Chooses the right icon and text to show to the user for the received operation result.
1253 * @param result Result of a remote operation performed in this activity
1255 private void updateAuthStatusIconAndText(RemoteOperationResult result
) {
1256 mAuthStatusIcon
= R
.drawable
.common_error
; // the most common case in the switch below
1258 switch (result
.getCode()) {
1260 mAuthStatusIcon
= R
.drawable
.ic_lock
;
1261 mAuthStatusText
= R
.string
.auth_secure_connection
;
1266 if (mHostUrlInput
.getText().toString().trim().toLowerCase().startsWith("http://") ) {
1267 mAuthStatusText
= R
.string
.auth_connection_established
;
1268 mAuthStatusIcon
= R
.drawable
.ic_ok
;
1270 mAuthStatusText
= R
.string
.auth_nossl_plain_ok_title
;
1271 mAuthStatusIcon
= R
.drawable
.ic_lock_open
;
1275 case NO_NETWORK_CONNECTION
:
1276 mAuthStatusIcon
= R
.drawable
.no_network
;
1277 mAuthStatusText
= R
.string
.auth_no_net_conn_title
;
1280 case SSL_RECOVERABLE_PEER_UNVERIFIED
:
1281 mAuthStatusText
= R
.string
.auth_ssl_unverified_server_title
;
1283 case BAD_OC_VERSION
:
1284 mAuthStatusText
= R
.string
.auth_bad_oc_version_title
;
1286 case WRONG_CONNECTION
:
1287 mAuthStatusText
= R
.string
.auth_wrong_connection_title
;
1290 mAuthStatusText
= R
.string
.auth_timeout_title
;
1292 case INCORRECT_ADDRESS
:
1293 mAuthStatusText
= R
.string
.auth_incorrect_address_title
;
1296 mAuthStatusText
= R
.string
.auth_ssl_general_error_title
;
1299 mAuthStatusText
= R
.string
.auth_unauthorized
;
1301 case HOST_NOT_AVAILABLE
:
1302 mAuthStatusText
= R
.string
.auth_unknown_host_title
;
1304 case INSTANCE_NOT_CONFIGURED
:
1305 mAuthStatusText
= R
.string
.auth_not_configured_title
;
1307 case FILE_NOT_FOUND
:
1308 mAuthStatusText
= R
.string
.auth_incorrect_path_title
;
1311 mAuthStatusText
= R
.string
.auth_oauth_error
;
1313 case OAUTH2_ERROR_ACCESS_DENIED
:
1314 mAuthStatusText
= R
.string
.auth_oauth_error_access_denied
;
1316 case ACCOUNT_NOT_NEW
:
1317 mAuthStatusText
= R
.string
.auth_account_not_new
;
1319 case ACCOUNT_NOT_THE_SAME
:
1320 mAuthStatusText
= R
.string
.auth_account_not_the_same
;
1322 case UNHANDLED_HTTP_CODE
:
1324 mAuthStatusText
= R
.string
.auth_unknown_error_title
;
1327 mAuthStatusText
= 0;
1328 mAuthStatusIcon
= 0;
1333 private void updateStatusIconFailUserName(){
1334 mAuthStatusIcon
= R
.drawable
.common_error
;
1335 mAuthStatusText
= R
.string
.auth_fail_get_user_name
;
1338 private void updateServerStatusIconNoRegularAuth(){
1339 mServerStatusIcon
= R
.drawable
.common_error
;
1340 mServerStatusText
= R
.string
.auth_can_not_auth_against_server
;
1344 * Processes the result of the request for and access token send
1345 * to an OAuth authorization server.
1347 * @param result Result of the operation.
1349 private void onGetOAuthAccessTokenFinish(RemoteOperationResult result
) {
1350 mWaitingForOpId
= Long
.MAX_VALUE
;
1351 dismissDialog(WAIT_DIALOG_TAG
);
1353 if (result
.isSuccess()) {
1354 /// be gentle with the user
1355 IndeterminateProgressDialog dialog
=
1356 IndeterminateProgressDialog
.newInstance(R
.string
.auth_trying_to_login
, true
);
1357 dialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
1359 /// time to test the retrieved access token on the ownCloud server
1360 @SuppressWarnings("unchecked")
1361 Map
<String
, String
> tokens
= (Map
<String
, String
>)(result
.getData().get(0));
1362 mAuthToken
= tokens
.get(OAuth2Constants
.KEY_ACCESS_TOKEN
);
1363 Log_OC
.d(TAG
, "Got ACCESS TOKEN: " + mAuthToken
);
1365 /// validate token accessing to root folder / getting session
1366 OwnCloudCredentials credentials
= OwnCloudCredentialsFactory
.newBearerCredentials(
1368 accessRootFolder(credentials
);
1371 updateAuthStatusIconAndText(result
);
1373 Log_OC
.d(TAG
, "Access failed: " + result
.getLogMessage());
1379 * Processes the result of the access check performed to try the user credentials.
1381 * Creates a new account through the AccountManager.
1383 * @param result Result of the operation.
1386 public void onAuthenticatorTaskCallback(RemoteOperationResult result
) {
1387 mWaitingForOpId
= Long
.MAX_VALUE
;
1388 dismissDialog(WAIT_DIALOG_TAG
);
1391 if (result
.isSuccess()) {
1392 Log_OC
.d(TAG
, "Successful access - time to save the account");
1394 boolean success
= false
;
1396 if (mAction
== ACTION_CREATE
) {
1397 success
= createAccount(result
);
1401 updateAccountAuthentication();
1404 } catch (AccountNotFoundException e
) {
1405 Log_OC
.e(TAG
, "Account " + mAccount
+ " was removed!", e
);
1406 Toast
.makeText(this, R
.string
.auth_account_does_not_exist
,
1407 Toast
.LENGTH_SHORT
).show();
1416 } else if (result
.isServerFail() || result
.isException()) {
1417 /// server errors or exceptions in authorization take to requiring a new check of
1419 mServerIsChecked
= true
;
1420 mServerIsValid
= false
;
1421 mServerInfo
= new GetServerInfoOperation
.ServerInfo();
1423 // update status icon and text
1424 updateServerStatusIconAndText(result
);
1426 mAuthStatusIcon
= 0;
1427 mAuthStatusText
= 0;
1430 // update input controls state
1431 showRefreshButton(true
);
1432 mOkButton
.setEnabled(false
);
1434 // very special case (TODO: move to a common place for all the remote operations)
1435 if (result
.getCode() == ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
) {
1436 showUntrustedCertDialog(result
);
1439 } else { // authorization fail due to client side - probably wrong credentials
1440 updateAuthStatusIconAndText(result
);
1442 Log_OC
.d(TAG
, "Access failed: " + result
.getLogMessage());
1450 * Updates the authentication token.
1452 * Sets the proper response so that the AccountAuthenticator that started this activity
1453 * saves a new authorization token for mAccount.
1455 * Kills the session kept by OwnCloudClientManager so that a new one will created with
1456 * the new credentials when needed.
1458 private void updateAccountAuthentication() throws AccountNotFoundException
{
1460 Bundle response
= new Bundle();
1461 response
.putString(AccountManager
.KEY_ACCOUNT_NAME
, mAccount
.name
);
1462 response
.putString(AccountManager
.KEY_ACCOUNT_TYPE
, mAccount
.type
);
1464 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType()).
1465 equals(mAuthTokenType
)) {
1466 response
.putString(AccountManager
.KEY_AUTHTOKEN
, mAuthToken
);
1467 // the next line is necessary, notifications are calling directly to the
1468 // AuthenticatorActivity to update, without AccountManager intervention
1469 mAccountMgr
.setAuthToken(mAccount
, mAuthTokenType
, mAuthToken
);
1471 } else if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).
1472 equals(mAuthTokenType
)) {
1474 response
.putString(AccountManager
.KEY_AUTHTOKEN
, mAuthToken
);
1475 // the next line is necessary; by now, notifications are calling directly to the
1476 // AuthenticatorActivity to update, without AccountManager intervention
1477 mAccountMgr
.setAuthToken(mAccount
, mAuthTokenType
, mAuthToken
);
1480 response
.putString(AccountManager
.KEY_AUTHTOKEN
, mPasswordInput
.getText().toString());
1481 mAccountMgr
.setPassword(mAccount
, mPasswordInput
.getText().toString());
1483 setAccountAuthenticatorResult(response
);
1489 * Creates a new account through the Account Authenticator that started this activity.
1491 * This makes the account permanent.
1493 * TODO Decide how to name the OAuth accounts
1495 private boolean createAccount(RemoteOperationResult authResult
) {
1496 /// create and save new ownCloud account
1497 boolean isOAuth
= AccountTypeUtils
.
1498 getAuthTokenTypeAccessToken(MainApp
.getAccountType()).equals(mAuthTokenType
);
1499 boolean isSaml
= AccountTypeUtils
.
1500 getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).equals(mAuthTokenType
);
1502 String lastPermanentLocation
= authResult
.getLastPermanentLocation();
1503 if (lastPermanentLocation
!= null
) {
1504 mServerInfo
.mBaseUrl
= AccountUtils
.trimWebdavSuffix(lastPermanentLocation
);
1507 Uri uri
= Uri
.parse(mServerInfo
.mBaseUrl
);
1508 String username
= mUsernameInput
.getText().toString().trim();
1510 username
= "OAuth_user" + (new java
.util
.Random(System
.currentTimeMillis())).nextLong();
1512 String accountName
= com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.
1513 buildAccountName(uri
, username
);
1514 Account newAccount
= new Account(accountName
, MainApp
.getAccountType());
1515 if (AccountUtils
.exists(newAccount
, getApplicationContext())) {
1516 // fail - not a new account, but an existing one; disallow
1517 RemoteOperationResult result
= new RemoteOperationResult(ResultCode
.ACCOUNT_NOT_NEW
);
1518 updateAuthStatusIconAndText(result
);
1520 Log_OC
.d(TAG
, result
.getLogMessage());
1524 mAccount
= newAccount
;
1526 if (isOAuth
|| isSaml
) {
1527 // with external authorizations, the password is never input in the app
1528 mAccountMgr
.addAccountExplicitly(mAccount
, "", null
);
1530 mAccountMgr
.addAccountExplicitly(
1531 mAccount
, mPasswordInput
.getText().toString(), null
1535 // include account version with the new account
1536 mAccountMgr
.setUserData(
1538 Constants
.KEY_OC_ACCOUNT_VERSION
,
1539 Integer
.toString(AccountUtils
.ACCOUNT_VERSION
)
1542 /// add the new account as default in preferences, if there is none already
1543 Account defaultAccount
= AccountUtils
.getCurrentOwnCloudAccount(this);
1544 if (defaultAccount
== null
) {
1545 SharedPreferences
.Editor editor
= PreferenceManager
1546 .getDefaultSharedPreferences(this).edit();
1547 editor
.putString("select_oc_account", accountName
);
1551 /// prepare result to return to the Authenticator
1552 // TODO check again what the Authenticator makes with it; probably has the same
1553 // effect as addAccountExplicitly, but it's not well done
1554 final Intent intent
= new Intent();
1555 intent
.putExtra(AccountManager
.KEY_ACCOUNT_TYPE
, MainApp
.getAccountType());
1556 intent
.putExtra(AccountManager
.KEY_ACCOUNT_NAME
, mAccount
.name
);
1557 intent
.putExtra(AccountManager
.KEY_USERDATA
, username
);
1558 if (isOAuth
|| isSaml
) {
1559 mAccountMgr
.setAuthToken(mAccount
, mAuthTokenType
, mAuthToken
);
1561 /// add user data to the new account; TODO probably can be done in the last parameter
1562 // addAccountExplicitly, or in KEY_USERDATA
1563 mAccountMgr
.setUserData(
1564 mAccount
, Constants
.KEY_OC_VERSION
, mServerInfo
.mVersion
.getVersion()
1566 mAccountMgr
.setUserData(
1567 mAccount
, Constants
.KEY_OC_BASE_URL
, mServerInfo
.mBaseUrl
1571 mAccountMgr
.setUserData(mAccount
, Constants
.KEY_SUPPORTS_SAML_WEB_SSO
, "TRUE");
1572 } else if (isOAuth
) {
1573 mAccountMgr
.setUserData(mAccount
, Constants
.KEY_SUPPORTS_OAUTH2
, "TRUE");
1576 setAccountAuthenticatorResult(intent
.getExtras());
1577 setResult(RESULT_OK
, intent
);
1585 * Starts and activity to open the 'new account' page in the ownCloud web site
1587 * @param view 'Account register' button
1589 public void onRegisterClick(View view
) {
1590 Intent register
= new Intent(
1591 Intent
.ACTION_VIEW
, Uri
.parse(getString(R
.string
.welcome_link_url
))
1593 setResult(RESULT_CANCELED
);
1594 startActivity(register
);
1599 * Updates the content and visibility state of the icon and text associated
1600 * to the last check on the ownCloud server.
1603 private void showServerStatus() {
1604 if (mServerStatusIcon
== 0 && mServerStatusText
== 0) {
1605 mServerStatusView
.setVisibility(View
.INVISIBLE
);
1608 mServerStatusView
.setText(mServerStatusText
);
1609 mServerStatusView
.setCompoundDrawablesWithIntrinsicBounds(mServerStatusIcon
, 0, 0, 0);
1610 mServerStatusView
.setVisibility(View
.VISIBLE
);
1617 * Updates the content and visibility state of the icon and text associated
1618 * to the interactions with the OAuth authorization server.
1620 private void showAuthStatus() {
1621 if (mAuthStatusIcon
== 0 && mAuthStatusText
== 0) {
1622 mAuthStatusView
.setVisibility(View
.INVISIBLE
);
1625 mAuthStatusView
.setText(mAuthStatusText
);
1626 mAuthStatusView
.setCompoundDrawablesWithIntrinsicBounds(mAuthStatusIcon
, 0, 0, 0);
1627 mAuthStatusView
.setVisibility(View
.VISIBLE
);
1632 private void showRefreshButton (boolean show
) {
1634 mRefreshButton
.setVisibility(View
.VISIBLE
);
1636 mRefreshButton
.setVisibility(View
.GONE
);
1641 * Called when the refresh button in the input field for ownCloud host is clicked.
1643 * Performs a new check on the URL in the input field.
1645 * @param view Refresh 'button'
1647 public void onRefreshClick(View view
) {
1653 * Called when the eye icon in the password field is clicked.
1655 * Toggles the visibility of the password in the field.
1657 public void onViewPasswordClick() {
1658 int selectionStart
= mPasswordInput
.getSelectionStart();
1659 int selectionEnd
= mPasswordInput
.getSelectionEnd();
1660 if (isPasswordVisible()) {
1665 mPasswordInput
.setSelection(selectionStart
, selectionEnd
);
1670 * Called when the checkbox for OAuth authorization is clicked.
1672 * Hides or shows the input fields for user & password.
1674 * @param view 'View password' 'button'
1676 public void onCheckClick(View view
) {
1677 CheckBox oAuth2Check
= (CheckBox
)view
;
1678 if (oAuth2Check
.isChecked()) {
1679 mAuthTokenType
= OAUTH_TOKEN_TYPE
;
1681 mAuthTokenType
= BASIC_TOKEN_TYPE
;
1683 updateAuthenticationPreFragmentVisibility();
1688 * Called when the 'action' button in an IME is pressed ('enter' in software keyboard).
1690 * Used to trigger the authentication check when the user presses 'enter' after writing the
1691 * password, or to throw the server test when the only field on screen is the URL input field.
1694 public boolean onEditorAction(TextView inputField
, int actionId
, KeyEvent event
) {
1695 if (actionId
== EditorInfo
.IME_ACTION_DONE
&& inputField
!= null
&&
1696 inputField
.equals(mPasswordInput
)) {
1697 if (mOkButton
.isEnabled()) {
1698 mOkButton
.performClick();
1701 } else if (actionId
== EditorInfo
.IME_ACTION_NEXT
&& inputField
!= null
&&
1702 inputField
.equals(mHostUrlInput
)) {
1703 if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).
1704 equals(mAuthTokenType
)) {
1708 return false
; // always return false to grant that the software keyboard is hidden anyway
1712 private abstract static class RightDrawableOnTouchListener
implements OnTouchListener
{
1714 private int fuzz
= 75;
1720 public boolean onTouch(View view
, MotionEvent event
) {
1721 Drawable rightDrawable
= null
;
1722 if (view
instanceof TextView
) {
1723 Drawable
[] drawables
= ((TextView
)view
).getCompoundDrawables();
1724 if (drawables
.length
> 2) {
1725 rightDrawable
= drawables
[2];
1728 if (rightDrawable
!= null
) {
1729 final int x
= (int) event
.getX();
1730 final int y
= (int) event
.getY();
1731 final Rect bounds
= rightDrawable
.getBounds();
1732 if ( x
>= (view
.getRight() - bounds
.width() - fuzz
) &&
1733 x
<= (view
.getRight() - view
.getPaddingRight() + fuzz
) &&
1734 y
>= (view
.getPaddingTop() - fuzz
) &&
1735 y
<= (view
.getHeight() - view
.getPaddingBottom()) + fuzz
) {
1737 return onDrawableTouch(event
);
1743 public abstract boolean onDrawableTouch(final MotionEvent event
);
1747 private void getRemoteUserNameOperation(String sessionCookie
) {
1749 Intent getUserNameIntent
= new Intent();
1750 getUserNameIntent
.setAction(OperationsService
.ACTION_GET_USER_NAME
);
1751 getUserNameIntent
.putExtra(OperationsService
.EXTRA_SERVER_URL
, mServerInfo
.mBaseUrl
);
1752 getUserNameIntent
.putExtra(OperationsService
.EXTRA_COOKIE
, sessionCookie
);
1754 if (mOperationsServiceBinder
!= null
) {
1755 mWaitingForOpId
= mOperationsServiceBinder
.queueNewOperation(getUserNameIntent
);
1761 public void onSsoFinished(String sessionCookie
) {
1762 if (sessionCookie
!= null
&& sessionCookie
.length() > 0) {
1763 Log_OC
.d(TAG
, "Successful SSO - time to save the account");
1764 mAuthToken
= sessionCookie
;
1765 getRemoteUserNameOperation(sessionCookie
);
1766 Fragment fd
= getSupportFragmentManager().findFragmentByTag(SAML_DIALOG_TAG
);
1767 if (fd
!= null
&& fd
instanceof DialogFragment
) {
1768 Dialog d
= ((DialogFragment
)fd
).getDialog();
1769 if (d
!= null
&& d
.isShowing()) {
1776 Log_OC
.d(TAG
, "SSO failed");
1782 public boolean onTouchEvent(MotionEvent event
) {
1783 if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).
1784 equals(mAuthTokenType
) &&
1785 mHostUrlInput
.hasFocus() && event
.getAction() == MotionEvent
.ACTION_DOWN
) {
1788 return super.onTouchEvent(event
);
1793 * Show untrusted cert dialog
1795 public void showUntrustedCertDialog(
1796 X509Certificate x509Certificate
, SslError error
, SslErrorHandler handler
1798 // Show a dialog with the certificate info
1799 SslUntrustedCertDialog dialog
;
1800 if (x509Certificate
== null
) {
1801 dialog
= SslUntrustedCertDialog
.newInstanceForEmptySslError(error
, handler
);
1803 dialog
= SslUntrustedCertDialog
.
1804 newInstanceForFullSslError(x509Certificate
, error
, handler
);
1806 FragmentManager fm
= getSupportFragmentManager();
1807 FragmentTransaction ft
= fm
.beginTransaction();
1808 ft
.addToBackStack(null
);
1809 dialog
.show(ft
, UNTRUSTED_CERT_DIALOG_TAG
);
1814 * Show untrusted cert dialog
1816 private void showUntrustedCertDialog(RemoteOperationResult result
) {
1817 // Show a dialog with the certificate info
1818 SslUntrustedCertDialog dialog
= SslUntrustedCertDialog
.
1819 newInstanceForFullSslError((CertificateCombinedException
)result
.getException());
1820 FragmentManager fm
= getSupportFragmentManager();
1821 FragmentTransaction ft
= fm
.beginTransaction();
1822 ft
.addToBackStack(null
);
1823 dialog
.show(ft
, UNTRUSTED_CERT_DIALOG_TAG
);
1828 * Called from SslValidatorDialog when a new server certificate was correctly saved.
1830 public void onSavedCertificate() {
1831 Fragment fd
= getSupportFragmentManager().findFragmentByTag(SAML_DIALOG_TAG
);
1833 // if SAML dialog is not shown,
1834 // the SslDialog was shown due to an SSL error in the server check
1840 * Called from SslValidatorDialog when a new server certificate could not be saved
1841 * when the user requested it.
1844 public void onFailedSavingCertificate() {
1845 dismissDialog(SAML_DIALOG_TAG
);
1846 Toast
.makeText(this, R
.string
.ssl_validator_not_saved
, Toast
.LENGTH_LONG
).show();
1850 public void onCancelCertificate() {
1851 dismissDialog(SAML_DIALOG_TAG
);
1855 private void doOnResumeAndBound() {
1856 //Log_OC.wtf(TAG, "registering to listen for operation callbacks" );
1857 mOperationsServiceBinder
.addOperationListener(AuthenticatorActivity
.this, mHandler
);
1858 if (mWaitingForOpId
<= Integer
.MAX_VALUE
) {
1859 mOperationsServiceBinder
.dispatchResultIfFinished((int)mWaitingForOpId
, this);
1862 if (mPendingAutoCheck
) {
1868 private void dismissDialog(String dialogTag
){
1869 Fragment frag
= getSupportFragmentManager().findFragmentByTag(dialogTag
);
1870 if (frag
!= null
&& frag
instanceof DialogFragment
) {
1871 DialogFragment dialog
= (DialogFragment
) frag
;
1878 * Implements callback methods for service binding.
1880 private class OperationsServiceConnection
implements ServiceConnection
{
1883 public void onServiceConnected(ComponentName component
, IBinder service
) {
1884 if (component
.equals(
1885 new ComponentName(AuthenticatorActivity
.this, OperationsService
.class)
1887 mOperationsServiceBinder
= (OperationsServiceBinder
) service
;
1889 doOnResumeAndBound();
1896 public void onServiceDisconnected(ComponentName component
) {
1897 if (component
.equals(
1898 new ComponentName(AuthenticatorActivity
.this, OperationsService
.class)
1900 Log_OC
.e(TAG
, "Operations service crashed");
1901 mOperationsServiceBinder
= null
;
1908 * Create and show dialog for request authentication to the user
1909 * @param webView Web view to emebd into the authentication dialog.
1910 * @param handler Object responsible for catching and recovering HTTP authentication fails.
1912 public void createAuthenticationDialog(WebView webView
, HttpAuthHandler handler
) {
1914 // Show a dialog with the certificate info
1915 CredentialsDialogFragment dialog
=
1916 CredentialsDialogFragment
.newInstanceForCredentials(webView
, handler
);
1917 FragmentManager fm
= getSupportFragmentManager();
1918 FragmentTransaction ft
= fm
.beginTransaction();
1919 ft
.addToBackStack(null
);
1920 dialog
.setCancelable(false
);
1921 dialog
.show(ft
, CREDENTIALS_DIALOG_TAG
);
1923 if (!mIsFirstAuthAttempt
) {
1925 getApplicationContext(),
1926 getText(R
.string
.saml_authentication_wrong_pass
),
1930 mIsFirstAuthAttempt
= false
;
1935 * For retrieving the clicking on authentication cancel button
1937 public void doNegativeAuthenticatioDialogClick(){
1938 mIsFirstAuthAttempt
= true
;