Search in sources :

Example 1 with OwnCloudCredentials

use of com.owncloud.android.lib.common.OwnCloudCredentials in project android by owncloud.

the class AuthenticatorAsyncTask method doInBackground.

@Override
protected RemoteOperationResult doInBackground(Object... params) {
    RemoteOperationResult result;
    if (params != null && params.length == 2) {
        String url = (String) params[0];
        OwnCloudCredentials credentials = (OwnCloudCredentials) params[1];
        // Client
        Uri uri = Uri.parse(url);
        OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, true);
        client.setCredentials(credentials);
        // Operation - try credentials
        ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(REMOTE_PATH, mContext, SUCCESS_IF_ABSENT);
        result = operation.execute(client);
        String targetUrlAfterPermanentRedirection = null;
        if (operation.wasRedirected()) {
            RedirectionPath redirectionPath = operation.getRedirectionPath();
            targetUrlAfterPermanentRedirection = redirectionPath.getLastPermanentLocation();
        }
        // Operation - get display name
        if (result.isSuccess()) {
            GetRemoteUserInfoOperation remoteUserNameOperation = new GetRemoteUserInfoOperation();
            if (targetUrlAfterPermanentRedirection != null) {
                // we can't assume that any subpath of the domain is correctly redirected; ugly stuff
                client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(AccountUtils.trimWebdavSuffix(targetUrlAfterPermanentRedirection)), mContext, true);
                client.setCredentials(credentials);
            }
            result = remoteUserNameOperation.execute(client);
        }
        // let the caller knows what is real URL that should be accessed for the account
        // being authenticated if the initial URL is being redirected permanently (HTTP code 301)
        result.setLastPermanentLocation(targetUrlAfterPermanentRedirection);
    } else {
        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
    }
    return result;
}
Also used : RedirectionPath(com.owncloud.android.lib.common.network.RedirectionPath) GetRemoteUserInfoOperation(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Uri(android.net.Uri) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Example 2 with OwnCloudCredentials

use of com.owncloud.android.lib.common.OwnCloudCredentials in project android by nextcloud.

the class AuthenticatorActivity method onGetOAuthAccessTokenFinish.

/**
 * Processes the result of the request for and access token send
 * to an OAuth authorization server.
 *
 * @param result Result of the operation.
 */
private void onGetOAuthAccessTokenFinish(RemoteOperationResult result) {
    mWaitingForOpId = Long.MAX_VALUE;
    dismissDialog(WAIT_DIALOG_TAG);
    if (result.isSuccess()) {
        // / be gentle with the user
        IndeterminateProgressDialog dialog = IndeterminateProgressDialog.newInstance(R.string.auth_trying_to_login, true);
        dialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
        // / time to test the retrieved access token on the ownCloud server
        @SuppressWarnings("unchecked") Map<String, String> tokens = (Map<String, String>) (result.getData().get(0));
        mAuthToken = tokens.get(OAuth2Constants.KEY_ACCESS_TOKEN);
        Log_OC.d(TAG, "Got ACCESS TOKEN: " + mAuthToken);
        // / validate token accessing to root folder / getting session
        OwnCloudCredentials credentials = OwnCloudCredentialsFactory.newBearerCredentials(mAuthToken);
        accessRootFolder(credentials);
    } else {
        updateAuthStatusIconAndText(result);
        showAuthStatus();
        Log_OC.d(TAG, "Access failed: " + result.getLogMessage());
    }
}
Also used : IndeterminateProgressDialog(com.owncloud.android.ui.dialog.IndeterminateProgressDialog) Map(java.util.Map) HashMap(java.util.HashMap) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Example 3 with OwnCloudCredentials

use of com.owncloud.android.lib.common.OwnCloudCredentials in project android by nextcloud.

the class AuthenticatorActivity method onRestoreInstanceState.

@Override
public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // AsyncTask
    boolean inProgress = savedInstanceState.getBoolean(KEY_ASYNC_TASK_IN_PROGRESS);
    if (inProgress) {
        String username = savedInstanceState.getString(KEY_USERNAME);
        String password = savedInstanceState.getString(KEY_PASSWORD);
        OwnCloudCredentials credentials = OwnCloudCredentialsFactory.newBasicCredentials(username, password);
        accessRootFolder(credentials);
    }
}
Also used : OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Example 4 with OwnCloudCredentials

use of com.owncloud.android.lib.common.OwnCloudCredentials in project android by nextcloud.

the class AuthenticatorAsyncTask method doInBackground.

@Override
protected RemoteOperationResult<UserInfo> doInBackground(Object... params) {
    RemoteOperationResult<UserInfo> result;
    if (params != null && params.length == 2 && mWeakContext.get() != null) {
        String url = (String) params[0];
        Context context = mWeakContext.get();
        OwnCloudCredentials credentials = (OwnCloudCredentials) params[1];
        // Client
        Uri uri = Uri.parse(url);
        NextcloudClient nextcloudClient = OwnCloudClientFactory.createNextcloudClient(uri, credentials.getUsername(), credentials.toOkHttpCredentials(), context, true);
        // Operation - get display name
        RemoteOperationResult<UserInfo> userInfoResult = new GetUserInfoRemoteOperation().execute(nextcloudClient);
        // Operation - try credentials
        if (userInfoResult.isSuccess()) {
            OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, context, true);
            client.setUserId(userInfoResult.getResultData().getId());
            client.setCredentials(credentials);
            ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(ROOT_PATH, SUCCESS_IF_ABSENT);
            result = operation.execute(client);
            if (operation.wasRedirected()) {
                RedirectionPath redirectionPath = operation.getRedirectionPath();
                String permanentLocation = redirectionPath.getLastPermanentLocation();
                result.setLastPermanentLocation(permanentLocation);
            }
            result.setResultData(userInfoResult.getResultData());
        } else {
            result = userInfoResult;
        }
    } else {
        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
    }
    return result;
}
Also used : Context(android.content.Context) RedirectionPath(com.owncloud.android.lib.common.network.RedirectionPath) NextcloudClient(com.nextcloud.common.NextcloudClient) GetUserInfoRemoteOperation(com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UserInfo(com.owncloud.android.lib.common.UserInfo) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Uri(android.net.Uri) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Example 5 with OwnCloudCredentials

use of com.owncloud.android.lib.common.OwnCloudCredentials in project android by nextcloud.

the class FileActivity method performCredentialsUpdate.

public void performCredentialsUpdate(Account account, Context context) {
    try {
        // / step 1 - invalidate credentials of current account
        OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
        OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(ocAccount);
        if (client != null) {
            OwnCloudCredentials credentials = client.getCredentials();
            if (credentials != null) {
                AccountManager accountManager = AccountManager.get(context);
                if (credentials.authTokenExpires()) {
                    accountManager.invalidateAuthToken(account.type, credentials.getAuthToken());
                } else {
                    accountManager.clearPassword(account);
                }
            }
        }
        // / step 2 - request credentials to user
        Intent updateAccountCredentials = new Intent(context, AuthenticatorActivity.class);
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
        updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivityForResult(updateAccountCredentials, REQUEST_CODE__UPDATE_CREDENTIALS);
    } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
        DisplayUtils.showSnackMessage(this, R.string.auth_account_does_not_exist);
    }
}
Also used : UserAccountManager(com.nextcloud.client.account.UserAccountManager) AccountManager(android.accounts.AccountManager) Intent(android.content.Intent) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Aggregations

OwnCloudCredentials (com.owncloud.android.lib.common.OwnCloudCredentials)11 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)5 AccountManager (android.accounts.AccountManager)3 Intent (android.content.Intent)3 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)3 Uri (android.net.Uri)2 RedirectionPath (com.owncloud.android.lib.common.network.RedirectionPath)2 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)2 ExistenceCheckRemoteOperation (com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation)2 IndeterminateProgressDialog (com.owncloud.android.ui.dialog.IndeterminateProgressDialog)2 LoadingDialog (com.owncloud.android.ui.dialog.LoadingDialog)2 Map (java.util.Map)2 Context (android.content.Context)1 FragmentTransaction (androidx.fragment.app.FragmentTransaction)1 UserAccountManager (com.nextcloud.client.account.UserAccountManager)1 NextcloudClient (com.nextcloud.common.NextcloudClient)1 AccountUtils (com.owncloud.android.authentication.AccountUtils)1 UserInfo (com.owncloud.android.lib.common.UserInfo)1 OwnCloudVersion (com.owncloud.android.lib.resources.status.OwnCloudVersion)1 GetRemoteUserInfoOperation (com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation)1