/* ownCloud Android client application\r
* Copyright (C) 2012 Bartek Przybylski\r
- * Copyright (C) 2012-2014 ownCloud Inc.\r
+ * Copyright (C) 2012-2015 ownCloud Inc.\r
*\r
* This program is free software: you can redistribute it and/or modify\r
* it under the terms of the GNU General Public License version 2,\r
import android.graphics.drawable.Drawable;\r
import android.net.Uri;\r
import android.net.http.SslError;\r
+import android.os.AsyncTask;\r
import android.os.Bundle;\r
import android.os.Handler;\r
import android.os.IBinder;\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.lib.common.OwnCloudAccount;\r
+import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;\r
import com.owncloud.android.lib.common.accounts.AccountTypeUtils;\r
+import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;\r
import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;\r
import com.owncloud.android.lib.common.network.CertificateCombinedException;\r
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;\r
* @author masensio\r
*/\r
public class AuthenticatorActivity extends AccountAuthenticatorActivity\r
-implements OnRemoteOperationListener, OnFocusChangeListener, OnEditorActionListener, \r
-SsoWebViewClientListener, OnSslUntrustedCertListener {\r
+ implements OnRemoteOperationListener, OnFocusChangeListener, OnEditorActionListener,\r
+ SsoWebViewClientListener, OnSslUntrustedCertListener,\r
+ AuthenticatorAsyncTask.OnAuthenticatorTaskListener {\r
\r
private static final String TAG = AuthenticatorActivity.class.getSimpleName();\r
\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
+ private static final String KEY_USERNAME = "USERNAME";\r
+ private static final String KEY_PASSWORD = "PASSWORD";\r
+ private static final String KEY_ASYNC_TASK_IN_PROGRESS = "AUTH_IN_PROGRESS";\r
\r
/// parameters from EXTRAs in starter Intent\r
private byte mAction;\r
private int mAuthStatusText = 0, mAuthStatusIcon = 0;\r
\r
private String mAuthToken = "";\r
+ private AuthenticatorAsyncTask mAsyncTask;\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
if (\r
AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(\r
MainApp.getAccountType()\r
- ).equals(mAuthTokenType) &&\r
- mHostUrlInput.hasFocus()\r
- ) {\r
+ ).equals(mAuthTokenType) &&\r
+ mHostUrlInput.hasFocus()\r
+ ) {\r
checkOcServer();\r
}\r
}\r
* intended to defer the processing of the redirection caught in \r
* {@link #onNewIntent(Intent)} until {@link #onResume()} \r
* \r
- * See {@link #loadSavedInstanceState(Bundle)}\r
+ * See {@link #onSaveInstanceState(Bundle)}\r
*/\r
@Override\r
protected void onSaveInstanceState(Bundle outState) {\r
/// authentication\r
outState.putBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG, mIsFirstAuthAttempt);\r
\r
+ /// AsyncTask (User and password)\r
+ outState.putString(KEY_USERNAME, mUsernameInput.getText().toString());\r
+ outState.putString(KEY_PASSWORD, mPasswordInput.getText().toString());\r
+\r
+ if (mAsyncTask != null) {\r
+ mAsyncTask.cancel(true);\r
+ outState.putBoolean(KEY_ASYNC_TASK_IN_PROGRESS, true);\r
+ } else {\r
+ outState.putBoolean(KEY_ASYNC_TASK_IN_PROGRESS, false);\r
+ }\r
+ mAsyncTask = null;\r
+\r
//Log_OC.wtf(TAG, "onSaveInstanceState end" );\r
}\r
\r
+ @Override\r
+ public void onRestoreInstanceState(Bundle savedInstanceState) {\r
+ super.onRestoreInstanceState(savedInstanceState);\r
+\r
+ // AsyncTask\r
+ boolean inProgress = savedInstanceState.getBoolean(KEY_ASYNC_TASK_IN_PROGRESS);\r
+ if (inProgress){\r
+ mAsyncTask = new AuthenticatorAsyncTask(this);\r
+ String username = savedInstanceState.getString(KEY_USERNAME);\r
+ String password = savedInstanceState.getString(KEY_PASSWORD);\r
+ String[] params = {mServerInfo.mBaseUrl, username, password, mAuthToken, mAuthTokenType};\r
+ mAsyncTask.execute(params);\r
+ }\r
+ }\r
\r
/**\r
* The redirection triggered by the OAuth authentication server as response to the \r
}\r
\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, "/");\r
- existenceCheckIntent.putExtra(OperationsService.EXTRA_USERNAME, username);\r
- existenceCheckIntent.putExtra(OperationsService.EXTRA_PASSWORD, password);\r
- existenceCheckIntent.putExtra(OperationsService.EXTRA_AUTH_TOKEN, mAuthToken);\r
- \r
- if (mOperationsServiceBinder != null) {\r
- //Log_OC.wtf(TAG, "starting existenceCheckRemoteOperation..." );\r
- mWaitingForOpId = mOperationsServiceBinder.queueNewOperation(existenceCheckIntent);\r
+ // delete the account if the token has changed\r
+ if (mAction == ACTION_UPDATE_TOKEN || mAction == ACTION_UPDATE_EXPIRED_TOKEN) {\r
+ // Remove the cookies in AccountManager\r
+ mAccountMgr.setUserData(mAccount, Constants.KEY_COOKIES, null);\r
}\r
+\r
+ mAsyncTask = new AuthenticatorAsyncTask(this);\r
+ String[] params = { mServerInfo.mBaseUrl, username, password, mAuthToken, mAuthTokenType};\r
+ mAsyncTask.execute(params);\r
+\r
}\r
\r
/**\r
onGetOAuthAccessTokenFinish(result);\r
\r
} else if (operation instanceof ExistenceCheckRemoteOperation) {\r
+ // TODO : remove this response??\r
//Log_OC.wtf(TAG, "received detection response through callback" );\r
if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).\r
equals(mAuthTokenType)) {\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
- /*\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
} else {\r
- updateToken();\r
- success = true;\r
+ try {\r
+ updateAccountAuthentication();\r
+ success = true;\r
+\r
+ } catch (AccountNotFoundException e) {\r
+ Log_OC.e(TAG, "Account " + mAccount + " was removed!", e);\r
+ Toast.makeText(this, R.string.auth_account_does_not_exist, Toast.LENGTH_SHORT).show();\r
+ finish();\r
+ }\r
}\r
}\r
\r
private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperationResult result) {\r
mWaitingForOpId = Long.MAX_VALUE;\r
dismissDialog(WAIT_DIALOG_TAG);\r
-
- if (result.isIdPRedirection()) {
+\r
+ if (result.isIdPRedirection()) {\r
String targetUrl = mServerInfo.mBaseUrl \r
+ AccountUtils.getWebdavPath(mServerInfo.mVersion, mAuthTokenType);\r
\r
/**\r
* Processes the result of the server check performed when the user finishes the enter of the\r
* server URL.\r
- * \r
- * @param operation Server check performed.\r
+ *\r
* @param result Result of the check.\r
*/\r
private void onGetServerInfoFinish(RemoteOperationResult result) {\r
* Processes the result of the access check performed to try the user credentials.\r
* \r
* Creates a new account through the AccountManager.\r
- * \r
- * @param operation Access check performed.\r
+ *\r
* @param result Result of the operation.\r
*/\r
private void onAuthorizationCheckFinish(RemoteOperationResult result) {\r
success = createAccount();\r
\r
} else {\r
- updateToken();\r
- success = true;\r
+ try {\r
+ updateAccountAuthentication();\r
+ success = true;\r
+\r
+ } catch (AccountNotFoundException e) {\r
+ Log_OC.e(TAG, "Account " + mAccount + " was removed!", e);\r
+ Toast.makeText(this, R.string.auth_account_does_not_exist, Toast.LENGTH_SHORT).show();\r
+ finish();\r
+ }\r
}\r
\r
if (success) {\r
finish();\r
}\r
\r
- } else if (result.isServerFail() || result.isException()) {
+ } else if (result.isServerFail() || result.isException()) {\r
/// server errors or exceptions in authorization take to requiring a new check of \r
/// the server\r
mServerIsChecked = true;\r
\r
\r
/**\r
- * Sets the proper response to get that the Account Authenticator that started this activity \r
+ * Updates the authentication token.\r
+ *\r
+ * Sets the proper response so that the AccountAuthenticator that started this activity\r
* saves a new authorization token for mAccount.\r
+ *\r
+ * Kills the session kept by OwnCloudClientManager so that a new one will created with\r
+ * the new credentials when needed.\r
*/\r
- private void updateToken() {\r
+ private void updateAccountAuthentication() throws AccountNotFoundException {\r
+ \r
+// OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(\r
+// new OwnCloudAccount(mAccount, this)\r
+// );\r
+\r
Bundle response = new Bundle();\r
response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);\r
response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type);\r
mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);\r
}\r
/// add user data to the new account; TODO probably can be done in the last parameter \r
- // addAccountExplicitly, or in KEY_USERDATA
+ // addAccountExplicitly, or in KEY_USERDATA\r
mAccountMgr.setUserData(\r
mAccount, Constants.KEY_OC_VERSION, mServerInfo.mVersion.getVersion()\r
);\r
mAccountMgr.setUserData(\r
mAccount, Constants.KEY_OC_BASE_URL, mServerInfo.mBaseUrl\r
);\r
-
+\r
if (isSaml) {\r
mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE"); \r
} else if (isOAuth) {\r
/**\r
* Updates the content and visibility state of the icon and text associated\r
* to the last check on the ownCloud server.\r
- * \r
- * @param serverStatusText Resource identifier of the text to show.\r
- * @param serverStatusIcon Resource identifier of the icon to show.\r
+ *\r
*/\r
private void showServerStatus() {\r
if (mServerStatusIcon == 0 && mServerStatusText == 0) {\r
public void doNegativeAuthenticatioDialogClick(){\r
mIsFirstAuthAttempt = true;\r
}\r
+\r
+\r
+ @Override\r
+ public void onAuthenticatorTaskCallback(RemoteOperationResult result) {\r
+ //Log_OC.wtf(TAG, "received detection response through callback" );\r
+ if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).\r
+ equals(mAuthTokenType)) {\r
+ onSamlBasedFederatedSingleSignOnAuthorizationStart(result);\r
+\r
+ } else {\r
+ onAuthorizationCheckFinish(result);\r
+ }\r
+ }\r
}\r
--- /dev/null
+/* ownCloud Android client application
+ * Copyright (C) 2012-2015 ownCloud Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+package com.owncloud.android.authentication;
+
+import android.app.Activity;
+import android.content.Context;
+import android.net.Uri;
+import android.os.AsyncTask;
+
+import com.owncloud.android.MainApp;
+import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.common.OwnCloudClientFactory;
+import com.owncloud.android.lib.common.OwnCloudCredentials;
+import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
+import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
+import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
+
+import java.lang.ref.WeakReference;
+
+
+/**
+ * Async Task to verify the credentials of a user
+ *
+ * @author masensio on 09/02/2015.
+ */
+public class AuthenticatorAsyncTask extends AsyncTask<String, Void, RemoteOperationResult> {
+
+ private static String REMOTE_PATH = "/";
+ private static boolean SUCCESS_IF_ABSENT = false;
+
+ private Context mContext;
+ private final WeakReference<OnAuthenticatorTaskListener> mListener;
+ protected Activity mActivity;
+
+ public AuthenticatorAsyncTask(Activity activity) {
+ mContext = activity.getApplicationContext();
+ mListener = new WeakReference<OnAuthenticatorTaskListener>((OnAuthenticatorTaskListener)activity);
+ mActivity = activity;
+ }
+
+ @Override
+ protected RemoteOperationResult doInBackground(String... params) {
+
+ RemoteOperationResult result;
+ if (params!= null && params.length==5) {
+ String url = params[0];
+ String username = params[1];
+ String password = params[2];
+ String authToken = params[3];
+ String authTokenType = params[4];
+
+ // Client
+ String basic = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType());
+ String oAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType());
+ String saml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType());
+
+ Uri uri = Uri.parse(url);
+ OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, false);
+ OwnCloudCredentials credentials = null;
+ if (authTokenType.equals(basic)) {
+ credentials = OwnCloudCredentialsFactory.newBasicCredentials(
+ username, password); // basic
+
+ } else if (authTokenType.equals(oAuth)) {
+ credentials = OwnCloudCredentialsFactory.newBearerCredentials(
+ authToken); // bearer token
+
+ } else if (authTokenType.equals(saml)) {
+ credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials(
+ authToken); // SAML SSO
+ }
+
+ client.setCredentials(credentials);
+
+ // Operation
+ ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(REMOTE_PATH,
+ mContext, SUCCESS_IF_ABSENT);
+ result = operation.execute(client);
+
+ } else {
+ result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
+ }
+
+ return result;
+ }
+
+ @Override
+ protected void onPostExecute(RemoteOperationResult result) {
+
+ if (result!= null)
+ {
+ OnAuthenticatorTaskListener listener = mListener.get();
+ if (listener!= null)
+ {
+ listener.onAuthenticatorTaskCallback(result);
+ }
+ }
+ }
+ /*
+ * Interface to retrieve data from recognition task
+ */
+ public interface OnAuthenticatorTaskListener{
+
+ void onAuthenticatorTaskCallback(RemoteOperationResult result);
+ }
+}