Improved status messages for authentication through SAML-based federated SSO
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
index 35496c3..f648b6e 100644 (file)
@@ -34,6 +34,7 @@ import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
 import android.accounts.Account;\r
 import android.accounts.AccountAuthenticatorActivity;\r
 import android.accounts.AccountManager;\r
+import android.annotation.SuppressLint;\r
 import android.app.AlertDialog;\r
 import android.app.Dialog;\r
 import android.app.ProgressDialog;\r
@@ -57,6 +58,8 @@ 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.CookieManager;\r
+import android.webkit.WebSettings;\r
 import android.webkit.WebView;\r
 import android.widget.CheckBox;\r
 import android.widget.EditText;\r
@@ -149,7 +152,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
     private TextView mOAuthTokenEndpointText;\r
     \r
     private TextView mAccountNameInput;\r
-    private WebView mWebSsoView;\r
+    private WebView mSsoWebView;\r
+    private SsoWebViewClient mWebViewClient;\r
     \r
     private View mOkButton;\r
 \r
@@ -174,7 +178,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         mOAuthTokenEndpointText = (TextView)findViewById(R.id.oAuthEntryPoint_2);\r
         mOAuth2Check = (CheckBox) findViewById(R.id.oauth_onOff_check);\r
         mAccountNameInput = (EditText) findViewById(R.id.account_name);\r
-        mWebSsoView = (WebView) findViewById(R.id.web_sso_view);\r
+        mSsoWebView = (WebView) findViewById(R.id.web_sso_view);\r
         mOkButton = findViewById(R.id.buttonOK);\r
         mAuthStatusLayout = (TextView) findViewById(R.id.auth_status_text); \r
         \r
@@ -231,7 +235,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             if (mAction == ACTION_UPDATE_TOKEN || !mHostUrlInputEnabled) {\r
                 checkOcServer(); \r
             }\r
-\r
+            \r
         } else {\r
             /// connection state and info\r
             mServerIsValid = savedInstanceState.getBoolean(KEY_SERVER_VALID);\r
@@ -254,7 +258,10 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
 \r
             // account data, if updating\r
             mAccount = savedInstanceState.getParcelable(KEY_ACCOUNT);\r
-            mCurrentAuthTokenType = savedInstanceState.getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE, AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD);\r
+            mCurrentAuthTokenType = savedInstanceState.getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE);\r
+            if (mCurrentAuthTokenType == null) {\r
+                mCurrentAuthTokenType =  AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD;\r
+            }\r
 \r
             // check if server check was interrupted by a configuration change\r
             if (savedInstanceState.getBoolean(KEY_SERVER_CHECK_IN_PROGRESS, false)) {\r
@@ -291,7 +298,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
 \r
         mPasswordInput.setText("");     // clean password to avoid social hacking (disadvantage: password in removed if the device is turned aside)\r
 \r
-        /// bind view elements to listeners\r
+        /// bind view elements to listeners and other friends\r
         mHostUrlInput.setOnFocusChangeListener(this);\r
         mHostUrlInput.addTextChangedListener(new TextWatcher() {\r
 \r
@@ -320,7 +327,24 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
                 }\r
                 return true;\r
             }\r
-        });
+        });\r
+        \r
+    }\r
+    \r
+    @SuppressLint("SetJavaScriptEnabled")\r
+       private void initWebView() {\r
+        CookieManager cookieManager = CookieManager.getInstance();\r
+        cookieManager.setAcceptCookie(true);\r
+        //cookieManager.removeSessionCookie();        \r
+\r
+        mWebViewClient = new SsoWebViewClient(this);\r
+        mSsoWebView.setWebViewClient(mWebViewClient);\r
+        WebSettings webSettings = mSsoWebView.getSettings();\r
+        webSettings.setJavaScriptEnabled(true);\r
+        webSettings.setBuiltInZoomControls(true);\r
+        webSettings.setLoadWithOverviewMode(false);\r
+        webSettings.setSavePassword(false);\r
+        webSettings.setUserAgentString(WebdavClient.USER_AGENT);\r
     }\r
 \r
     private void initAuthorizationMethod() {\r
@@ -456,7 +480,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
                 getString(R.string.oauth2_grant_type),\r
                 queryParameters);\r
         //WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(getString(R.string.oauth2_url_endpoint_access)), getApplicationContext());\r
-        WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(mOAuthTokenEndpointText.getText().toString().trim()), getApplicationContext());\r
+        WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(mOAuthTokenEndpointText.getText().toString().trim()), getApplicationContext(), true);\r
         operation.execute(client, this, mHandler);\r
     }\r
 \r
@@ -520,7 +544,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             mServerStatusIcon = R.drawable.progress_small;\r
             showServerStatus();\r
             mOcServerChkOperation = new  OwnCloudServerCheckOperation(uri, this);\r
-            WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(uri), this);\r
+            WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(uri), this, true);\r
             mOperationThread = mOcServerChkOperation.execute(client, this, mHandler);\r
         } else {\r
             mServerStatusText = 0;\r
@@ -647,7 +671,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
 \r
         /// test credentials accessing the root folder\r
         mAuthCheckOperation = new  ExistenceCheckOperation("", this, false);\r
-        WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this);\r
+        WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);\r
         client.setBasicCredentials(username, password);\r
         mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);\r
     }\r
@@ -662,6 +686,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         mAuthStatusIcon = R.drawable.progress_small;\r
         mAuthStatusText = R.string.oauth_login_connection;\r
         showAuthStatus();\r
+        \r
 \r
         // GET AUTHORIZATION request\r
         //Uri uri = Uri.parse(getString(R.string.oauth2_url_endpoint_auth));\r
@@ -684,12 +709,18 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
      * in the server.\r
      */\r
     private void startSamlBasedFederatedSingleSignOnAuthorization() {\r
+        // be gentle with the user\r
+        mAuthStatusIcon = R.drawable.progress_small;\r
+        mAuthStatusText = R.string.auth_connecting_auth_server;\r
+        showAuthStatus();\r
+        showDialog(DIALOG_LOGIN_PROGRESS);\r
+        \r
         /// get the path to the root folder through WebDAV from the version server\r
         String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType);\r
 \r
         /// test credentials accessing the root folder\r
         mAuthCheckOperation = new  ExistenceCheckOperation("", this, false);\r
-        WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this);\r
+        WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, false);\r
         mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);\r
     }\r
 \r
@@ -709,13 +740,37 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
 \r
         } else if (operation instanceof ExistenceCheckOperation)  {\r
             if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType)) {\r
-                Toast.makeText(this, result.getLogMessage(), Toast.LENGTH_LONG).show();\r
+                onSamlBasedFederatedSingleSignOnAuthorizationStart(operation, result);\r
                 \r
             } else {\r
                 onAuthorizationCheckFinish((ExistenceCheckOperation)operation, result);\r
             }\r
         }\r
     }\r
+    \r
+    \r
+    private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperation operation, RemoteOperationResult result) {\r
+        try {\r
+            dismissDialog(DIALOG_LOGIN_PROGRESS);\r
+        } catch (IllegalArgumentException e) {\r
+            // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens\r
+        }\r
+\r
+        if (result.isTemporalRedirection()) {\r
+            String url = result.getRedirectedLocation();\r
+            mWebViewClient.setTargetUrl(mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType));\r
+            mSsoWebView.loadUrl(url);\r
+            \r
+            mAuthStatusIcon = android.R.drawable.ic_secure;\r
+            mAuthStatusText = R.string.auth_follow_auth_server;\r
+            \r
+        } else {\r
+            mAuthStatusIcon = R.drawable.common_error;\r
+            mAuthStatusText = R.string.auth_unsupported_auth_method;\r
+            \r
+        }\r
+        showAuthStatus();\r
+    }\r
 \r
 \r
     /**\r
@@ -974,7 +1029,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             mOAuthAccessToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);\r
             Log_OC.d(TAG, "Got ACCESS TOKEN: " + mOAuthAccessToken);\r
             mAuthCheckOperation = new ExistenceCheckOperation("", this, false);\r
-            WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this);\r
+            WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);\r
             client.setBearerCredentials(mOAuthAccessToken);\r
             mAuthCheckOperation.execute(client, this, mHandler);\r
 \r
@@ -1338,7 +1393,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             mUsernameInput.setVisibility(View.GONE);\r
             mPasswordInput.setVisibility(View.GONE);\r
             mAccountNameInput.setVisibility(View.GONE);\r
-            mWebSsoView.setVisibility(View.GONE);\r
+            mSsoWebView.setVisibility(View.GONE);\r
             \r
         } else if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType)) {\r
             // SAML-based web Single Sign On\r
@@ -1347,7 +1402,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             mUsernameInput.setVisibility(View.GONE);\r
             mPasswordInput.setVisibility(View.GONE);\r
             mAccountNameInput.setVisibility(View.VISIBLE);\r
-            mWebSsoView.setVisibility(View.VISIBLE);\r
+            mSsoWebView.setVisibility(View.VISIBLE);\r
+            initWebView();\r
             \r
         } else {\r
             // basic HTTP authorization\r
@@ -1356,7 +1412,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             mUsernameInput.setVisibility(View.VISIBLE);\r
             mPasswordInput.setVisibility(View.VISIBLE);\r
             mAccountNameInput.setVisibility(View.GONE);\r
-            mWebSsoView.setVisibility(View.GONE);\r
+            mSsoWebView.setVisibility(View.GONE);\r
         }\r
     }\r
     \r