Merge branch 'idn_hosts' of https://github.com/ogasser/android into idn_hosts
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
index b87c2dc..ee38c4b 100644 (file)
@@ -50,7 +50,9 @@ import android.view.View.OnFocusChangeListener;
 import android.view.View.OnTouchListener;\r
 import android.view.Window;\r
 import android.view.inputmethod.EditorInfo;\r
+import android.webkit.HttpAuthHandler;\r
 import android.webkit.SslErrorHandler;\r
+import android.webkit.WebView;\r
 import android.widget.Button;\r
 import android.widget.CheckBox;\r
 import android.widget.EditText;\r
@@ -64,26 +66,26 @@ import com.owncloud.android.R;
 import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;\r
 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;\r
 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;\r
-import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod;\r
-import com.owncloud.android.operations.GetServerInfoOperation;\r
-import com.owncloud.android.operations.OAuth2GetAccessToken;\r
-
 import com.owncloud.android.lib.common.network.CertificateCombinedException;\r
 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;\r
 import com.owncloud.android.lib.common.operations.RemoteOperation;\r
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;\r
 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;\r
+import com.owncloud.android.lib.common.utils.Log_OC;\r
 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;\r
+import com.owncloud.android.lib.resources.status.OwnCloudVersion;\r
 import com.owncloud.android.lib.resources.users.GetRemoteUserNameOperation;\r
-\r
+import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod;\r
+import com.owncloud.android.operations.GetServerInfoOperation;\r
+import com.owncloud.android.operations.OAuth2GetAccessToken;\r
 import com.owncloud.android.services.OperationsService;\r
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;\r
+import com.owncloud.android.ui.dialog.CredentialsDialogFragment;\r
 import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;\r
 import com.owncloud.android.ui.dialog.SamlWebViewDialog;\r
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;\r
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;\r
-import com.owncloud.android.utils.Log_OC;\r
-import com.owncloud.android.lib.resources.status.OwnCloudVersion;\r
+import com.owncloud.android.utils.DisplayUtils;\r
 \r
 /**\r
  * This Activity is used to add an ownCloud account to the App\r
@@ -127,6 +129,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private static final String UNTRUSTED_CERT_DIALOG_TAG = "UNTRUSTED_CERT_DIALOG";\r
     private static final String SAML_DIALOG_TAG = "SAML_DIALOG";\r
     private static final String WAIT_DIALOG_TAG = "WAIT_DIALOG";\r
+    private static final String CREDENTIALS_DIALOG_TAG = "CREDENTIALS_DIALOG";\r
+    private static final String KEY_AUTH_IS_FIRST_ATTEMPT_TAG = "KEY_AUTH_IS_FIRST_ATTEMPT";\r
 \r
     \r
     /// parameters from EXTRAs in starter Intent\r
@@ -172,6 +176,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     \r
     private String mAuthToken = "";\r
 \r
+    private boolean mIsFirstAuthAttempt;\r
+\r
     \r
     /// Identifier of operation in progress which result shouldn't be lost \r
     private long mWaitingForOpId = Long.MAX_VALUE;\r
@@ -188,6 +194,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         super.onCreate(savedInstanceState);\r
         getWindow().requestFeature(Window.FEATURE_NO_TITLE);\r
 \r
+        mIsFirstAuthAttempt = true;\r
+\r
         // bind to Operations Service\r
         mOperationsServiceConnection = new OperationsServiceConnection();\r
         if (!bindService(new Intent(this, OperationsService.class), \r
@@ -212,6 +220,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         } else {\r
             mAuthTokenType = savedInstanceState.getString(KEY_AUTH_TOKEN_TYPE);\r
             mWaitingForOpId = savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID);\r
+            mIsFirstAuthAttempt = savedInstanceState.getBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG);\r
         }\r
         \r
         /// load user interface\r
@@ -343,7 +352,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         \r
         /// step 2 - set properties of UI elements (text, visibility, enabled...)\r
         mHostUrlInput = (EditText) findViewById(R.id.hostUrlInput);\r
-        mHostUrlInput.setText(mServerInfo.mBaseUrl);\r
+        // Convert IDN to Unicode\r
+        mHostUrlInput.setText(DisplayUtils.convertIdn(mServerInfo.mBaseUrl, false));\r
         if (mAction != ACTION_CREATE) {\r
             /// lock things that should not change\r
             mHostUrlInput.setEnabled(false);\r
@@ -558,6 +568,9 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         outState.putInt(KEY_AUTH_STATUS_TEXT, mAuthStatusText);\r
         outState.putString(KEY_AUTH_TOKEN, mAuthToken);\r
 \r
+        /// authentication\r
+        outState.putBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG, mIsFirstAuthAttempt);\r
+\r
         //Log_OC.wtf(TAG, "onSaveInstanceState end" );\r
     }\r
 \r
@@ -716,6 +729,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         showRefreshButton(false);\r
         \r
         if (uri.length() != 0) {\r
+            // Handle internationalized domain names\r
+            uri = DisplayUtils.convertIdn(uri, true);\r
             mServerStatusText = R.string.auth_testing_connection;\r
             mServerStatusIcon = R.drawable.progress_small;\r
             showServerStatus();\r
@@ -723,9 +738,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             Intent getServerInfoIntent = new Intent();\r
             getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO);\r
             getServerInfoIntent.putExtra(OperationsService.EXTRA_SERVER_URL, uri);\r
-            getServerInfoIntent.putExtra(OperationsService.EXTRA_AUTH_TOKEN_TYPE, mAuthTokenType);\r
             if (mOperationsServiceBinder != null) {\r
-                //Log_OC.wtf(TAG, "checking server..." );\r
                 mWaitingForOpId = mOperationsServiceBinder.newOperation(getServerInfoIntent);\r
             } else {\r
               Log_OC.wtf(TAG, "Server check tried with OperationService unbound!" );\r
@@ -828,9 +841,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * the root folder of the ownCloud server.\r
      */\r
     private void checkBasicAuthorization() {\r
-        /// get the path to the root folder through WebDAV from the version server\r
-        String webdav_path = AccountUtils.getWebdavPath(mServerInfo.mVersion, mAuthTokenType);\r
-\r
         /// get basic credentials entered by user\r
         String username = mUsernameInput.getText().toString();\r
         String password = mPasswordInput.getText().toString();\r
@@ -840,26 +850,19 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 IndeterminateProgressDialog.newInstance(R.string.auth_trying_to_login, true);\r
         dialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);\r
 \r
-        /// test credentials accessing the root folder\r
-        String remotePath ="";\r
-        boolean successIfAbsent = false;\r
-        boolean followRedirects = true;\r
-        startExistenceCheckRemoteOperation(remotePath, this, successIfAbsent, webdav_path, username, password, followRedirects);\r
+        /// validate credentials accessing the root folder\r
+        accessRootFolderRemoteOperation(username, password);\r
         \r
     }\r
 \r
-    private void startExistenceCheckRemoteOperation(String remotePath, Context context, boolean successIfAbsent, String webdav_path,\r
-            String username, String password, boolean followRedirects) {\r
+    private void accessRootFolderRemoteOperation(String username, String password) {\r
         Intent existenceCheckIntent = new Intent();\r
         existenceCheckIntent.setAction(OperationsService.ACTION_EXISTENCE_CHECK);\r
         existenceCheckIntent.putExtra(OperationsService.EXTRA_SERVER_URL, mServerInfo.mBaseUrl);\r
-        existenceCheckIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);\r
-        existenceCheckIntent.putExtra(OperationsService.EXTRA_SUCCESS_IF_ABSENT, successIfAbsent);\r
-        existenceCheckIntent.putExtra(OperationsService.EXTRA_WEBDAV_PATH, webdav_path);\r
+        existenceCheckIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, "/");\r
         existenceCheckIntent.putExtra(OperationsService.EXTRA_USERNAME, username);\r
         existenceCheckIntent.putExtra(OperationsService.EXTRA_PASSWORD, password);\r
         existenceCheckIntent.putExtra(OperationsService.EXTRA_AUTH_TOKEN, mAuthToken);\r
-        existenceCheckIntent.putExtra(OperationsService.EXTRA_FOLLOW_REDIRECTS, followRedirects);\r
         \r
         if (mOperationsServiceBinder != null) {\r
             //Log_OC.wtf(TAG, "starting existenceCheckRemoteOperation..." );\r
@@ -904,14 +907,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 IndeterminateProgressDialog.newInstance(R.string.auth_trying_to_login, true);\r
         dialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);\r
 \r
-        /// get the path to the root folder through WebDAV from the version server\r
-        String webdav_path = AccountUtils.getWebdavPath(mServerInfo.mVersion, mAuthTokenType);\r
-\r
-        /// test credentials accessing the root folder\r
-        String remotePath ="";\r
-        boolean successIfAbsent = false;\r
-        boolean followRedirections = false;\r
-        startExistenceCheckRemoteOperation(remotePath, this, successIfAbsent, webdav_path, "", "", followRedirections);\r
+        /// validate credentials accessing the root folder\r
+        accessRootFolderRemoteOperation("", "");\r
 \r
     }\r
 \r
@@ -959,7 +956,15 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
 \r
                 if (!mUsernameInput.getText().toString().equals(username)) {\r
                     // fail - not a new account, but an existing one; disallow\r
-                    result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME); \r
+                    result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME);\r
+                    /*\r
+                    OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(\r
+                            new OwnCloudAccount(\r
+                                    Uri.parse(mServerInfo.mBaseUrl),\r
+                                    OwnCloudCredentialsFactory.newSamlSsoCredentials(mAuthToken))\r
+                            );\r
+                            */\r
+                    mAuthToken = "";\r
                     updateAuthStatusIconAndText(result);\r
                     showAuthStatus();\r
                     Log_OC.d(TAG, result.getLogMessage());\r
@@ -1174,6 +1179,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         case UNKNOWN_ERROR:\r
             mServerStatusText = R.string.auth_unknown_error_title;\r
             break;\r
+        case OK_REDIRECT_TO_NON_SECURE_CONNECTION:\r
+            mServerStatusIcon = android.R.drawable.ic_partial_secure;\r
+            mServerStatusText = R.string.auth_redirect_non_secure_connection_title;\r
+            break;\r
         default:\r
             mServerStatusText = 0;\r
             mServerStatusIcon = 0;\r
@@ -1284,8 +1293,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         mWaitingForOpId = Long.MAX_VALUE;\r
         dismissDialog(WAIT_DIALOG_TAG);\r
 \r
-        String webdav_path = AccountUtils.getWebdavPath(mServerInfo.mVersion, mAuthTokenType);\r
-        if (result.isSuccess() && webdav_path != null) {\r
+        if (result.isSuccess()) {\r
             /// be gentle with the user\r
             IndeterminateProgressDialog dialog = \r
                     IndeterminateProgressDialog.newInstance(R.string.auth_trying_to_login, true);\r
@@ -1298,10 +1306,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             //mAuthToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);\r
             Log_OC.d(TAG, "Got ACCESS TOKEN: " + mAuthToken);\r
             \r
-            String remotePath ="";\r
-            boolean successIfAbsent = false;\r
-            boolean followRedirects = true;\r
-            startExistenceCheckRemoteOperation(remotePath, this, successIfAbsent, webdav_path, "", "", followRedirects);\r
+            accessRootFolderRemoteOperation("", "");\r
 \r
         } else {\r
             updateAuthStatusIconAndText(result);\r
@@ -1640,7 +1645,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         getUserNameIntent.setAction(OperationsService.ACTION_GET_USER_NAME);\r
         getUserNameIntent.putExtra(OperationsService.EXTRA_SERVER_URL, mServerInfo.mBaseUrl);\r
         getUserNameIntent.putExtra(OperationsService.EXTRA_COOKIE, sessionCookie);\r
-        getUserNameIntent.putExtra(OperationsService.EXTRA_FOLLOW_REDIRECTS, followRedirects);\r
         \r
         if (mOperationsServiceBinder != null) {\r
             //Log_OC.wtf(TAG, "starting getRemoteUserNameOperation..." );\r
@@ -1697,6 +1701,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         dialog.show(ft, UNTRUSTED_CERT_DIALOG_TAG);\r
     }\r
 \r
+\r
     /**\r
      * Show untrusted cert dialog \r
      */\r
@@ -1787,5 +1792,33 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         }\r
     \r
     }\r
-    \r
+\r
+    /**\r
+     * Create and show dialog for request authentication to the user\r
+     * @param webView\r
+     * @param handler\r
+     */\r
+    public void createAuthenticationDialog(WebView webView, HttpAuthHandler handler) {\r
+\r
+        // Show a dialog with the certificate info\r
+        CredentialsDialogFragment dialog = CredentialsDialogFragment.newInstanceForCredentials(webView, handler);\r
+        FragmentManager fm = getSupportFragmentManager();\r
+        FragmentTransaction ft = fm.beginTransaction();\r
+        ft.addToBackStack(null);\r
+        dialog.setCancelable(false);\r
+        dialog.show(ft, CREDENTIALS_DIALOG_TAG);\r
+\r
+        if (!mIsFirstAuthAttempt) {\r
+            Toast.makeText(getApplicationContext(), getText(R.string.saml_authentication_wrong_pass), Toast.LENGTH_LONG).show();\r
+        } else {\r
+            mIsFirstAuthAttempt = false;\r
+        }\r
+    }\r
+\r
+    /**\r
+     * For retrieving the clicking on authentication cancel button\r
+     */\r
+    public void doNegativeAuthenticatioDialogClick(){\r
+        mIsFirstAuthAttempt = true;\r
+    }\r
 }\r