OC-2465: Move getUserNameForSamlSso to AccountUtils in oc_framework
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
index 105d644..9d6d3f3 100644 (file)
@@ -23,7 +23,6 @@ import android.accounts.AccountManager;
 import android.app.AlertDialog;\r
 import android.app.Dialog;\r
 import android.app.ProgressDialog;\r
 import android.app.AlertDialog;\r
 import android.app.Dialog;\r
 import android.app.ProgressDialog;\r
-import android.content.ContentResolver;\r
 import android.content.DialogInterface;\r
 import android.content.Intent;\r
 import android.content.SharedPreferences;\r
 import android.content.DialogInterface;\r
 import android.content.Intent;\r
 import android.content.SharedPreferences;\r
@@ -51,24 +50,24 @@ import android.widget.TextView;
 import android.widget.TextView.OnEditorActionListener;\r
 \r
 import com.actionbarsherlock.app.SherlockDialogFragment;\r
 import android.widget.TextView.OnEditorActionListener;\r
 \r
 import com.actionbarsherlock.app.SherlockDialogFragment;\r
-import com.owncloud.android.Log_OC;\r
 import com.owncloud.android.MainApp;\r
 import com.owncloud.android.R;\r
 import com.owncloud.android.MainApp;\r
 import com.owncloud.android.R;\r
-import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;\r
 import com.owncloud.android.oc_framework.accounts.AccountTypeUtils;\r
 import com.owncloud.android.oc_framework.accounts.OwnCloudAccount;\r
 import com.owncloud.android.oc_framework.accounts.AccountTypeUtils;\r
 import com.owncloud.android.oc_framework.accounts.OwnCloudAccount;\r
+import com.owncloud.android.oc_framework.accounts.SsoWebViewClient.SsoWebViewClientListener;\r
 import com.owncloud.android.oc_framework.network.webdav.OwnCloudClientFactory;\r
 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;\r
 import com.owncloud.android.oc_framework.network.webdav.OwnCloudClientFactory;\r
 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;\r
-import com.owncloud.android.operations.ExistenceCheckOperation;\r
 import com.owncloud.android.operations.OAuth2GetAccessToken;\r
 import com.owncloud.android.oc_framework.operations.OnRemoteOperationListener;\r
 import com.owncloud.android.operations.OwnCloudServerCheckOperation;\r
 import com.owncloud.android.oc_framework.operations.RemoteOperation;\r
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;\r
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;\r
 import com.owncloud.android.operations.OAuth2GetAccessToken;\r
 import com.owncloud.android.oc_framework.operations.OnRemoteOperationListener;\r
 import com.owncloud.android.operations.OwnCloudServerCheckOperation;\r
 import com.owncloud.android.oc_framework.operations.RemoteOperation;\r
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;\r
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;\r
+import com.owncloud.android.oc_framework.operations.remote.ExistenceCheckRemoteOperation;\r
 import com.owncloud.android.ui.dialog.SamlWebViewDialog;\r
 import com.owncloud.android.ui.dialog.SslValidatorDialog;\r
 import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;\r
 import com.owncloud.android.ui.dialog.SamlWebViewDialog;\r
 import com.owncloud.android.ui.dialog.SslValidatorDialog;\r
 import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;\r
+import com.owncloud.android.utils.Log_OC;\r
 import com.owncloud.android.oc_framework.utils.OwnCloudVersion;\r
 \r
 /**\r
 import com.owncloud.android.oc_framework.utils.OwnCloudVersion;\r
 \r
 /**\r
@@ -104,7 +103,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
     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
     \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
     \r
-    private static final String KEY_OC_USERNAME_EQUALS = "oc_username=";\r
+    // TODO Remove it\r
+    //private static final String KEY_OC_USERNAME_EQUALS = "oc_username="; \r
 \r
     private static final String AUTH_ON = "on";\r
     private static final String AUTH_OFF = "off";\r
 \r
     private static final String AUTH_ON = "on";\r
     private static final String AUTH_OFF = "off";\r
@@ -132,7 +132,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
     private final Handler mHandler = new Handler();\r
     private Thread mOperationThread;\r
     private OwnCloudServerCheckOperation mOcServerChkOperation;\r
     private final Handler mHandler = new Handler();\r
     private Thread mOperationThread;\r
     private OwnCloudServerCheckOperation mOcServerChkOperation;\r
-    private ExistenceCheckOperation mAuthCheckOperation;\r
+    private ExistenceCheckRemoteOperation mAuthCheckOperation;\r
     private RemoteOperationResult mLastSslUntrustedServerResult;\r
 \r
     private Uri mNewCapturedUriFromOAuth2Redirection;\r
     private RemoteOperationResult mLastSslUntrustedServerResult;\r
 \r
     private Uri mNewCapturedUriFromOAuth2Redirection;\r
@@ -717,7 +717,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         showDialog(DIALOG_LOGIN_PROGRESS);\r
 \r
         /// test credentials accessing the root folder\r
         showDialog(DIALOG_LOGIN_PROGRESS);\r
 \r
         /// test credentials accessing the root folder\r
-        mAuthCheckOperation = new  ExistenceCheckOperation("", this, false);\r
+        mAuthCheckOperation = new  ExistenceCheckRemoteOperation("", this, false);\r
         WebdavClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);\r
         client.setBasicCredentials(username, password);\r
         mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);\r
         WebdavClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);\r
         client.setBasicCredentials(username, password);\r
         mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);\r
@@ -766,7 +766,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);\r
 \r
         /// test credentials accessing the root folder\r
         String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);\r
 \r
         /// test credentials accessing the root folder\r
-        mAuthCheckOperation = new  ExistenceCheckOperation("", this, false);\r
+        mAuthCheckOperation = new  ExistenceCheckRemoteOperation("", this, false);\r
         WebdavClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, false);\r
         mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);\r
       \r
         WebdavClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, false);\r
         mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);\r
       \r
@@ -786,12 +786,12 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         } else if (operation instanceof OAuth2GetAccessToken) {\r
             onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result);\r
 \r
         } else if (operation instanceof OAuth2GetAccessToken) {\r
             onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result);\r
 \r
-        } else if (operation instanceof ExistenceCheckOperation)  {\r
+        } else if (operation instanceof ExistenceCheckRemoteOperation)  {\r
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
                 onSamlBasedFederatedSingleSignOnAuthorizationStart(operation, result);\r
                 \r
             } else {\r
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
                 onSamlBasedFederatedSingleSignOnAuthorizationStart(operation, result);\r
                 \r
             } else {\r
-                onAuthorizationCheckFinish((ExistenceCheckOperation)operation, result);\r
+                onAuthorizationCheckFinish((ExistenceCheckRemoteOperation)operation, result);\r
             }\r
         }\r
     }\r
             }\r
         }\r
     }\r
@@ -1085,7 +1085,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             /// time to test the retrieved access token on the ownCloud server\r
             mAuthToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);\r
             Log_OC.d(TAG, "Got ACCESS TOKEN: " + mAuthToken);\r
             /// time to test the retrieved access token on the ownCloud server\r
             mAuthToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);\r
             Log_OC.d(TAG, "Got ACCESS TOKEN: " + mAuthToken);\r
-            mAuthCheckOperation = new ExistenceCheckOperation("", this, false);\r
+            mAuthCheckOperation = new ExistenceCheckRemoteOperation("", this, false);\r
             WebdavClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);\r
             client.setBearerCredentials(mAuthToken);\r
             mAuthCheckOperation.execute(client, this, mHandler);\r
             WebdavClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);\r
             client.setBearerCredentials(mAuthToken);\r
             mAuthCheckOperation.execute(client, this, mHandler);\r
@@ -1106,7 +1106,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
      * @param operation     Access check performed.\r
      * @param result        Result of the operation.\r
      */\r
      * @param operation     Access check performed.\r
      * @param result        Result of the operation.\r
      */\r
-    private void onAuthorizationCheckFinish(ExistenceCheckOperation operation, RemoteOperationResult result) {\r
+    private void onAuthorizationCheckFinish(ExistenceCheckRemoteOperation operation, RemoteOperationResult result) {\r
         try {\r
             dismissDialog(DIALOG_LOGIN_PROGRESS);\r
         } catch (IllegalArgumentException e) {\r
         try {\r
             dismissDialog(DIALOG_LOGIN_PROGRESS);\r
         } catch (IllegalArgumentException e) {\r
@@ -1178,7 +1178,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);\r
             \r
         } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
             mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);\r
             \r
         } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
-            String username = getUserNameForSamlSso();\r
+            String username = com.owncloud.android.oc_framework.accounts.AccountUtils.getUserNameForSamlSso(mAuthToken);\r
             if (!mUsernameInput.getText().toString().equals(username)) {\r
                 // fail - not a new account, but an existing one; disallow\r
                 RemoteOperationResult result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME); \r
             if (!mUsernameInput.getText().toString().equals(username)) {\r
                 // fail - not a new account, but an existing one; disallow\r
                 RemoteOperationResult result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME); \r
@@ -1218,7 +1218,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         Uri uri = Uri.parse(mHostBaseUrl);\r
         String username = mUsernameInput.getText().toString().trim();\r
         if (isSaml) {\r
         Uri uri = Uri.parse(mHostBaseUrl);\r
         String username = mUsernameInput.getText().toString().trim();\r
         if (isSaml) {\r
-            username = getUserNameForSamlSso();\r
+            username = com.owncloud.android.oc_framework.accounts.AccountUtils.getUserNameForSamlSso(mAuthToken);\r
             \r
         } else if (isOAuth) {\r
             username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();\r
             \r
         } else if (isOAuth) {\r
             username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();\r
@@ -1280,19 +1280,19 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         }\r
     }\r
 \r
         }\r
     }\r
 \r
-    \r
-    private String getUserNameForSamlSso() {\r
-        if (mAuthToken != null) {\r
-            String [] cookies = mAuthToken.split(";");\r
-            for (int i=0; i<cookies.length; i++) {\r
-                if (cookies[i].startsWith(KEY_OC_USERNAME_EQUALS )) {\r
-                    String value = Uri.decode(cookies[i].substring(KEY_OC_USERNAME_EQUALS.length()));\r
-                    return value;\r
-                }\r
-            }\r
-        }\r
-        return "";\r
-    }\r
+// TODO Remove it\r
+//    private String getUserNameForSamlSso() {\r
+//        if (mAuthToken != null) {\r
+//            String [] cookies = mAuthToken.split(";");\r
+//            for (int i=0; i<cookies.length; i++) {\r
+//                if (cookies[i].startsWith(KEY_OC_USERNAME_EQUALS )) {\r
+//                    String value = Uri.decode(cookies[i].substring(KEY_OC_USERNAME_EQUALS.length()));\r
+//                    return value;\r
+//                }\r
+//            }\r
+//        }\r
+//        return "";\r
+//    }\r
 \r
 \r
     /**\r
 \r
 \r
     /**\r