129da6c9d822c79043621c9a5bfea68969a78eca
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.authentication;
20
21 import java.security.cert.X509Certificate;
22
23 import android.accounts.Account;
24 import android.accounts.AccountManager;
25 import android.app.AlertDialog;
26 import android.app.Dialog;
27 import android.app.ProgressDialog;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.DialogInterface;
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.Fragment;
43 import android.support.v4.app.FragmentManager;
44 import android.support.v4.app.FragmentTransaction;
45 import android.text.Editable;
46 import android.text.InputType;
47 import android.text.TextWatcher;
48 import android.util.Log;
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.Window;
55 import android.view.inputmethod.EditorInfo;
56 import android.webkit.SslErrorHandler;
57 import android.widget.Button;
58 import android.widget.CheckBox;
59 import android.widget.EditText;
60 import android.widget.TextView;
61 import android.widget.TextView.OnEditorActionListener;
62 import android.widget.Toast;
63
64 import com.actionbarsherlock.app.SherlockDialogFragment;
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.accounts.AccountTypeUtils;
69 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
70 import com.owncloud.android.lib.common.OwnCloudClientFactory;
71 import com.owncloud.android.lib.common.OwnCloudClient;
72 import com.owncloud.android.operations.DetectAuthenticationMethodOperation;
73 import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod;
74 import com.owncloud.android.operations.OAuth2GetAccessToken;
75
76 import com.owncloud.android.lib.common.network.CertificateCombinedException;
77 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
78 import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation;
79 import com.owncloud.android.lib.common.operations.RemoteOperation;
80 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
81 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
82 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
83 import com.owncloud.android.lib.resources.users.GetRemoteUserNameOperation;
84
85 import com.owncloud.android.services.OperationsService;
86 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
87 import com.owncloud.android.ui.dialog.SamlWebViewDialog;
88 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
89 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;
90 import com.owncloud.android.utils.Log_OC;
91 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
92
93 /**
94 * This Activity is used to add an ownCloud account to the App
95 *
96 * @author Bartek Przybylski
97 * @author David A. Velasco
98 */
99 public class AuthenticatorActivity extends AccountAuthenticatorActivity
100 implements OnRemoteOperationListener, OnFocusChangeListener, OnEditorActionListener,
101 SsoWebViewClientListener, OnSslUntrustedCertListener {
102
103 private static final String TAG = AuthenticatorActivity.class.getSimpleName();
104
105 public static final String EXTRA_ACCOUNT = "ACCOUNT";
106 public static final String EXTRA_USER_NAME = "USER_NAME";
107 public static final String EXTRA_HOST_NAME = "HOST_NAME";
108 public static final String EXTRA_ACTION = "ACTION";
109 public static final String EXTRA_ENFORCED_UPDATE = "ENFORCE_UPDATE";
110
111 private static final String KEY_AUTH_MESSAGE_VISIBILITY = "AUTH_MESSAGE_VISIBILITY";
112 private static final String KEY_AUTH_MESSAGE_TEXT = "AUTH_MESSAGE_TEXT";
113 private static final String KEY_HOST_URL_TEXT = "HOST_URL_TEXT";
114 private static final String KEY_OC_VERSION = "OC_VERSION";
115 private static final String KEY_OC_VERSION_STRING = "OC_VERSION_STRING";
116 private static final String KEY_ACCOUNT = "ACCOUNT";
117 private static final String KEY_SERVER_VALID = "SERVER_VALID";
118 private static final String KEY_SERVER_CHECKED = "SERVER_CHECKED";
119 private static final String KEY_SERVER_CHECK_IN_PROGRESS = "SERVER_CHECK_IN_PROGRESS";
120 private static final String KEY_SERVER_STATUS_TEXT = "SERVER_STATUS_TEXT";
121 private static final String KEY_SERVER_STATUS_ICON = "SERVER_STATUS_ICON";
122 private static final String KEY_IS_SSL_CONN = "IS_SSL_CONN";
123 private static final String KEY_PASSWORD_VISIBLE = "PASSWORD_VISIBLE";
124 private static final String KEY_AUTH_STATUS_TEXT = "AUTH_STATUS_TEXT";
125 private static final String KEY_AUTH_STATUS_ICON = "AUTH_STATUS_ICON";
126 private static final String KEY_REFRESH_BUTTON_ENABLED = "KEY_REFRESH_BUTTON_ENABLED";
127 //private static final String KEY_IS_SHARED_SUPPORTED = "KEY_IS_SHARE_SUPPORTED";
128 private static final String KEY_SERVER_AUTH_METHOD = "KEY_SERVER_AUTH_METHOD";
129 private static final String KEY_DETECT_AUTH_OP_ID = "KEY_DETECT_AUTH_OP_ID";
130
131
132 private static final String AUTH_ON = "on";
133 private static final String AUTH_OFF = "off";
134 private static final String AUTH_OPTIONAL = "optional";
135
136 private static final int DIALOG_LOGIN_PROGRESS = 0;
137 private static final int DIALOG_CERT_NOT_SAVED = 1;
138 private static final int DIALOG_OAUTH2_LOGIN_PROGRESS = 2;
139
140 public static final byte ACTION_CREATE = 0;
141 public static final byte ACTION_UPDATE_TOKEN = 1;
142
143 private static final String TAG_SAML_DIALOG = "samlWebViewDialog";
144
145 private String mHostBaseUrl;
146 private OwnCloudVersion mDiscoveredVersion;
147
148 private String mAuthMessageText;
149 private int mAuthMessageVisibility, mServerStatusText, mServerStatusIcon;
150 private boolean mServerIsChecked, mServerIsValid, mIsSslConn;
151 private AuthenticationMethod mServerAuthMethod = AuthenticationMethod.UNKNOWN;
152 private int mDetectAuthOpId = -1;
153
154 private int mAuthStatusText, mAuthStatusIcon;
155 private TextView mAuthStatusLayout;
156
157 private final Handler mHandler = new Handler();
158 private Thread mOperationThread;
159 private GetRemoteStatusOperation mOcServerChkOperation;
160 private ExistenceCheckRemoteOperation mAuthCheckOperation;
161
162 private Uri mNewCapturedUriFromOAuth2Redirection;
163
164 private AccountManager mAccountMgr;
165 private boolean mJustCreated;
166 private byte mAction;
167 private Account mAccount;
168
169 private TextView mAuthMessage;
170
171 private EditText mHostUrlInput;
172 private boolean mHostUrlInputEnabled;
173 private View mRefreshButton;
174
175 private String mAuthTokenType;
176
177 private EditText mUsernameInput;
178 private EditText mPasswordInput;
179
180 private CheckBox mOAuth2Check;
181
182 private TextView mOAuthAuthEndpointText;
183 private TextView mOAuthTokenEndpointText;
184
185 private SamlWebViewDialog mSamlDialog;
186
187 private View mOkButton;
188
189 private String mAuthToken;
190
191 private boolean mResumed; // Control if activity is resumed
192
193 public static String DIALOG_UNTRUSTED_CERT = "DIALOG_UNTRUSTED_CERT";
194
195 private ServiceConnection mOperationsServiceConnection = null;
196
197 private OperationsServiceBinder mOperationsServiceBinder = null;
198
199 /**
200 * {@inheritDoc}
201 *
202 * IMPORTANT ENTRY POINT 1: activity is shown to the user
203 */
204 @Override
205 protected void onCreate(Bundle savedInstanceState) {
206 super.onCreate(savedInstanceState);
207 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
208
209 // bind to Operations Service
210 mOperationsServiceConnection = new OperationsServiceConnection();
211 if (!bindService(new Intent(this, OperationsService.class),
212 mOperationsServiceConnection,
213 Context.BIND_AUTO_CREATE)) {
214 Toast.makeText(this,
215 R.string.error_cant_bind_to_operations_service,
216 Toast.LENGTH_LONG)
217 .show();
218 finish();
219 }
220
221 /// set view and get references to view elements
222 setContentView(R.layout.account_setup);
223 mAuthMessage = (TextView) findViewById(R.id.auth_message);
224 mHostUrlInput = (EditText) findViewById(R.id.hostUrlInput);
225 mHostUrlInput.setText(getString(R.string.server_url)); // valid although R.string.server_url is an empty string
226 mUsernameInput = (EditText) findViewById(R.id.account_username);
227 mPasswordInput = (EditText) findViewById(R.id.account_password);
228 mOAuthAuthEndpointText = (TextView)findViewById(R.id.oAuthEntryPoint_1);
229 mOAuthTokenEndpointText = (TextView)findViewById(R.id.oAuthEntryPoint_2);
230 mOAuth2Check = (CheckBox) findViewById(R.id.oauth_onOff_check);
231 mOkButton = findViewById(R.id.buttonOK);
232 mAuthStatusLayout = (TextView) findViewById(R.id.auth_status_text);
233
234 /// set Host Url Input Enabled
235 mHostUrlInputEnabled = getResources().getBoolean(R.bool.show_server_url_input);
236
237 /// set visibility of link for new users
238 boolean accountRegisterVisibility = getResources().getBoolean(R.bool.show_welcome_link);
239 Button welcomeLink = (Button) findViewById(R.id.welcome_link);
240 if (welcomeLink != null) {
241 if (accountRegisterVisibility) {
242 welcomeLink.setVisibility(View.VISIBLE);
243 welcomeLink.setText(String.format(getString(R.string.auth_register), getString(R.string.app_name)));
244 } else {
245 findViewById(R.id.welcome_link).setVisibility(View.GONE);
246 }
247 }
248
249 /// initialization
250 mAccountMgr = AccountManager.get(this);
251 mNewCapturedUriFromOAuth2Redirection = null;
252 mAction = getIntent().getByteExtra(EXTRA_ACTION, ACTION_CREATE);
253 mAccount = null;
254 mHostBaseUrl = "";
255 boolean refreshButtonEnabled = false;
256
257 // URL input configuration applied
258 if (!mHostUrlInputEnabled)
259 {
260 findViewById(R.id.hostUrlFrame).setVisibility(View.GONE);
261 mRefreshButton = findViewById(R.id.centeredRefreshButton);
262
263 } else {
264 mRefreshButton = findViewById(R.id.embeddedRefreshButton);
265 }
266
267 if (savedInstanceState == null) {
268 mResumed = false;
269 /// connection state and info
270 mAuthMessageVisibility = View.GONE;
271 mServerStatusText = mServerStatusIcon = 0;
272 mServerIsValid = false;
273 mServerIsChecked = false;
274 mIsSslConn = false;
275 mAuthStatusText = mAuthStatusIcon = 0;
276
277 /// retrieve extras from intent
278 mAccount = getIntent().getExtras().getParcelable(EXTRA_ACCOUNT);
279 if (mAccount != null) {
280 String ocVersion = mAccountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION);
281 String ocVersionString = mAccountMgr.getUserData(mAccount, Constants.KEY_OC_VERSION_STRING);
282 if (ocVersion != null) {
283 mDiscoveredVersion = new OwnCloudVersion(ocVersion, ocVersionString);
284 }
285 mHostBaseUrl = normalizeUrl(mAccountMgr.getUserData(mAccount, Constants.KEY_OC_BASE_URL));
286 mHostUrlInput.setText(mHostBaseUrl);
287 String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));
288 mUsernameInput.setText(userName);
289
290 }
291 initAuthorizationMethod(); // checks intent and setup.xml to determine mCurrentAuthorizationMethod
292 mJustCreated = true;
293
294 if (mAction == ACTION_UPDATE_TOKEN || !mHostUrlInputEnabled) {
295 checkOcServer();
296 }
297
298 } else {
299 mResumed = true;
300 /// connection state and info
301 mAuthMessageVisibility = savedInstanceState.getInt(KEY_AUTH_MESSAGE_VISIBILITY);
302 mAuthMessageText = savedInstanceState.getString(KEY_AUTH_MESSAGE_TEXT);
303 mServerIsValid = savedInstanceState.getBoolean(KEY_SERVER_VALID);
304 mServerIsChecked = savedInstanceState.getBoolean(KEY_SERVER_CHECKED);
305 mServerStatusText = savedInstanceState.getInt(KEY_SERVER_STATUS_TEXT);
306 mServerStatusIcon = savedInstanceState.getInt(KEY_SERVER_STATUS_ICON);
307 mIsSslConn = savedInstanceState.getBoolean(KEY_IS_SSL_CONN);
308 mAuthStatusText = savedInstanceState.getInt(KEY_AUTH_STATUS_TEXT);
309 mAuthStatusIcon = savedInstanceState.getInt(KEY_AUTH_STATUS_ICON);
310 if (savedInstanceState.getBoolean(KEY_PASSWORD_VISIBLE, false)) {
311 showPassword();
312 }
313
314 /// server data
315 String ocVersion = savedInstanceState.getString(KEY_OC_VERSION);
316 String ocVersionString = savedInstanceState.getString(KEY_OC_VERSION_STRING);
317 if (ocVersion != null) {
318 mDiscoveredVersion = new OwnCloudVersion(ocVersion, ocVersionString);
319 }
320 mHostBaseUrl = savedInstanceState.getString(KEY_HOST_URL_TEXT);
321
322 // account data, if updating
323 mAccount = savedInstanceState.getParcelable(KEY_ACCOUNT);
324 mAuthTokenType = savedInstanceState.getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE);
325 if (mAuthTokenType == null) {
326 mAuthTokenType = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType());
327
328 }
329
330 // check if server check was interrupted by a configuration change
331 if (savedInstanceState.getBoolean(KEY_SERVER_CHECK_IN_PROGRESS, false)) {
332 checkOcServer();
333 }
334
335 // refresh button enabled
336 refreshButtonEnabled = savedInstanceState.getBoolean(KEY_REFRESH_BUTTON_ENABLED);
337
338
339 mServerAuthMethod = AuthenticationMethod.valueOf(
340 savedInstanceState.getString(KEY_SERVER_AUTH_METHOD));
341 mDetectAuthOpId = savedInstanceState.getInt(KEY_DETECT_AUTH_OP_ID);
342
343 }
344
345 if (mAuthMessageVisibility== View.VISIBLE) {
346 showAuthMessage(mAuthMessageText);
347 }
348 else {
349 hideAuthMessage();
350 }
351 adaptViewAccordingToAuthenticationMethod();
352 showServerStatus();
353 showAuthStatus();
354
355 if (mAction == ACTION_UPDATE_TOKEN) {
356 /// lock things that should not change
357 mHostUrlInput.setEnabled(false);
358 mHostUrlInput.setFocusable(false);
359 mUsernameInput.setEnabled(false);
360 mUsernameInput.setFocusable(false);
361 mOAuth2Check.setVisibility(View.GONE);
362 }
363
364 //if (mServerIsChecked && !mServerIsValid && mRefreshButtonEnabled) showRefreshButton();
365 if (mServerIsChecked && !mServerIsValid && refreshButtonEnabled) showRefreshButton();
366 mOkButton.setEnabled(mServerIsValid); // state not automatically recovered in configuration changes
367
368 if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType) ||
369 !AUTH_OPTIONAL.equals(getString(R.string.auth_method_oauth2))) {
370 mOAuth2Check.setVisibility(View.GONE);
371 }
372
373 mPasswordInput.setText(""); // clean password to avoid social hacking (disadvantage: password in removed if the device is turned aside)
374
375 /// bind view elements to listeners and other friends
376 mHostUrlInput.setOnFocusChangeListener(this);
377 mHostUrlInput.setImeOptions(EditorInfo.IME_ACTION_NEXT);
378 mHostUrlInput.setOnEditorActionListener(this);
379 mHostUrlInput.addTextChangedListener(new TextWatcher() {
380
381 @Override
382 public void afterTextChanged(Editable s) {
383 if (!mHostBaseUrl.equals(normalizeUrl(mHostUrlInput.getText().toString()))) {
384 mOkButton.setEnabled(false);
385 }
386 }
387
388 @Override
389 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
390 }
391
392 @Override
393 public void onTextChanged(CharSequence s, int start, int before, int count) {
394 if (!mResumed) {
395 mAuthStatusIcon = 0;
396 mAuthStatusText = 0;
397 showAuthStatus();
398 }
399 mResumed = false;
400 }
401 });
402
403 mPasswordInput.setOnFocusChangeListener(this);
404 mPasswordInput.setImeOptions(EditorInfo.IME_ACTION_DONE);
405 mPasswordInput.setOnEditorActionListener(this);
406 mPasswordInput.setOnTouchListener(new RightDrawableOnTouchListener() {
407 @Override
408 public boolean onDrawableTouch(final MotionEvent event) {
409 if (event.getAction() == MotionEvent.ACTION_UP) {
410 AuthenticatorActivity.this.onViewPasswordClick();
411 }
412 return true;
413 }
414 });
415
416 findViewById(R.id.scroll).setOnTouchListener(new OnTouchListener() {
417 @Override
418 public boolean onTouch(View view, MotionEvent event) {
419 if (event.getAction() == MotionEvent.ACTION_DOWN) {
420 if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType) &&
421 mHostUrlInput.hasFocus()) {
422 checkOcServer();
423 }
424 }
425 return false;
426 }
427 });
428
429 }
430
431
432
433 private void initAuthorizationMethod() {
434 boolean oAuthRequired = false;
435 boolean samlWebSsoRequired = false;
436
437 mAuthTokenType = getIntent().getExtras().getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE);
438 mAccount = getIntent().getExtras().getParcelable(EXTRA_ACCOUNT);
439
440 // TODO could be a good moment to validate the received token type, if not null
441
442 if (mAuthTokenType == null) {
443 if (mAccount != null) {
444 /// same authentication method than the one used to create the account to update
445 oAuthRequired = (mAccountMgr.getUserData(mAccount, Constants.KEY_SUPPORTS_OAUTH2) != null);
446 samlWebSsoRequired = (mAccountMgr.getUserData(mAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null);
447
448 } else {
449 /// use the one set in setup.xml
450 oAuthRequired = AUTH_ON.equals(getString(R.string.auth_method_oauth2));
451 samlWebSsoRequired = AUTH_ON.equals(getString(R.string.auth_method_saml_web_sso));
452 }
453 if (oAuthRequired) {
454 mAuthTokenType = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType());
455 } else if (samlWebSsoRequired) {
456 mAuthTokenType = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType());
457 } else {
458 mAuthTokenType = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType());
459 }
460 }
461
462 if (mAccount != null) {
463 String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));
464 mUsernameInput.setText(userName);
465 }
466
467 mOAuth2Check.setChecked(AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType));
468
469 }
470
471 /**
472 * Saves relevant state before {@link #onPause()}
473 *
474 * Do NOT save {@link #mNewCapturedUriFromOAuth2Redirection}; it keeps a temporal flag, intended to defer the
475 * processing of the redirection caught in {@link #onNewIntent(Intent)} until {@link #onResume()}
476 *
477 * See {@link #loadSavedInstanceState(Bundle)}
478 */
479 @Override
480 protected void onSaveInstanceState(Bundle outState) {
481 //Log.wtf(TAG, "onSaveInstanceState init" );
482 super.onSaveInstanceState(outState);
483
484 /// connection state and info
485 outState.putInt(KEY_AUTH_MESSAGE_VISIBILITY, mAuthMessage.getVisibility());
486 outState.putString(KEY_AUTH_MESSAGE_TEXT, mAuthMessage.getText().toString());
487 outState.putInt(KEY_SERVER_STATUS_TEXT, mServerStatusText);
488 outState.putInt(KEY_SERVER_STATUS_ICON, mServerStatusIcon);
489 outState.putBoolean(KEY_SERVER_VALID, mServerIsValid);
490 outState.putBoolean(KEY_SERVER_CHECKED, mServerIsChecked);
491 outState.putBoolean(KEY_SERVER_CHECK_IN_PROGRESS, (!mServerIsValid && mOcServerChkOperation != null));
492 outState.putBoolean(KEY_IS_SSL_CONN, mIsSslConn);
493 outState.putBoolean(KEY_PASSWORD_VISIBLE, isPasswordVisible());
494 outState.putInt(KEY_AUTH_STATUS_ICON, mAuthStatusIcon);
495 outState.putInt(KEY_AUTH_STATUS_TEXT, mAuthStatusText);
496
497 /// server data
498 if (mDiscoveredVersion != null) {
499 outState.putString(KEY_OC_VERSION, mDiscoveredVersion.getVersion());
500 outState.putString(KEY_OC_VERSION_STRING, mDiscoveredVersion.getVersionString());
501 }
502 outState.putString(KEY_HOST_URL_TEXT, mHostBaseUrl);
503
504 /// account data, if updating
505 if (mAccount != null) {
506 outState.putParcelable(KEY_ACCOUNT, mAccount);
507 }
508 outState.putString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE, mAuthTokenType);
509
510 // refresh button enabled
511 outState.putBoolean(KEY_REFRESH_BUTTON_ENABLED, (mRefreshButton.getVisibility() == View.VISIBLE));
512
513 outState.putString(KEY_SERVER_AUTH_METHOD, mServerAuthMethod.name());
514 outState.putInt(KEY_DETECT_AUTH_OP_ID, mDetectAuthOpId);
515 //Log.wtf(TAG, "onSaveInstanceState end" );
516 }
517
518
519 /**
520 * The redirection triggered by the OAuth authentication server as response to the GET AUTHORIZATION request
521 * is caught here.
522 *
523 * To make this possible, this activity needs to be qualified with android:launchMode = "singleTask" in the
524 * AndroidManifest.xml file.
525 */
526 @Override
527 protected void onNewIntent (Intent intent) {
528 Log_OC.d(TAG, "onNewIntent()");
529 Uri data = intent.getData();
530 if (data != null && data.toString().startsWith(getString(R.string.oauth2_redirect_uri))) {
531 mNewCapturedUriFromOAuth2Redirection = data;
532 }
533 }
534
535
536 /**
537 * The redirection triggered by the OAuth authentication server as response to the GET AUTHORIZATION, and
538 * deferred in {@link #onNewIntent(Intent)}, is processed here.
539 */
540 @Override
541 protected void onResume() {
542 //Log.wtf(TAG, "onResume init" );
543 super.onResume();
544
545 if (mAction == ACTION_UPDATE_TOKEN && mJustCreated && getIntent().getBooleanExtra(EXTRA_ENFORCED_UPDATE, false)) {
546 if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType)) {
547 //Toast.makeText(this, R.string.auth_expired_oauth_token_toast, Toast.LENGTH_LONG).show();
548 showAuthMessage(getString(R.string.auth_expired_oauth_token_toast));
549 } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {
550 //Toast.makeText(this, R.string.auth_expired_saml_sso_token_toast, Toast.LENGTH_LONG).show();
551 showAuthMessage(getString(R.string.auth_expired_saml_sso_token_toast));
552 } else {
553 //Toast.makeText(this, R.string.auth_expired_basic_auth_toast, Toast.LENGTH_LONG).show();
554 showAuthMessage(getString(R.string.auth_expired_basic_auth_toast));
555 }
556 }
557
558 if (mNewCapturedUriFromOAuth2Redirection != null) {
559 getOAuth2AccessTokenFromCapturedRedirection();
560 }
561
562 mJustCreated = false;
563
564 if (mOperationsServiceBinder != null) {
565 doOnResumeAndBound();
566 }
567
568 //Log.wtf(TAG, "onResume end" );
569 }
570
571
572 @Override
573 protected void onPause() {
574 //Log.wtf(TAG, "onPause init" );
575 if (mOperationsServiceBinder != null) {
576 //Log.wtf(TAG, "unregistering to listen for operation callbacks" );
577 mOperationsServiceBinder.removeOperationListener(this);
578 }
579 super.onPause();
580 //Log.wtf(TAG, "onPause end" );
581 }
582
583 @Override
584 protected void onDestroy() {
585 if (mOperationsServiceConnection != null) {
586 unbindService(mOperationsServiceConnection);
587 mOperationsServiceBinder = null;
588 }
589 super.onDestroy();
590 }
591
592
593 /**
594 * Parses the redirection with the response to the GET AUTHORIZATION request to the
595 * oAuth server and requests for the access token (GET ACCESS TOKEN)
596 */
597 private void getOAuth2AccessTokenFromCapturedRedirection() {
598 /// Parse data from OAuth redirection
599 String queryParameters = mNewCapturedUriFromOAuth2Redirection.getQuery();
600 mNewCapturedUriFromOAuth2Redirection = null;
601
602 /// Showing the dialog with instructions for the user.
603 showDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);
604
605 /// GET ACCESS TOKEN to the oAuth server
606 RemoteOperation operation = new OAuth2GetAccessToken( getString(R.string.oauth2_client_id),
607 getString(R.string.oauth2_redirect_uri),
608 getString(R.string.oauth2_grant_type),
609 queryParameters);
610 //OwnCloudClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(getString(R.string.oauth2_url_endpoint_access)), getApplicationContext());
611 OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mOAuthTokenEndpointText.getText().toString().trim()), getApplicationContext(), true);
612 operation.execute(client, this, mHandler);
613 }
614
615
616
617 /**
618 * Handles the change of focus on the text inputs for the server URL and the password
619 */
620 public void onFocusChange(View view, boolean hasFocus) {
621 if (view.getId() == R.id.hostUrlInput) {
622 if (!hasFocus) {
623 onUrlInputFocusLost((TextView) view);
624 }
625 else {
626 hideRefreshButton();
627 }
628
629 } else if (view.getId() == R.id.account_password) {
630 onPasswordFocusChanged((TextView) view, hasFocus);
631 }
632 }
633
634
635 /**
636 * Handles changes in focus on the text input for the server URL.
637 *
638 * IMPORTANT ENTRY POINT 2: When (!hasFocus), user wrote the server URL and changed to
639 * other field. The operation to check the existence of the server in the entered URL is
640 * started.
641 *
642 * When hasFocus: user 'comes back' to write again the server URL.
643 *
644 * @param hostInput TextView with the URL input field receiving the change of focus.
645 */
646 private void onUrlInputFocusLost(TextView hostInput) {
647 if (!mHostBaseUrl.equals(normalizeUrl(mHostUrlInput.getText().toString()))) {
648 checkOcServer();
649 } else {
650 mOkButton.setEnabled(mServerIsValid);
651 if (!mServerIsValid) {
652 showRefreshButton();
653 }
654 }
655 }
656
657
658 private void checkOcServer() {
659 String uri = trimUrlWebdav(mHostUrlInput.getText().toString().trim());
660
661 if (!mHostUrlInputEnabled){
662 uri = getString(R.string.server_url);
663 }
664
665 mServerIsValid = false;
666 mServerIsChecked = false;
667 mOkButton.setEnabled(false);
668 mDiscoveredVersion = null;
669 hideRefreshButton();
670 if (uri.length() != 0) {
671 mServerStatusText = R.string.auth_testing_connection;
672 mServerStatusIcon = R.drawable.progress_small;
673 showServerStatus();
674 mOcServerChkOperation = new GetRemoteStatusOperation(uri, this);
675 OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(uri), this, true);
676 mOperationThread = mOcServerChkOperation.execute(client, this, mHandler);
677 } else {
678 mServerStatusText = 0;
679 mServerStatusIcon = 0;
680 showServerStatus();
681 }
682 }
683
684
685 /**
686 * Handles changes in focus on the text input for the password (basic authorization).
687 *
688 * When (hasFocus), the button to toggle password visibility is shown.
689 *
690 * When (!hasFocus), the button is made invisible and the password is hidden.
691 *
692 * @param passwordInput TextView with the password input field receiving the change of focus.
693 * @param hasFocus 'True' if focus is received, 'false' if is lost
694 */
695 private void onPasswordFocusChanged(TextView passwordInput, boolean hasFocus) {
696 if (hasFocus) {
697 showViewPasswordButton();
698 } else {
699 hidePassword();
700 hidePasswordButton();
701 }
702 }
703
704
705 private void showViewPasswordButton() {
706 //int drawable = android.R.drawable.ic_menu_view;
707 int drawable = R.drawable.ic_view;
708 if (isPasswordVisible()) {
709 //drawable = android.R.drawable.ic_secure;
710 drawable = R.drawable.ic_hide;
711 }
712 mPasswordInput.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawable, 0);
713 }
714
715 private boolean isPasswordVisible() {
716 return ((mPasswordInput.getInputType() & InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
717 }
718
719 private void hidePasswordButton() {
720 mPasswordInput.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
721 }
722
723 private void showPassword() {
724 mPasswordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
725 showViewPasswordButton();
726 }
727
728 private void hidePassword() {
729 mPasswordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
730 showViewPasswordButton();
731 }
732
733
734 /**
735 * Cancels the authenticator activity
736 *
737 * IMPORTANT ENTRY POINT 3: Never underestimate the importance of cancellation
738 *
739 * This method is bound in the layout/acceoun_setup.xml resource file.
740 *
741 * @param view Cancel button
742 */
743 public void onCancelClick(View view) {
744 setResult(RESULT_CANCELED); // TODO review how is this related to AccountAuthenticator (debugging)
745 finish();
746 }
747
748
749
750 /**
751 * Checks the credentials of the user in the root of the ownCloud server
752 * before creating a new local account.
753 *
754 * For basic authorization, a check of existence of the root folder is
755 * performed.
756 *
757 * For OAuth, starts the flow to get an access token; the credentials test
758 * is postponed until it is available.
759 *
760 * IMPORTANT ENTRY POINT 4
761 *
762 * @param view OK button
763 */
764 public void onOkClick(View view) {
765 // this check should be unnecessary
766 if (mDiscoveredVersion == null || !mDiscoveredVersion.isVersionValid() || mHostBaseUrl == null || mHostBaseUrl.length() == 0) {
767 mServerStatusIcon = R.drawable.common_error;
768 mServerStatusText = R.string.auth_wtf_reenter_URL;
769 showServerStatus();
770 mOkButton.setEnabled(false);
771 Log_OC.wtf(TAG, "The user was allowed to click 'connect' to an unchecked server!!");
772 return;
773 }
774
775 if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType)) {
776 startOauthorization();
777 } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {
778 startSamlBasedFederatedSingleSignOnAuthorization();
779 } else {
780 checkBasicAuthorization();
781 }
782 }
783
784
785 /**
786 * Tests the credentials entered by the user performing a check of existence on
787 * the root folder of the ownCloud server.
788 */
789 private void checkBasicAuthorization() {
790 /// get the path to the root folder through WebDAV from the version server
791 String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);
792
793 /// get basic credentials entered by user
794 String username = mUsernameInput.getText().toString();
795 String password = mPasswordInput.getText().toString();
796
797 /// be gentle with the user
798 showDialog(DIALOG_LOGIN_PROGRESS);
799
800 /// test credentials accessing the root folder
801 mAuthCheckOperation = new ExistenceCheckRemoteOperation("", this, false);
802 OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);
803 client.setBasicCredentials(username, password);
804 mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);
805 }
806
807
808 /**
809 * Starts the OAuth 'grant type' flow to get an access token, with
810 * a GET AUTHORIZATION request to the BUILT-IN authorization server.
811 */
812 private void startOauthorization() {
813 // be gentle with the user
814 mAuthStatusIcon = R.drawable.progress_small;
815 mAuthStatusText = R.string.oauth_login_connection;
816 showAuthStatus();
817
818
819 // GET AUTHORIZATION request
820 //Uri uri = Uri.parse(getString(R.string.oauth2_url_endpoint_auth));
821 Uri uri = Uri.parse(mOAuthAuthEndpointText.getText().toString().trim());
822 Uri.Builder uriBuilder = uri.buildUpon();
823 uriBuilder.appendQueryParameter(OAuth2Constants.KEY_RESPONSE_TYPE, getString(R.string.oauth2_response_type));
824 uriBuilder.appendQueryParameter(OAuth2Constants.KEY_REDIRECT_URI, getString(R.string.oauth2_redirect_uri));
825 uriBuilder.appendQueryParameter(OAuth2Constants.KEY_CLIENT_ID, getString(R.string.oauth2_client_id));
826 uriBuilder.appendQueryParameter(OAuth2Constants.KEY_SCOPE, getString(R.string.oauth2_scope));
827 //uriBuilder.appendQueryParameter(OAuth2Constants.KEY_STATE, whateverwewant);
828 uri = uriBuilder.build();
829 Log_OC.d(TAG, "Starting browser to view " + uri.toString());
830 Intent i = new Intent(Intent.ACTION_VIEW, uri);
831 startActivity(i);
832 }
833
834
835 /**
836 * Starts the Web Single Sign On flow to get access to the root folder
837 * in the server.
838 */
839 private void startSamlBasedFederatedSingleSignOnAuthorization() {
840 // be gentle with the user
841 mAuthStatusIcon = R.drawable.progress_small;
842 mAuthStatusText = R.string.auth_connecting_auth_server;
843 showAuthStatus();
844 showDialog(DIALOG_LOGIN_PROGRESS);
845
846 /// get the path to the root folder through WebDAV from the version server
847 String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);
848
849 /// test credentials accessing the root folder
850 mAuthCheckOperation = new ExistenceCheckRemoteOperation("", this, false);
851 OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, false);
852 mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);
853
854 }
855
856 /**
857 * Callback method invoked when a RemoteOperation executed by this Activity finishes.
858 *
859 * Dispatches the operation flow to the right method.
860 */
861 @Override
862 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
863
864 if (operation instanceof GetRemoteStatusOperation) {
865 onOcServerCheckFinish((GetRemoteStatusOperation) operation, result);
866
867 } else if (operation instanceof OAuth2GetAccessToken) {
868 onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result);
869
870 } else if (operation instanceof ExistenceCheckRemoteOperation) {
871 if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {
872 onSamlBasedFederatedSingleSignOnAuthorizationStart(operation, result);
873
874 } else {
875 onAuthorizationCheckFinish((ExistenceCheckRemoteOperation)operation, result);
876 }
877 } else if (operation instanceof GetRemoteUserNameOperation) {
878 onGetUserNameFinish((GetRemoteUserNameOperation) operation, result);
879
880 } else if (operation instanceof DetectAuthenticationMethodOperation) {
881 Log.wtf(TAG, "received detection response through callback" );
882 onDetectAuthenticationFinish(result);
883 }
884
885 }
886
887 private void onDetectAuthenticationFinish(RemoteOperationResult result) {
888 // Read authentication method
889 mDetectAuthOpId = -1;
890 if (result.getData().size() > 0) {
891 AuthenticationMethod authMethod = (AuthenticationMethod) result.getData().get(0);
892 String basic = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType());
893 String oAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType());
894 String saml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType());
895
896 if ( ( mAuthTokenType.equals(basic) && !authMethod.equals(AuthenticationMethod.BASIC_HTTP_AUTH) ) ||
897 ( mAuthTokenType.equals(oAuth) && !authMethod.equals(AuthenticationMethod.BEARER_TOKEN) ) ||
898 ( mAuthTokenType.equals(saml) && !authMethod.equals(AuthenticationMethod.SAML_WEB_SSO) ) ) {
899
900 mOkButton.setEnabled(false);
901 mServerIsValid = false;
902 //show an alert message ( Server Status )
903 updateServerStatusIconNoRegularAuth();
904 showServerStatus();
905
906 } else {
907 mOkButton.setEnabled(true);
908
909 // Show server status
910 showServerStatus();
911 }
912
913 }
914 }
915
916
917
918 private void onGetUserNameFinish(GetRemoteUserNameOperation operation, RemoteOperationResult result) {
919
920 if (result.isSuccess()) {
921 boolean success = false;
922 String username = operation.getUserName();
923
924 if ( mAction == ACTION_CREATE) {
925 mUsernameInput.setText(username);
926 success = createAccount();
927 } else {
928
929 if (!mUsernameInput.getText().toString().equals(username)) {
930 // fail - not a new account, but an existing one; disallow
931 result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME);
932 updateAuthStatusIconAndText(result);
933 showAuthStatus();
934 Log_OC.d(TAG, result.getLogMessage());
935 } else {
936 updateToken();
937 success = true;
938 }
939 }
940
941 if (success)
942 finish();
943 } else {
944 updateStatusIconFailUserName();
945 showAuthStatus();
946 Log_OC.e(TAG, "Access to user name failed: " + result.getLogMessage());
947 }
948
949 }
950
951 private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperation operation, RemoteOperationResult result) {
952 try {
953 dismissDialog(DIALOG_LOGIN_PROGRESS);
954 } catch (IllegalArgumentException e) {
955 // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens
956 }
957
958 //if (result.isTemporalRedirection() && result.isIdPRedirection()) {
959 if (result.isIdPRedirection()) {
960 String url = result.getRedirectedLocation();
961 String targetUrl = mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);
962
963 // Show dialog
964 mSamlDialog = SamlWebViewDialog.newInstance(url, targetUrl);
965 mSamlDialog.show(getSupportFragmentManager(), TAG_SAML_DIALOG);
966
967 mAuthStatusIcon = 0;
968 mAuthStatusText = 0;
969
970 } else {
971 mAuthStatusIcon = R.drawable.common_error;
972 mAuthStatusText = R.string.auth_unsupported_auth_method;
973
974 }
975 showAuthStatus();
976 }
977
978
979 /**
980 * Processes the result of the server check performed when the user finishes the enter of the
981 * server URL.
982 *
983 * @param operation Server check performed.
984 * @param result Result of the check.
985 */
986 private void onOcServerCheckFinish(GetRemoteStatusOperation operation, RemoteOperationResult result) {
987 if (operation.equals(mOcServerChkOperation)) {
988 /// save result state
989 mServerIsChecked = true;
990 mServerIsValid = result.isSuccess();
991 mIsSslConn = (result.getCode() == ResultCode.OK_SSL);
992 mOcServerChkOperation = null;
993
994
995 /// retrieve discovered version and normalize server URL
996 mDiscoveredVersion = operation.getDiscoveredVersion();
997 mHostBaseUrl = normalizeUrl(mHostUrlInput.getText().toString());
998
999 // Refresh server status, but don't show it
1000 updateServerStatusIconAndText(result);
1001
1002 /// update status icon and text
1003 if (mServerIsValid) {
1004 hideRefreshButton();
1005 // Try to create an account with user and pass "", to know if it is a regular server
1006 // Update connect button in the answer of this method
1007 detectAuthorizationMethod();
1008 } else {
1009 showRefreshButton();
1010 // Show server status
1011 showServerStatus();
1012 }
1013
1014 /// very special case (TODO: move to a common place for all the remote operations)
1015 if (result.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) {
1016 showUntrustedCertDialog(result);
1017 }
1018
1019
1020 } // else nothing ; only the last check operation is considered;
1021 // multiple can be triggered if the user amends a URL before a previous check can be triggered
1022 }
1023
1024
1025 /**
1026 * Try to access with user/pass ""/"", to know if it is a regular server
1027 */
1028 private void detectAuthorizationMethod() {
1029
1030 Log_OC.d(TAG, "Trying empty authorization to detect authentication method");
1031
1032 String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);
1033
1034 /// test credentials
1035 //Intent detectAuthIntent = new Intent(this, OperationsService.class);
1036 Intent detectAuthIntent = new Intent();
1037 detectAuthIntent.setAction(OperationsService.ACTION_DETECT_AUTHENTICATION_METHOD);
1038 detectAuthIntent.putExtra(OperationsService.EXTRA_SERVER_URL, mHostBaseUrl);
1039 detectAuthIntent.putExtra(OperationsService.EXTRA_WEBDAV_PATH, webdav_path);
1040
1041 //if (mOperationsBinder != null) { // let's let it crash to detect if is really possible
1042 mServerAuthMethod = AuthenticationMethod.UNKNOWN;
1043 if (mOperationsServiceBinder != null) {
1044 //Log.wtf(TAG, "starting detection..." );
1045 mDetectAuthOpId = mOperationsServiceBinder.newOperation(detectAuthIntent);
1046 }
1047 //}
1048 }
1049
1050
1051 private String normalizeUrl(String url) {
1052 if (url != null && url.length() > 0) {
1053 url = url.trim();
1054 if (!url.toLowerCase().startsWith("http://") &&
1055 !url.toLowerCase().startsWith("https://")) {
1056 if (mIsSslConn) {
1057 url = "https://" + url;
1058 } else {
1059 url = "http://" + url;
1060 }
1061 }
1062
1063 // OC-208: Add suffix remote.php/webdav to normalize (OC-34)
1064 url = trimUrlWebdav(url);
1065
1066 if (url.endsWith("/")) {
1067 url = url.substring(0, url.length() - 1);
1068 }
1069
1070 }
1071 return (url != null ? url : "");
1072 }
1073
1074
1075 private String trimUrlWebdav(String url){
1076 if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0)){
1077 url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0.length());
1078 } else if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_2_0)){
1079 url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_2_0.length());
1080 } else if (url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_1_2)){
1081 url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_1_2.length());
1082 }
1083 return (url != null ? url : "");
1084 }
1085
1086
1087 /**
1088 * Chooses the right icon and text to show to the user for the received operation result.
1089 *
1090 * @param result Result of a remote operation performed in this activity
1091 */
1092 private void updateServerStatusIconAndText(RemoteOperationResult result) {
1093 mServerStatusIcon = R.drawable.common_error; // the most common case in the switch below
1094
1095 switch (result.getCode()) {
1096 case OK_SSL:
1097 mServerStatusIcon = android.R.drawable.ic_secure;
1098 mServerStatusText = R.string.auth_secure_connection;
1099 break;
1100
1101 case OK_NO_SSL:
1102 case OK:
1103 if (mHostUrlInput.getText().toString().trim().toLowerCase().startsWith("http://") ) {
1104 mServerStatusText = R.string.auth_connection_established;
1105 mServerStatusIcon = R.drawable.ic_ok;
1106 } else {
1107 mServerStatusText = R.string.auth_nossl_plain_ok_title;
1108 mServerStatusIcon = android.R.drawable.ic_partial_secure;
1109 }
1110 break;
1111
1112 case NO_NETWORK_CONNECTION:
1113 mServerStatusIcon = R.drawable.no_network;
1114 mServerStatusText = R.string.auth_no_net_conn_title;
1115 break;
1116
1117 case SSL_RECOVERABLE_PEER_UNVERIFIED:
1118 mServerStatusText = R.string.auth_ssl_unverified_server_title;
1119 break;
1120 case BAD_OC_VERSION:
1121 mServerStatusText = R.string.auth_bad_oc_version_title;
1122 break;
1123 case WRONG_CONNECTION:
1124 mServerStatusText = R.string.auth_wrong_connection_title;
1125 break;
1126 case TIMEOUT:
1127 mServerStatusText = R.string.auth_timeout_title;
1128 break;
1129 case INCORRECT_ADDRESS:
1130 mServerStatusText = R.string.auth_incorrect_address_title;
1131 break;
1132 case SSL_ERROR:
1133 mServerStatusText = R.string.auth_ssl_general_error_title;
1134 break;
1135 case UNAUTHORIZED:
1136 mServerStatusText = R.string.auth_unauthorized;
1137 break;
1138 case HOST_NOT_AVAILABLE:
1139 mServerStatusText = R.string.auth_unknown_host_title;
1140 break;
1141 case INSTANCE_NOT_CONFIGURED:
1142 mServerStatusText = R.string.auth_not_configured_title;
1143 break;
1144 case FILE_NOT_FOUND:
1145 mServerStatusText = R.string.auth_incorrect_path_title;
1146 break;
1147 case OAUTH2_ERROR:
1148 mServerStatusText = R.string.auth_oauth_error;
1149 break;
1150 case OAUTH2_ERROR_ACCESS_DENIED:
1151 mServerStatusText = R.string.auth_oauth_error_access_denied;
1152 break;
1153 case UNHANDLED_HTTP_CODE:
1154 case UNKNOWN_ERROR:
1155 mServerStatusText = R.string.auth_unknown_error_title;
1156 break;
1157 default:
1158 mServerStatusText = 0;
1159 mServerStatusIcon = 0;
1160 }
1161 }
1162
1163
1164 /**
1165 * Chooses the right icon and text to show to the user for the received operation result.
1166 *
1167 * @param result Result of a remote operation performed in this activity
1168 */
1169 private void updateAuthStatusIconAndText(RemoteOperationResult result) {
1170 mAuthStatusIcon = R.drawable.common_error; // the most common case in the switch below
1171
1172 switch (result.getCode()) {
1173 case OK_SSL:
1174 mAuthStatusIcon = android.R.drawable.ic_secure;
1175 mAuthStatusText = R.string.auth_secure_connection;
1176 break;
1177
1178 case OK_NO_SSL:
1179 case OK:
1180 if (mHostUrlInput.getText().toString().trim().toLowerCase().startsWith("http://") ) {
1181 mAuthStatusText = R.string.auth_connection_established;
1182 mAuthStatusIcon = R.drawable.ic_ok;
1183 } else {
1184 mAuthStatusText = R.string.auth_nossl_plain_ok_title;
1185 mAuthStatusIcon = android.R.drawable.ic_partial_secure;
1186 }
1187 break;
1188
1189 case NO_NETWORK_CONNECTION:
1190 mAuthStatusIcon = R.drawable.no_network;
1191 mAuthStatusText = R.string.auth_no_net_conn_title;
1192 break;
1193
1194 case SSL_RECOVERABLE_PEER_UNVERIFIED:
1195 mAuthStatusText = R.string.auth_ssl_unverified_server_title;
1196 break;
1197 case BAD_OC_VERSION:
1198 mAuthStatusText = R.string.auth_bad_oc_version_title;
1199 break;
1200 case WRONG_CONNECTION:
1201 mAuthStatusText = R.string.auth_wrong_connection_title;
1202 break;
1203 case TIMEOUT:
1204 mAuthStatusText = R.string.auth_timeout_title;
1205 break;
1206 case INCORRECT_ADDRESS:
1207 mAuthStatusText = R.string.auth_incorrect_address_title;
1208 break;
1209 case SSL_ERROR:
1210 mAuthStatusText = R.string.auth_ssl_general_error_title;
1211 break;
1212 case UNAUTHORIZED:
1213 mAuthStatusText = R.string.auth_unauthorized;
1214 break;
1215 case HOST_NOT_AVAILABLE:
1216 mAuthStatusText = R.string.auth_unknown_host_title;
1217 break;
1218 case INSTANCE_NOT_CONFIGURED:
1219 mAuthStatusText = R.string.auth_not_configured_title;
1220 break;
1221 case FILE_NOT_FOUND:
1222 mAuthStatusText = R.string.auth_incorrect_path_title;
1223 break;
1224 case OAUTH2_ERROR:
1225 mAuthStatusText = R.string.auth_oauth_error;
1226 break;
1227 case OAUTH2_ERROR_ACCESS_DENIED:
1228 mAuthStatusText = R.string.auth_oauth_error_access_denied;
1229 break;
1230 case ACCOUNT_NOT_NEW:
1231 mAuthStatusText = R.string.auth_account_not_new;
1232 break;
1233 case ACCOUNT_NOT_THE_SAME:
1234 mAuthStatusText = R.string.auth_account_not_the_same;
1235 break;
1236 case UNHANDLED_HTTP_CODE:
1237 case UNKNOWN_ERROR:
1238 mAuthStatusText = R.string.auth_unknown_error_title;
1239 break;
1240 default:
1241 mAuthStatusText = 0;
1242 mAuthStatusIcon = 0;
1243 }
1244 }
1245
1246
1247 private void updateStatusIconFailUserName(){
1248 mAuthStatusIcon = R.drawable.common_error;
1249 mAuthStatusText = R.string.auth_fail_get_user_name;
1250 }
1251
1252 private void updateServerStatusIconNoRegularAuth(){
1253 mServerStatusIcon = R.drawable.common_error;
1254 mServerStatusText = R.string.auth_can_not_auth_against_server;
1255 }
1256
1257 /**
1258 * Processes the result of the request for and access token send
1259 * to an OAuth authorization server.
1260 *
1261 * @param operation Operation performed requesting the access token.
1262 * @param result Result of the operation.
1263 */
1264 private void onGetOAuthAccessTokenFinish(OAuth2GetAccessToken operation, RemoteOperationResult result) {
1265 try {
1266 dismissDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);
1267 } catch (IllegalArgumentException e) {
1268 // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens
1269 }
1270
1271 String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);
1272 if (result.isSuccess() && webdav_path != null) {
1273 /// be gentle with the user
1274 showDialog(DIALOG_LOGIN_PROGRESS);
1275
1276 /// time to test the retrieved access token on the ownCloud server
1277 mAuthToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);
1278 Log_OC.d(TAG, "Got ACCESS TOKEN: " + mAuthToken);
1279 mAuthCheckOperation = new ExistenceCheckRemoteOperation("", this, false);
1280 OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);
1281 client.setBearerCredentials(mAuthToken);
1282 mAuthCheckOperation.execute(client, this, mHandler);
1283
1284 } else {
1285 updateAuthStatusIconAndText(result);
1286 showAuthStatus();
1287 Log_OC.d(TAG, "Access failed: " + result.getLogMessage());
1288 }
1289 }
1290
1291
1292 /**
1293 * Processes the result of the access check performed to try the user credentials.
1294 *
1295 * Creates a new account through the AccountManager.
1296 *
1297 * @param operation Access check performed.
1298 * @param result Result of the operation.
1299 */
1300 private void onAuthorizationCheckFinish(ExistenceCheckRemoteOperation operation, RemoteOperationResult result) {
1301 try {
1302 dismissDialog(DIALOG_LOGIN_PROGRESS);
1303 } catch (IllegalArgumentException e) {
1304 // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens
1305 }
1306
1307 if (result.isSuccess()) {
1308 Log_OC.d(TAG, "Successful access - time to save the account");
1309
1310 boolean success = false;
1311 if (mAction == ACTION_CREATE) {
1312 success = createAccount();
1313
1314 } else {
1315 updateToken();
1316 success = true;
1317 }
1318
1319 if (success) {
1320 finish();
1321 }
1322
1323 } else if (result.isServerFail() || result.isException()) {
1324 /// if server fail or exception in authorization, the UI is updated as when a server check failed
1325 mServerIsChecked = true;
1326 mServerIsValid = false;
1327 mIsSslConn = false;
1328 mOcServerChkOperation = null;
1329 mDiscoveredVersion = null;
1330 mHostBaseUrl = normalizeUrl(mHostUrlInput.getText().toString());
1331
1332 // update status icon and text
1333 updateServerStatusIconAndText(result);
1334 showServerStatus();
1335 mAuthStatusIcon = 0;
1336 mAuthStatusText = 0;
1337 showAuthStatus();
1338
1339 // update input controls state
1340 showRefreshButton();
1341 mOkButton.setEnabled(false);
1342
1343 // very special case (TODO: move to a common place for all the remote operations) (dangerous here?)
1344 if (result.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) {
1345 showUntrustedCertDialog(result);
1346 }
1347
1348 } else { // authorization fail due to client side - probably wrong credentials
1349 updateAuthStatusIconAndText(result);
1350 showAuthStatus();
1351 Log_OC.d(TAG, "Access failed: " + result.getLogMessage());
1352 }
1353 }
1354
1355
1356
1357
1358 /**
1359 * Sets the proper response to get that the Account Authenticator that started this activity saves
1360 * a new authorization token for mAccount.
1361 */
1362 private void updateToken() {
1363 Bundle response = new Bundle();
1364 response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);
1365 response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type);
1366
1367 if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType)) {
1368 response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);
1369 // the next line is necessary; by now, notifications are calling directly to the AuthenticatorActivity to update, without AccountManager intervention
1370 mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);
1371
1372 } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {
1373
1374 response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);
1375 // the next line is necessary; by now, notifications are calling directly to the AuthenticatorActivity to update, without AccountManager intervention
1376 mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);
1377
1378 } else {
1379 response.putString(AccountManager.KEY_AUTHTOKEN, mPasswordInput.getText().toString());
1380 mAccountMgr.setPassword(mAccount, mPasswordInput.getText().toString());
1381 }
1382 setAccountAuthenticatorResult(response);
1383
1384 }
1385
1386
1387 /**
1388 * Creates a new account through the Account Authenticator that started this activity.
1389 *
1390 * This makes the account permanent.
1391 *
1392 * TODO Decide how to name the OAuth accounts
1393 */
1394 private boolean createAccount() {
1395 /// create and save new ownCloud account
1396 boolean isOAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType);
1397 boolean isSaml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType);
1398
1399 Uri uri = Uri.parse(mHostBaseUrl);
1400 String username = mUsernameInput.getText().toString().trim();
1401 if (isOAuth) {
1402 username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();
1403 }
1404 String accountName = username + "@" + uri.getHost();
1405 if (uri.getPort() >= 0) {
1406 accountName += ":" + uri.getPort();
1407 }
1408 mAccount = new Account(accountName, MainApp.getAccountType());
1409 if (AccountUtils.exists(mAccount, getApplicationContext())) {
1410 // fail - not a new account, but an existing one; disallow
1411 RemoteOperationResult result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_NEW);
1412 updateAuthStatusIconAndText(result);
1413 showAuthStatus();
1414 Log_OC.d(TAG, result.getLogMessage());
1415 return false;
1416
1417 } else {
1418
1419 if (isOAuth || isSaml) {
1420 mAccountMgr.addAccountExplicitly(mAccount, "", null); // with external authorizations, the password is never input in the app
1421 } else {
1422 mAccountMgr.addAccountExplicitly(mAccount, mPasswordInput.getText().toString(), null);
1423 }
1424
1425 /// add the new account as default in preferences, if there is none already
1426 Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
1427 if (defaultAccount == null) {
1428 SharedPreferences.Editor editor = PreferenceManager
1429 .getDefaultSharedPreferences(this).edit();
1430 editor.putString("select_oc_account", accountName);
1431 editor.commit();
1432 }
1433
1434 /// prepare result to return to the Authenticator
1435 // TODO check again what the Authenticator makes with it; probably has the same effect as addAccountExplicitly, but it's not well done
1436 final Intent intent = new Intent();
1437 intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, MainApp.getAccountType());
1438 intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);
1439 /*if (!isOAuth)
1440 intent.putExtra(AccountManager.KEY_AUTHTOKEN, MainApp.getAccountType()); */
1441 intent.putExtra(AccountManager.KEY_USERDATA, username);
1442 if (isOAuth || isSaml) {
1443 mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);
1444 }
1445 /// add user data to the new account; TODO probably can be done in the last parameter addAccountExplicitly, or in KEY_USERDATA
1446 mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION, mDiscoveredVersion.getVersion());
1447 mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION_STRING, mDiscoveredVersion.getVersionString());
1448 mAccountMgr.setUserData(mAccount, Constants.KEY_OC_BASE_URL, mHostBaseUrl);
1449
1450 if (isSaml) {
1451 mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE");
1452 } else if (isOAuth) {
1453 mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_OAUTH2, "TRUE");
1454 }
1455
1456 setAccountAuthenticatorResult(intent.getExtras());
1457 setResult(RESULT_OK, intent);
1458
1459 return true;
1460 }
1461 }
1462
1463
1464 /**
1465 * {@inheritDoc}
1466 *
1467 * Necessary to update the contents of the SSL Dialog
1468 *
1469 * TODO move to some common place for all possible untrusted SSL failures
1470 */
1471 @Override
1472 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
1473 switch (id) {
1474 case DIALOG_LOGIN_PROGRESS:
1475 case DIALOG_CERT_NOT_SAVED:
1476 case DIALOG_OAUTH2_LOGIN_PROGRESS:
1477 break;
1478 default:
1479 Log_OC.e(TAG, "Incorrect dialog called with id = " + id);
1480 }
1481 }
1482
1483
1484 /**
1485 * {@inheritDoc}
1486 */
1487 @Override
1488 protected Dialog onCreateDialog(int id) {
1489 Dialog dialog = null;
1490 switch (id) {
1491 case DIALOG_LOGIN_PROGRESS: {
1492 /// simple progress dialog
1493 ProgressDialog working_dialog = new ProgressDialog(this);
1494 working_dialog.setMessage(getResources().getString(R.string.auth_trying_to_login));
1495 working_dialog.setIndeterminate(true);
1496 working_dialog.setCancelable(true);
1497 working_dialog
1498 .setOnCancelListener(new DialogInterface.OnCancelListener() {
1499 @Override
1500 public void onCancel(DialogInterface dialog) {
1501 /// TODO study if this is enough
1502 Log_OC.i(TAG, "Login canceled");
1503 if (mOperationThread != null) {
1504 mOperationThread.interrupt();
1505 finish();
1506 }
1507 }
1508 });
1509 dialog = working_dialog;
1510 break;
1511 }
1512 case DIALOG_OAUTH2_LOGIN_PROGRESS: {
1513 ProgressDialog working_dialog = new ProgressDialog(this);
1514 working_dialog.setMessage(String.format("Getting authorization"));
1515 working_dialog.setIndeterminate(true);
1516 working_dialog.setCancelable(true);
1517 working_dialog
1518 .setOnCancelListener(new DialogInterface.OnCancelListener() {
1519 @Override
1520 public void onCancel(DialogInterface dialog) {
1521 Log_OC.i(TAG, "Login canceled");
1522 finish();
1523 }
1524 });
1525 dialog = working_dialog;
1526 break;
1527 }
1528 case DIALOG_CERT_NOT_SAVED: {
1529 AlertDialog.Builder builder = new AlertDialog.Builder(this);
1530 builder.setMessage(getResources().getString(R.string.ssl_validator_not_saved));
1531 builder.setCancelable(false);
1532 builder.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
1533 @Override
1534 public void onClick(DialogInterface dialog, int which) {
1535 dialog.dismiss();
1536 };
1537 });
1538 dialog = builder.create();
1539 break;
1540 }
1541 default:
1542 Log_OC.e(TAG, "Incorrect dialog called with id = " + id);
1543 }
1544 return dialog;
1545 }
1546
1547
1548 /**
1549 * Starts and activity to open the 'new account' page in the ownCloud web site
1550 *
1551 * @param view 'Account register' button
1552 */
1553 public void onRegisterClick(View view) {
1554 Intent register = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.welcome_link_url)));
1555 setResult(RESULT_CANCELED);
1556 startActivity(register);
1557 }
1558
1559
1560 /**
1561 * Updates the content and visibility state of the icon and text associated
1562 * to the last check on the ownCloud server.
1563 */
1564 private void showServerStatus() {
1565 TextView tv = (TextView) findViewById(R.id.server_status_text);
1566
1567 if (mServerStatusIcon == 0 && mServerStatusText == 0) {
1568 tv.setVisibility(View.INVISIBLE);
1569
1570 } else {
1571 tv.setText(mServerStatusText);
1572 tv.setCompoundDrawablesWithIntrinsicBounds(mServerStatusIcon, 0, 0, 0);
1573 tv.setVisibility(View.VISIBLE);
1574 }
1575
1576 }
1577
1578
1579 /**
1580 * Updates the content and visibility state of the icon and text associated
1581 * to the interactions with the OAuth authorization server.
1582 */
1583 private void showAuthStatus() {
1584 if (mAuthStatusIcon == 0 && mAuthStatusText == 0) {
1585 mAuthStatusLayout.setVisibility(View.INVISIBLE);
1586
1587 } else {
1588 mAuthStatusLayout.setText(mAuthStatusText);
1589 mAuthStatusLayout.setCompoundDrawablesWithIntrinsicBounds(mAuthStatusIcon, 0, 0, 0);
1590 mAuthStatusLayout.setVisibility(View.VISIBLE);
1591 }
1592 }
1593
1594
1595 private void showRefreshButton() {
1596 mRefreshButton.setVisibility(View.VISIBLE);
1597 }
1598
1599 private void hideRefreshButton() {
1600 mRefreshButton.setVisibility(View.GONE);
1601 }
1602
1603 /**
1604 * Called when the refresh button in the input field for ownCloud host is clicked.
1605 *
1606 * Performs a new check on the URL in the input field.
1607 *
1608 * @param view Refresh 'button'
1609 */
1610 public void onRefreshClick(View view) {
1611 checkOcServer();
1612 }
1613
1614
1615 /**
1616 * Called when the eye icon in the password field is clicked.
1617 *
1618 * Toggles the visibility of the password in the field.
1619 */
1620 public void onViewPasswordClick() {
1621 int selectionStart = mPasswordInput.getSelectionStart();
1622 int selectionEnd = mPasswordInput.getSelectionEnd();
1623 if (isPasswordVisible()) {
1624 hidePassword();
1625 } else {
1626 showPassword();
1627 }
1628 mPasswordInput.setSelection(selectionStart, selectionEnd);
1629 }
1630
1631
1632 /**
1633 * Called when the checkbox for OAuth authorization is clicked.
1634 *
1635 * Hides or shows the input fields for user & password.
1636 *
1637 * @param view 'View password' 'button'
1638 */
1639 public void onCheckClick(View view) {
1640 CheckBox oAuth2Check = (CheckBox)view;
1641 if (oAuth2Check.isChecked()) {
1642 mAuthTokenType = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType());
1643 } else {
1644 mAuthTokenType = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType());
1645 }
1646 adaptViewAccordingToAuthenticationMethod();
1647 }
1648
1649
1650 /**
1651 * Changes the visibility of input elements depending on
1652 * the current authorization method.
1653 */
1654 private void adaptViewAccordingToAuthenticationMethod () {
1655 if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType)) {
1656 // OAuth 2 authorization
1657 mOAuthAuthEndpointText.setVisibility(View.VISIBLE);
1658 mOAuthTokenEndpointText.setVisibility(View.VISIBLE);
1659 mUsernameInput.setVisibility(View.GONE);
1660 mPasswordInput.setVisibility(View.GONE);
1661
1662 } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {
1663 // SAML-based web Single Sign On
1664 mOAuthAuthEndpointText.setVisibility(View.GONE);
1665 mOAuthTokenEndpointText.setVisibility(View.GONE);
1666 mUsernameInput.setVisibility(View.GONE);
1667 mPasswordInput.setVisibility(View.GONE);
1668 } else {
1669 // basic HTTP authorization
1670 mOAuthAuthEndpointText.setVisibility(View.GONE);
1671 mOAuthTokenEndpointText.setVisibility(View.GONE);
1672 mUsernameInput.setVisibility(View.VISIBLE);
1673 mPasswordInput.setVisibility(View.VISIBLE);
1674 }
1675 }
1676
1677 /**
1678 * Called when the 'action' button in an IME is pressed ('enter' in software keyboard).
1679 *
1680 * Used to trigger the authentication check when the user presses 'enter' after writing the password,
1681 * or to throw the server test when the only field on screen is the URL input field.
1682 */
1683 @Override
1684 public boolean onEditorAction(TextView inputField, int actionId, KeyEvent event) {
1685 if (actionId == EditorInfo.IME_ACTION_DONE && inputField != null && inputField.equals(mPasswordInput)) {
1686 if (mOkButton.isEnabled()) {
1687 mOkButton.performClick();
1688 }
1689
1690 } else if (actionId == EditorInfo.IME_ACTION_NEXT && inputField != null && inputField.equals(mHostUrlInput)) {
1691 if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {
1692 checkOcServer();
1693 }
1694 }
1695 return false; // always return false to grant that the software keyboard is hidden anyway
1696 }
1697
1698
1699 private abstract static class RightDrawableOnTouchListener implements OnTouchListener {
1700
1701 private int fuzz = 75;
1702
1703 /**
1704 * {@inheritDoc}
1705 */
1706 @Override
1707 public boolean onTouch(View view, MotionEvent event) {
1708 Drawable rightDrawable = null;
1709 if (view instanceof TextView) {
1710 Drawable[] drawables = ((TextView)view).getCompoundDrawables();
1711 if (drawables.length > 2) {
1712 rightDrawable = drawables[2];
1713 }
1714 }
1715 if (rightDrawable != null) {
1716 final int x = (int) event.getX();
1717 final int y = (int) event.getY();
1718 final Rect bounds = rightDrawable.getBounds();
1719 if (x >= (view.getRight() - bounds.width() - fuzz) && x <= (view.getRight() - view.getPaddingRight() + fuzz)
1720 && y >= (view.getPaddingTop() - fuzz) && y <= (view.getHeight() - view.getPaddingBottom()) + fuzz) {
1721
1722 return onDrawableTouch(event);
1723 }
1724 }
1725 return false;
1726 }
1727
1728 public abstract boolean onDrawableTouch(final MotionEvent event);
1729 }
1730
1731
1732 public void onSamlDialogSuccess(String sessionCookie) {
1733 mAuthToken = sessionCookie;
1734
1735 if (sessionCookie != null && sessionCookie.length() > 0) {
1736 mAuthToken = sessionCookie;
1737
1738 GetRemoteUserNameOperation getUserOperation = new GetRemoteUserNameOperation();
1739 OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl), getApplicationContext(), true);
1740 client.setSsoSessionCookie(mAuthToken);
1741 getUserOperation.execute(client, this, mHandler);
1742 }
1743
1744
1745 }
1746
1747
1748 @Override
1749 public void onSsoFinished(String sessionCookies) {
1750 //Toast.makeText(this, "got cookies: " + sessionCookie, Toast.LENGTH_LONG).show();
1751
1752 if (sessionCookies != null && sessionCookies.length() > 0) {
1753 Log_OC.d(TAG, "Successful SSO - time to save the account");
1754 onSamlDialogSuccess(sessionCookies);
1755 Fragment fd = getSupportFragmentManager().findFragmentByTag(TAG_SAML_DIALOG);
1756 if (fd != null && fd instanceof SherlockDialogFragment) {
1757 Dialog d = ((SherlockDialogFragment)fd).getDialog();
1758 if (d != null && d.isShowing()) {
1759 d.dismiss();
1760 }
1761 }
1762
1763 } else {
1764 // TODO - show fail
1765 Log_OC.d(TAG, "SSO failed");
1766 }
1767
1768 }
1769
1770 /** Show auth_message
1771 *
1772 * @param message
1773 */
1774 private void showAuthMessage(String message) {
1775 mAuthMessage.setVisibility(View.VISIBLE);
1776 mAuthMessage.setText(message);
1777 }
1778
1779 private void hideAuthMessage() {
1780 mAuthMessage.setVisibility(View.GONE);
1781 }
1782
1783 @Override
1784 public boolean onTouchEvent(MotionEvent event) {
1785 if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType) &&
1786 mHostUrlInput.hasFocus() && event.getAction() == MotionEvent.ACTION_DOWN) {
1787 checkOcServer();
1788 }
1789 return super.onTouchEvent(event);
1790 }
1791
1792
1793 /**
1794 * Show untrusted cert dialog
1795 */
1796 public void showUntrustedCertDialog(X509Certificate x509Certificate, SslError error, SslErrorHandler handler) {
1797 // Show a dialog with the certificate info
1798 SslUntrustedCertDialog dialog = null;
1799 if (x509Certificate == null) {
1800 dialog = SslUntrustedCertDialog.newInstanceForEmptySslError(error, handler);
1801 } else {
1802 dialog = SslUntrustedCertDialog.newInstanceForFullSslError(x509Certificate, error, handler);
1803 }
1804 FragmentManager fm = getSupportFragmentManager();
1805 FragmentTransaction ft = fm.beginTransaction();
1806 ft.addToBackStack(null);
1807 dialog.show(ft, DIALOG_UNTRUSTED_CERT);
1808 }
1809
1810 /**
1811 * Show untrusted cert dialog
1812 */
1813 public void showUntrustedCertDialog(RemoteOperationResult result) {
1814 // Show a dialog with the certificate info
1815 SslUntrustedCertDialog dialog = SslUntrustedCertDialog.newInstanceForFullSslError((CertificateCombinedException)result.getException());
1816 FragmentManager fm = getSupportFragmentManager();
1817 FragmentTransaction ft = fm.beginTransaction();
1818 ft.addToBackStack(null);
1819 dialog.show(ft, DIALOG_UNTRUSTED_CERT);
1820
1821 }
1822
1823 /**
1824 * Dismiss untrusted cert dialog
1825 */
1826 public void dismissUntrustedCertDialog(){
1827 /*Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_UNTRUSTED_CERT);
1828 if (frag != null) {
1829 SslErrorViewAdapter dialog = (SslErrorViewAdapter) frag;
1830 dialog.dismiss();
1831 }
1832 */
1833 }
1834
1835 /**
1836 * Called from SslValidatorDialog when a new server certificate was correctly saved.
1837 */
1838 public void onSavedCertificate() {
1839 Fragment fd = getSupportFragmentManager().findFragmentByTag(TAG_SAML_DIALOG);
1840 if (fd == null) {
1841 // if SAML dialog is not shown, the SslDialog was shown due to an SSL error in the server check
1842 checkOcServer();
1843 }
1844 }
1845
1846 /**
1847 * Called from SslValidatorDialog when a new server certificate could not be saved
1848 * when the user requested it.
1849 */
1850 @Override
1851 public void onFailedSavingCertificate() {
1852 showDialog(DIALOG_CERT_NOT_SAVED);
1853 cancelWebView();
1854 }
1855
1856 @Override
1857 public void onCancelCertificate() {
1858 cancelWebView();
1859 }
1860
1861
1862 public void cancelWebView() {
1863 Fragment fd = getSupportFragmentManager().findFragmentByTag(TAG_SAML_DIALOG);
1864 if (fd != null && fd instanceof SherlockDialogFragment) {
1865 Dialog d = ((SherlockDialogFragment)fd).getDialog();
1866 if (d != null && d.isShowing()) {
1867 d.dismiss();
1868 }
1869 }
1870
1871 }
1872
1873
1874 private void doOnResumeAndBound() {
1875 //Log.wtf(TAG, "registering to listen for operation callbacks" );
1876 mOperationsServiceBinder.addOperationListener(AuthenticatorActivity.this, mHandler);
1877
1878 if (mDetectAuthOpId != -1) {
1879 RemoteOperationResult result =
1880 mOperationsServiceBinder.getOperationResultIfFinished(mDetectAuthOpId);
1881 if (result != null) {
1882 //Log.wtf(TAG, "found result of operation finished while rotating");
1883 onDetectAuthenticationFinish(result);
1884 }
1885 }
1886 }
1887
1888 /**
1889 * Implements callback methods for service binding.
1890 */
1891 private class OperationsServiceConnection implements ServiceConnection {
1892
1893 @Override
1894 public void onServiceConnected(ComponentName component, IBinder service) {
1895 if (component.equals(new ComponentName(AuthenticatorActivity.this, OperationsService.class))) {
1896 //Log_OC.wtf(TAG, "Operations service connected");
1897 mOperationsServiceBinder = (OperationsServiceBinder) service;
1898
1899 doOnResumeAndBound();
1900
1901 } else {
1902 return;
1903 }
1904
1905 }
1906
1907 @Override
1908 public void onServiceDisconnected(ComponentName component) {
1909 if (component.equals(new ComponentName(AuthenticatorActivity.this, OperationsService.class))) {
1910 Log_OC.e(TAG, "Operations service crashed");
1911 mOperationsServiceBinder = null;
1912 }
1913 }
1914
1915 }
1916
1917 }