AuthenticatorActivity updated to test existance through SAML-based-federated-authenti...
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
index d1f79cf..35496c3 100644 (file)
@@ -96,7 +96,6 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
     private static final String KEY_SERVER_STATUS_ICON = "SERVER_STATUS_ICON";\r
     private static final String KEY_IS_SSL_CONN = "IS_SSL_CONN";\r
     private static final String KEY_PASSWORD_VISIBLE = "PASSWORD_VISIBLE";\r
-    private static final String KEY_AUTH_METHOD = "AUTH_METHOD";\r
     private static final String KEY_AUTH_STATUS_TEXT = "AUTH_STATUS_TEXT";\r
     private static final String KEY_AUTH_STATUS_ICON = "AUTH_STATUS_ICON";\r
     private static final String KEY_REFRESH_BUTTON_ENABLED = "KEY_REFRESH_BUTTON_ENABLED";\r
@@ -105,10 +104,6 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
     private static final String AUTH_OFF = "off";\r
     private static final String AUTH_OPTIONAL = "optional";\r
     \r
-    private static final int AUTH_METHOD_BASIC_HTTP = 0;\r
-    private static final int AUTH_METHOD_OAUTH2 = 1;\r
-    private static final int AUTH_METHOD_SAML_WEB_SSO = 2;\r
-\r
     private static final int DIALOG_LOGIN_PROGRESS = 0;\r
     private static final int DIALOG_SSL_VALIDATOR = 1;\r
     private static final int DIALOG_CERT_NOT_SAVED = 2;\r
@@ -142,7 +137,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
     private boolean mHostUrlInputEnabled;\r
     private View mRefreshButton;\r
 \r
-    private int mCurrentAuthorizationMethod;  \r
+    private String mCurrentAuthTokenType;\r
     \r
     private EditText mUsernameInput;\r
     private EditText mPasswordInput;\r
@@ -230,7 +225,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
                 mHostUrlInput.setText(mHostBaseUrl);\r
             }\r
             initAuthorizationMethod();  // checks intent and setup.xml to determine mCurrentAuthorizationMethod\r
-            mOAuth2Check.setChecked(mCurrentAuthorizationMethod == AUTH_METHOD_OAUTH2);\r
+            mOAuth2Check.setChecked(mCurrentAuthTokenType == AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN);\r
             mJustCreated = true;\r
             \r
             if (mAction == ACTION_UPDATE_TOKEN || !mHostUrlInputEnabled) {\r
@@ -259,7 +254,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
 \r
             // account data, if updating\r
             mAccount = savedInstanceState.getParcelable(KEY_ACCOUNT);\r
-            mCurrentAuthorizationMethod = savedInstanceState.getInt(KEY_AUTH_METHOD, AUTH_METHOD_BASIC_HTTP);\r
+            mCurrentAuthTokenType = savedInstanceState.getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE, AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD);\r
 \r
             // check if server check was interrupted by a configuration change\r
             if (savedInstanceState.getBoolean(KEY_SERVER_CHECK_IN_PROGRESS, false)) {\r
@@ -289,7 +284,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         if (mServerIsChecked && !mServerIsValid && refreshButtonEnabled) showRefreshButton();\r
         mOkButton.setEnabled(mServerIsValid); // state not automatically recovered in configuration changes\r
 \r
-        if (mCurrentAuthorizationMethod == AUTH_METHOD_SAML_WEB_SSO || !AUTH_OPTIONAL.equals(getString(R.string.auth_method_oauth2))) {\r
+        if (mCurrentAuthTokenType == AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE || \r
+                !AUTH_OPTIONAL.equals(getString(R.string.auth_method_oauth2))) {\r
             mOAuth2Check.setVisibility(View.GONE);\r
         }\r
 \r
@@ -331,33 +327,31 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         boolean oAuthRequired = false;\r
         boolean samlWebSsoRequired = false;\r
 \r
-        String tokenType = getIntent().getExtras().getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE);\r
+        mCurrentAuthTokenType = getIntent().getExtras().getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE);\r
         mAccount = getIntent().getExtras().getParcelable(EXTRA_ACCOUNT);\r
         \r
-        if (tokenType != null) {\r
-            /// use the authentication method requested by caller \r
-            oAuthRequired = AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(tokenType);\r
-            samlWebSsoRequired = AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(tokenType);\r
-            \r
-        } else if (mAccount != null) {\r
-            /// same authentication method than the one used to create the account to update\r
-            oAuthRequired = (mAccountMgr.getUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);\r
-            samlWebSsoRequired = (mAccountMgr.getUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO) != null);\r
-            \r
-        } else {\r
-            /// use the one set in setup.xml\r
-            oAuthRequired = AUTH_ON.equals(getString(R.string.auth_method_oauth2));\r
-            samlWebSsoRequired = AUTH_ON.equals(getString(R.string.auth_method_saml_web_sso));            \r
-        }\r
+        // TODO could be a good moment to validate the received token type, if not null\r
         \r
-        if (oAuthRequired) {\r
-            mCurrentAuthorizationMethod = AUTH_METHOD_OAUTH2; \r
-        } else if (samlWebSsoRequired) {\r
-            mCurrentAuthorizationMethod = AUTH_METHOD_SAML_WEB_SSO;\r
-        } else {\r
-            mCurrentAuthorizationMethod = AUTH_METHOD_BASIC_HTTP;\r
+        if (mCurrentAuthTokenType == null) {    \r
+            if (mAccount != null) {\r
+                /// same authentication method than the one used to create the account to update\r
+                oAuthRequired = (mAccountMgr.getUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);\r
+                samlWebSsoRequired = (mAccountMgr.getUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO) != null);\r
+            \r
+            } else {\r
+                /// use the one set in setup.xml\r
+                oAuthRequired = AUTH_ON.equals(getString(R.string.auth_method_oauth2));\r
+                samlWebSsoRequired = AUTH_ON.equals(getString(R.string.auth_method_saml_web_sso));            \r
+            }\r
+            if (oAuthRequired) {\r
+                mCurrentAuthTokenType = AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN;\r
+            } else if (samlWebSsoRequired) {\r
+                mCurrentAuthTokenType = AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE;\r
+            } else {\r
+                mCurrentAuthTokenType = AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD;\r
+            }\r
         }\r
-\r
+    \r
         if (mAccount != null) {\r
             String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));\r
             mUsernameInput.setText(userName);\r
@@ -397,7 +391,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         if (mAccount != null) {\r
             outState.putParcelable(KEY_ACCOUNT, mAccount);\r
         }\r
-        outState.putInt(KEY_AUTH_METHOD, mCurrentAuthorizationMethod);\r
+        outState.putString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE, mCurrentAuthTokenType);\r
         \r
         // refresh button enabled\r
         outState.putBoolean(KEY_REFRESH_BUTTON_ENABLED, (mRefreshButton.getVisibility() == View.VISIBLE));\r
@@ -626,9 +620,10 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             return;\r
         }\r
 \r
-        if (mOAuth2Check.isChecked()) {\r
+        if (AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(mCurrentAuthTokenType)) {\r
             startOauthorization();\r
-\r
+        } else if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType)) { \r
+            startSamlBasedFederatedSingleSignOnAuthorization();\r
         } else {\r
             checkBasicAuthorization();\r
         }\r
@@ -641,7 +636,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
      */\r
     private void checkBasicAuthorization() {\r
         /// get the path to the root folder through WebDAV from the version server\r
-        String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, false);\r
+        String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType);\r
 \r
         /// get basic credentials entered by user\r
         String username = mUsernameInput.getText().toString();\r
@@ -685,6 +680,20 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
 \r
 \r
     /**\r
+     * Starts the Web Single Sign On flow to get access to the root folder\r
+     * in the server.\r
+     */\r
+    private void startSamlBasedFederatedSingleSignOnAuthorization() {\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
+        mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);\r
+    }\r
+\r
+    /**\r
      * Callback method invoked when a RemoteOperation executed by this Activity finishes.\r
      * \r
      * Dispatches the operation flow to the right method.\r
@@ -699,8 +708,12 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result);\r
 \r
         } else if (operation instanceof ExistenceCheckOperation)  {\r
-            onAuthorizationCheckFinish((ExistenceCheckOperation)operation, result);\r
-\r
+            if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType)) {\r
+                Toast.makeText(this, result.getLogMessage(), Toast.LENGTH_LONG).show();\r
+                \r
+            } else {\r
+                onAuthorizationCheckFinish((ExistenceCheckOperation)operation, result);\r
+            }\r
         }\r
     }\r
 \r
@@ -952,7 +965,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             // 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
-        String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, true);\r
+        String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType);\r
         if (result.isSuccess() && webdav_path != null) {\r
             /// be gentle with the user\r
             showDialog(DIALOG_LOGIN_PROGRESS);\r
@@ -1305,9 +1318,9 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
     public void onCheckClick(View view) {\r
         CheckBox oAuth2Check = (CheckBox)view;\r
         if (oAuth2Check.isChecked()) {\r
-            mCurrentAuthorizationMethod = AUTH_METHOD_OAUTH2;\r
+            mCurrentAuthTokenType = AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN;\r
         } else {\r
-            mCurrentAuthorizationMethod = AUTH_METHOD_BASIC_HTTP;\r
+            mCurrentAuthTokenType = AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD;\r
         }\r
         adaptViewAccordingToAuthenticationMethod();\r
     }\r
@@ -1318,37 +1331,33 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
      * the current authorization method.\r
      */\r
     private void adaptViewAccordingToAuthenticationMethod () {\r
-        switch (mCurrentAuthorizationMethod) { \r
-            case AUTH_METHOD_OAUTH2:\r
-                // OAuth 2 authorization\r
-                mOAuthAuthEndpointText.setVisibility(View.VISIBLE);\r
-                mOAuthTokenEndpointText.setVisibility(View.VISIBLE);\r
-                mUsernameInput.setVisibility(View.GONE);\r
-                mPasswordInput.setVisibility(View.GONE);\r
-                mAccountNameInput.setVisibility(View.GONE);\r
-                mWebSsoView.setVisibility(View.GONE);\r
-                break;\r
-                \r
-            case AUTH_METHOD_SAML_WEB_SSO:\r
-                // SAML-based web Single Sign On\r
-                mOAuthAuthEndpointText.setVisibility(View.GONE);\r
-                mOAuthTokenEndpointText.setVisibility(View.GONE);\r
-                mUsernameInput.setVisibility(View.GONE);\r
-                mPasswordInput.setVisibility(View.GONE);\r
-                mAccountNameInput.setVisibility(View.VISIBLE);\r
-                mWebSsoView.setVisibility(View.VISIBLE);\r
-                break;\r
-                \r
-            case AUTH_METHOD_BASIC_HTTP:\r
-            default:\r
-                // basic HTTP authorization\r
-                mOAuthAuthEndpointText.setVisibility(View.GONE);\r
-                mOAuthTokenEndpointText.setVisibility(View.GONE);\r
-                mUsernameInput.setVisibility(View.VISIBLE);\r
-                mPasswordInput.setVisibility(View.VISIBLE);\r
-                mAccountNameInput.setVisibility(View.GONE);\r
-                mWebSsoView.setVisibility(View.GONE);\r
-            }\r
+        if (AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(mCurrentAuthTokenType)) {\r
+            // OAuth 2 authorization\r
+            mOAuthAuthEndpointText.setVisibility(View.VISIBLE);\r
+            mOAuthTokenEndpointText.setVisibility(View.VISIBLE);\r
+            mUsernameInput.setVisibility(View.GONE);\r
+            mPasswordInput.setVisibility(View.GONE);\r
+            mAccountNameInput.setVisibility(View.GONE);\r
+            mWebSsoView.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
+            mOAuthAuthEndpointText.setVisibility(View.GONE);\r
+            mOAuthTokenEndpointText.setVisibility(View.GONE);\r
+            mUsernameInput.setVisibility(View.GONE);\r
+            mPasswordInput.setVisibility(View.GONE);\r
+            mAccountNameInput.setVisibility(View.VISIBLE);\r
+            mWebSsoView.setVisibility(View.VISIBLE);\r
+            \r
+        } else {\r
+            // basic HTTP authorization\r
+            mOAuthAuthEndpointText.setVisibility(View.GONE);\r
+            mOAuthTokenEndpointText.setVisibility(View.GONE);\r
+            mUsernameInput.setVisibility(View.VISIBLE);\r
+            mPasswordInput.setVisibility(View.VISIBLE);\r
+            mAccountNameInput.setVisibility(View.GONE);\r
+            mWebSsoView.setVisibility(View.GONE);\r
+        }\r
     }\r
     \r
     /**\r