X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/65d590b8e7ddb2c91a63f3104037dc3f7197f6bd..eda6ddd98f723053e8c65c7cac4065ddde1e401e:/src/com/owncloud/android/authentication/AuthenticatorActivity.java diff --git a/src/com/owncloud/android/authentication/AuthenticatorActivity.java b/src/com/owncloud/android/authentication/AuthenticatorActivity.java index f18ff2ab..73bbaecb 100644 --- a/src/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/src/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -48,6 +48,7 @@ import android.view.View.OnFocusChangeListener; import android.view.View.OnTouchListener; import android.view.Window; import android.view.inputmethod.EditorInfo; +import android.webkit.SslErrorHandler; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; @@ -64,6 +65,7 @@ import com.owncloud.android.lib.common.OwnCloudClientFactory; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.operations.OAuth2GetAccessToken; +import com.owncloud.android.lib.common.network.CertificateCombinedException; import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation; import com.owncloud.android.lib.common.operations.RemoteOperation; @@ -75,7 +77,6 @@ import com.owncloud.android.lib.resources.users.GetRemoteUserNameOperation; import com.owncloud.android.ui.dialog.SamlWebViewDialog; import com.owncloud.android.ui.dialog.SslUntrustedCertDialog; import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener; -import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener; import com.owncloud.android.utils.Log_OC; import com.owncloud.android.lib.resources.status.OwnCloudVersion; @@ -86,7 +87,7 @@ import com.owncloud.android.lib.resources.status.OwnCloudVersion; * @author David A. Velasco */ public class AuthenticatorActivity extends AccountAuthenticatorActivity - implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeListener, OnEditorActionListener, + implements OnRemoteOperationListener, OnFocusChangeListener, OnEditorActionListener, SsoWebViewClientListener, OnSslUntrustedCertListener { private static final String TAG = AuthenticatorActivity.class.getSimpleName(); @@ -174,6 +175,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity private boolean mResumed; // Control if activity is resumed public static String DIALOG_UNTRUSTED_CERT = "DIALOG_UNTRUSTED_CERT"; + + private boolean mTryEmptyAuthorization = false; /** @@ -814,6 +817,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } private void onGetUserNameFinish(GetRemoteUserNameOperation operation, RemoteOperationResult result) { + if (result.isSuccess()) { boolean success = false; String username = operation.getUserName(); @@ -838,7 +842,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity if (success) finish(); } else { - updateAuthStatusIconAndText(result); + updateStatusIconFailUserName(); showAuthStatus(); Log_OC.e(TAG, "Access to user name failed: " + result.getLogMessage()); } @@ -891,6 +895,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity /// update status icon and text if (mServerIsValid) { hideRefreshButton(); + // Try to create an account with user and pass "", to know if it is a regular server + tryEmptyAuthorization(); } else { showRefreshButton(); } @@ -906,14 +912,40 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity mDiscoveredVersion = operation.getDiscoveredVersion(); mHostBaseUrl = normalizeUrl(mHostUrlInput.getText().toString()); - /// allow or not the user try to access the server - mOkButton.setEnabled(mServerIsValid); +// /// allow or not the user try to access the server +// mOkButton.setEnabled(mServerIsValid); } // else nothing ; only the last check operation is considered; // multiple can be triggered if the user amends a URL before a previous check can be triggered } + /** + * Try to access with user/pass ""/"", to know if it is a regular server + */ + private void tryEmptyAuthorization() { + mTryEmptyAuthorization = true; + + Log_OC.d(TAG, "Trying empty authorization to detect authentication method"); + + /// be gentle with the user + showDialog(DIALOG_LOGIN_PROGRESS); + + /// get the path to the root folder through WebDAV from the version server + String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType); + + /// get basic credentials entered by user + String username = ""; + String password = ""; + + /// test credentials + mAuthCheckOperation = new ExistenceCheckRemoteOperation("", this, false); + OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true); + client.setBasicCredentials(username, password); + mOperationThread = mAuthCheckOperation.execute(client, this, mHandler); + } + + private String normalizeUrl(String url) { if (url != null && url.length() > 0) { url = url.trim(); @@ -1110,6 +1142,11 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } + private void updateStatusIconFailUserName(){ + mAuthStatusIcon = android.R.drawable.ic_secure; + mAuthStatusText = R.string.auth_fail_get_user_name; + } + /** * Processes the result of the request for and access token send * to an OAuth authorization server. @@ -1159,21 +1196,29 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } catch (IllegalArgumentException e) { // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens } - + if (result.isSuccess()) { - Log_OC.d(TAG, "Successful access - time to save the account"); + + if (mTryEmptyAuthorization) { + //allow or not the user try to access the server + mOkButton.setEnabled(mServerIsValid); + mTryEmptyAuthorization = false; + + } else { + Log_OC.d(TAG, "Successful access - time to save the account"); - boolean success = false; - if (mAction == ACTION_CREATE) { - success = createAccount(); + boolean success = false; + if (mAction == ACTION_CREATE) { + success = createAccount(); - } else { - updateToken(); - success = true; - } + } else { + updateToken(); + success = true; + } - if (success) { - finish(); + if (success) { + finish(); + } } } else if (result.isServerFail() || result.isException()) { @@ -1530,26 +1575,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } /** - * Called from SslValidatorDialog when a new server certificate was correctly saved. - */ - public void onSavedCertificate() { - checkOcServer(); - reloadWebView(); - - } - - /** - * Called from SslValidatorDialog when a new server certificate could not be saved - * when the user requested it. - */ - @Override - public void onFailedSavingCertificate() { - showDialog(DIALOG_CERT_NOT_SAVED); - cancelWebView(); - } - - - /** * Called when the 'action' button in an IME is pressed ('enter' in software keyboard). * * Used to trigger the authentication check when the user presses 'enter' after writing the password, @@ -1665,40 +1690,21 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity } - - public void cancelWebView() { - Fragment fd = getSupportFragmentManager().findFragmentByTag(TAG_SAML_DIALOG); - if (fd != null && fd instanceof SherlockDialogFragment) { - Dialog d = ((SherlockDialogFragment)fd).getDialog(); - if (d != null && d.isShowing()) { - d.dismiss(); - } - } - - } - - public void reloadWebView() { - Fragment fd = getSupportFragmentManager().findFragmentByTag(TAG_SAML_DIALOG); - if (fd != null && fd instanceof SamlWebViewDialog) { - ((SamlWebViewDialog) fd).reloadWebView(); - } - } - - @Override - public void onCancelCertificate() { - cancelWebView(); - } - /** * Show untrusted cert dialog */ - public void showUntrustedCertDialog(X509Certificate x509Certificate, SslError error) { + public void showUntrustedCertDialog(X509Certificate x509Certificate, SslError error, SslErrorHandler handler) { // Show a dialog with the certificate info - SslUntrustedCertDialog dialog = SslUntrustedCertDialog.newInstance(x509Certificate, error); + SslUntrustedCertDialog dialog = null; + if (x509Certificate == null) { + dialog = SslUntrustedCertDialog.newInstanceForEmptySslError(error, handler); + } else { + dialog = SslUntrustedCertDialog.newInstanceForFullSslError(x509Certificate, error, handler); + } FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); + ft.addToBackStack(null); dialog.show(ft, DIALOG_UNTRUSTED_CERT); - } /** @@ -1706,9 +1712,10 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity */ public void showUntrustedCertDialog(RemoteOperationResult result) { // Show a dialog with the certificate info - SslUntrustedCertDialog dialog = SslUntrustedCertDialog.newInstance(result, this); + SslUntrustedCertDialog dialog = SslUntrustedCertDialog.newInstanceForFullSslError((CertificateCombinedException)result.getException()); FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); + ft.addToBackStack(null); dialog.show(ft, DIALOG_UNTRUSTED_CERT); } @@ -1717,11 +1724,50 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity * Dismiss untrusted cert dialog */ public void dismissUntrustedCertDialog(){ - Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_UNTRUSTED_CERT); + /*Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_UNTRUSTED_CERT); if (frag != null) { - SslUntrustedCertDialog dialog = (SslUntrustedCertDialog) frag; + SslErrorViewAdapter dialog = (SslErrorViewAdapter) frag; dialog.dismiss(); } + */ } + /** + * Called from SslValidatorDialog when a new server certificate was correctly saved. + */ + public void onSavedCertificate() { + Fragment fd = getSupportFragmentManager().findFragmentByTag(TAG_SAML_DIALOG); + if (fd == null) { + // if SAML dialog is not shown, the SslDialog was shown due to an SSL error in the server check + checkOcServer(); + } + } + + /** + * Called from SslValidatorDialog when a new server certificate could not be saved + * when the user requested it. + */ + @Override + public void onFailedSavingCertificate() { + showDialog(DIALOG_CERT_NOT_SAVED); + cancelWebView(); + } + + @Override + public void onCancelCertificate() { + cancelWebView(); + } + + + public void cancelWebView() { + Fragment fd = getSupportFragmentManager().findFragmentByTag(TAG_SAML_DIALOG); + if (fd != null && fd instanceof SherlockDialogFragment) { + Dialog d = ((SherlockDialogFragment)fd).getDialog(); + if (d != null && d.isShowing()) { + d.dismiss(); + } + } + + } + }