Use v2 for user info, also switched to typed response

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
This commit is contained in:
tobiasKaminsky 2021-03-04 09:34:58 +01:00
parent b160958a95
commit 3fd76e1e2a
No known key found for this signature in database
GPG key ID: 0E00D4D47D0C5AF7
12 changed files with 130 additions and 77 deletions

View file

@ -32,6 +32,7 @@ import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.ocs.responses.PrivateKey;
import com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation;
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
import com.owncloud.android.lib.resources.status.OCCapability;
@ -56,7 +57,6 @@ import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@ -504,14 +504,14 @@ public class EndToEndRandomIT extends AbstractOnServerIT {
EncryptionUtils.PUBLIC_KEY,
publicKeyFromServer);
GetPrivateKeyOperation privateKeyOperation = new GetPrivateKeyOperation();
RemoteOperationResult privateKeyResult = privateKeyOperation.execute(account, targetContext);
RemoteOperationResult<PrivateKey> privateKeyResult = new GetPrivateKeyOperation().execute(account,
targetContext);
assertTrue(privateKeyResult.isSuccess());
String privateKey = (String) privateKeyResult.getData().get(0);
PrivateKey privateKey = privateKeyResult.getResultData();
String mnemonic = generateMnemonicString();
String decryptedPrivateKey = EncryptionUtils.decryptPrivateKey(privateKey, mnemonic);
String decryptedPrivateKey = EncryptionUtils.decryptPrivateKey(privateKey.getKey(), mnemonic);
arbitraryDataProvider.storeOrUpdateKeyValue(account.name,
EncryptionUtils.PRIVATE_KEY, decryptedPrivateKey);
@ -544,7 +544,7 @@ public class EndToEndRandomIT extends AbstractOnServerIT {
throw new Exception("failed to send CSR", result.getException());
}
PrivateKey privateKey = keyPair.getPrivate();
java.security.PrivateKey privateKey = keyPair.getPrivate();
String privateKeyString = EncryptionUtils.encodeBytesToBase64String(privateKey.getEncoded());
String privatePemKeyString = EncryptionUtils.privateKeyToPEM(privateKey);
String encryptedPrivateKey = EncryptionUtils.encryptPrivateKey(privatePemKeyString,

View file

@ -31,13 +31,13 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.java.util.Optional;
import com.owncloud.android.MainApp;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
import com.owncloud.android.lib.common.UserInfo;
import com.owncloud.android.lib.common.accounts.AccountUtils;
@ -361,13 +361,14 @@ public class UserAccountManagerImpl implements UserAccountManager {
// add userId
try {
OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton()
.getClientFor(ocAccount, context);
NextcloudClient nextcloudClient = OwnCloudClientManagerFactory
.getDefaultSingleton()
.getNextcloudClientFor(ocAccount, context);
RemoteOperationResult result = remoteUserNameOperation.execute(client);
RemoteOperationResult<UserInfo> result = remoteUserNameOperation.execute(nextcloudClient);
if (result.isSuccess()) {
UserInfo userInfo = (UserInfo) result.getData().get(0);
UserInfo userInfo = result.getResultData();
userId = userInfo.id;
displayName = userInfo.displayName;
} else {

View file

@ -124,7 +124,6 @@ import com.owncloud.android.utils.ErrorMessageAdapter;
import com.owncloud.android.utils.PermissionUtil;
import com.owncloud.android.utils.theme.ThemeDrawableUtils;
import com.owncloud.android.utils.theme.ThemeToolbarUtils;
import com.owncloud.android.utils.theme.ThemeUtils;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
@ -133,7 +132,6 @@ import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@ -835,7 +833,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
}
}
private void onGetUserNameFinish(RemoteOperationResult result) {
private void onGetUserNameFinish(RemoteOperationResult<UserInfo> result) {
mWaitingForOpId = Long.MAX_VALUE;
if (result.isSuccess()) {
boolean success = false;
@ -1105,7 +1103,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
* @param result Result of the operation.
*/
@Override
public void onAuthenticatorTaskCallback(RemoteOperationResult result) {
public void onAuthenticatorTaskCallback(RemoteOperationResult<UserInfo> result) {
mWaitingForOpId = Long.MAX_VALUE;
dismissWaitingDialog();
mAsyncTask = null;
@ -1230,7 +1228,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
*/
@SuppressFBWarnings("DMI")
@SuppressLint("TrulyRandom")
protected boolean createAccount(RemoteOperationResult authResult) {
protected boolean createAccount(RemoteOperationResult<UserInfo> authResult) {
String accountType = MainApp.getAccountType(this);
// create and save new ownCloud account
@ -1281,13 +1279,12 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION, mServerInfo.mVersion.getVersion());
mAccountMgr.setUserData(mAccount, Constants.KEY_OC_BASE_URL, mServerInfo.mBaseUrl);
ArrayList<Object> authResultData = authResult.getData();
if (authResultData == null || authResultData.size() == 0) {
UserInfo userInfo = authResult.getResultData();
if (userInfo == null) {
Log_OC.e(this, "Could not read user data!");
return false;
}
UserInfo userInfo = (UserInfo) authResultData.get(0);
mAccountMgr.setUserData(mAccount, Constants.KEY_DISPLAY_NAME, userInfo.getDisplayName());
mAccountMgr.setUserData(mAccount, Constants.KEY_USER_ID, userInfo.getId());
mAccountMgr.setUserData(mAccount,

View file

@ -24,9 +24,11 @@ import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import com.nextcloud.common.NextcloudClient;
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.UserInfo;
import com.owncloud.android.lib.common.network.RedirectionPath;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
@ -40,7 +42,7 @@ import static com.owncloud.android.datamodel.OCFile.ROOT_PATH;
/**
* Async Task to verify the credentials of a user
*/
public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOperationResult> {
public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOperationResult<UserInfo>> {
private static final boolean SUCCESS_IF_ABSENT = false;
@ -53,9 +55,9 @@ public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOpera
}
@Override
protected RemoteOperationResult doInBackground(Object... params) {
protected RemoteOperationResult<UserInfo> doInBackground(Object... params) {
RemoteOperationResult result;
RemoteOperationResult<UserInfo> result;
if (params != null && params.length == 2 && mWeakContext.get() != null) {
String url = (String) params[0];
Context context = mWeakContext.get();
@ -64,6 +66,11 @@ public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOpera
// Client
Uri uri = Uri.parse(url);
OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, context, true);
NextcloudClient nextcloudClient = OwnCloudClientFactory.createNextcloudClient(uri,
credentials.getUsername(),
credentials.toOkHttpCredentials(),
context,
true);
client.setCredentials(credentials);
// Operation - try credentials
@ -79,7 +86,7 @@ public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOpera
// Operation - get display name
if (result.isSuccess()) {
GetUserInfoRemoteOperation remoteUserNameOperation = new GetUserInfoRemoteOperation();
result = remoteUserNameOperation.execute(client);
result = remoteUserNameOperation.execute(nextcloudClient);
}
} else {
@ -90,13 +97,11 @@ public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOpera
}
@Override
protected void onPostExecute(RemoteOperationResult result) {
protected void onPostExecute(RemoteOperationResult<UserInfo> result) {
if (result!= null)
{
if (result != null) {
OnAuthenticatorTaskListener listener = mListener.get();
if (listener!= null)
{
if (listener != null) {
listener.onAuthenticatorTaskCallback(result);
}
}
@ -106,6 +111,6 @@ public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOpera
*/
public interface OnAuthenticatorTaskListener{
void onAuthenticatorTaskCallback(RemoteOperationResult result);
void onAuthenticatorTaskCallback(RemoteOperationResult<UserInfo> result);
}
}

View file

@ -22,8 +22,8 @@ package com.owncloud.android.operations;
import android.accounts.Account;
import android.accounts.AccountManager;
import com.nextcloud.common.NextcloudClient;
import com.owncloud.android.MainApp;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.UserInfo;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
@ -49,16 +49,15 @@ public class GetUserProfileOperation extends SyncOperation {
* Call {@link RemoteOperationResult#getData()}.get(0) to get it.
*/
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
public RemoteOperationResult<UserInfo> run(NextcloudClient client) {
// get display name
GetUserInfoRemoteOperation getDisplayName = new GetUserInfoRemoteOperation();
RemoteOperationResult result = getDisplayName.execute(client);
RemoteOperationResult<UserInfo> result = new GetUserInfoRemoteOperation().execute(client);
if (result.isSuccess()) {
// store display name with account data
AccountManager accountManager = AccountManager.get(MainApp.getAppContext());
UserInfo userInfo = (UserInfo) result.getData().get(0);
UserInfo userInfo = result.getResultData();
Account storedAccount = getStorageManager().getAccount();
accountManager.setUserData(storedAccount, AccountUtils.Constants.KEY_DISPLAY_NAME, userInfo.getDisplayName());
}

View file

@ -34,6 +34,7 @@ import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.DirectEditing;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.UserInfo;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
@ -278,12 +279,18 @@ public class RefreshFolderOperation extends RemoteOperation {
}
private void updateUserProfile() {
GetUserProfileOperation update = new GetUserProfileOperation();
RemoteOperationResult result = update.execute(mStorageManager, mContext);
try {
NextcloudClient nextcloudClient = OwnCloudClientFactory.createNextcloudClient(mAccount, mContext);
RemoteOperationResult<UserInfo> result = new GetUserProfileOperation().execute(nextcloudClient,
mStorageManager);
if (!result.isSuccess()) {
Log_OC.w(TAG, "Couldn't update user profile from server");
} else {
Log_OC.i(TAG, "Got display name: " + result.getData().get(0));
Log_OC.i(TAG, "Got display name: " + result.getResultData());
}
} catch (AccountUtils.AccountNotFoundException e) {
Log_OC.e(this, "Error updating profile", e);
}
}
@ -308,10 +315,11 @@ public class RefreshFolderOperation extends RemoteOperation {
}
private void updateDirectEditing(ArbitraryDataProvider arbitraryDataProvider, String newDirectEditingEtag) {
RemoteOperationResult result = new DirectEditingObtainRemoteOperation().execute(mAccount, mContext);
RemoteOperationResult<DirectEditing> result = new DirectEditingObtainRemoteOperation().execute(mAccount,
mContext);
if (result.isSuccess()) {
DirectEditing directEditing = (DirectEditing) result.getSingleData();
DirectEditing directEditing = result.getResultData();
String json = new Gson().toJson(directEditing);
arbitraryDataProvider.storeOrUpdateKeyValue(mAccount.name, ArbitraryDataProvider.DIRECT_EDITING, json);
} else {
@ -333,10 +341,11 @@ public class RefreshFolderOperation extends RemoteOperation {
return;
}
RemoteOperationResult result = new GetPredefinedStatusesRemoteOperation().execute(client);
RemoteOperationResult<ArrayList<PredefinedStatus>> result =
new GetPredefinedStatusesRemoteOperation().execute(client);
if (result.isSuccess()) {
ArrayList<PredefinedStatus> predefinedStatuses = (ArrayList<PredefinedStatus>) result.getSingleData();
ArrayList<PredefinedStatus> predefinedStatuses = result.getResultData();
String json = new Gson().toJson(predefinedStatuses);
arbitraryDataProvider.storeOrUpdateKeyValue(mAccount.name, ArbitraryDataProvider.PREDEFINED_STATUS, json);
} else {

View file

@ -23,17 +23,19 @@ package com.owncloud.android.operations.common;
import android.content.Context;
import android.os.Handler;
import com.nextcloud.common.NextcloudClient;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import androidx.annotation.NonNull;
/**
* Operation which execution involves both interactions with an ownCloud server and
* with local data in the device.
*
* Operation which execution involves both interactions with an ownCloud server and with local data in the device.
* <p>
* Provides methods to execute the operation both synchronously or asynchronously.
*/
public abstract class SyncOperation extends RemoteOperation {
@ -85,6 +87,15 @@ public abstract class SyncOperation extends RemoteOperation {
return super.execute(client);
}
public RemoteOperationResult execute(@NonNull NextcloudClient client, FileDataStorageManager storageManager) {
if (storageManager == null) {
throw new IllegalArgumentException("Trying to execute a sync operation with a NULL storage manager");
}
this.storageManager = storageManager;
return run(client);
}
/**
* Asynchronously executes the remote operation

View file

@ -27,6 +27,8 @@
package com.owncloud.android.ui.activity;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
@ -55,6 +57,7 @@ import com.nextcloud.client.di.Injectable;
import com.nextcloud.client.network.ClientFactory;
import com.nextcloud.client.onboarding.FirstRunActivity;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.java.util.Optional;
import com.nextcloud.ui.ChooseAccountDialogFragment;
import com.owncloud.android.MainApp;
@ -66,6 +69,7 @@ import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.ExternalLink;
import com.owncloud.android.lib.common.ExternalLinkType;
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
import com.owncloud.android.lib.common.Quota;
import com.owncloud.android.lib.common.UserInfo;
import com.owncloud.android.lib.common.accounts.ExternalLinksOperation;
@ -99,6 +103,7 @@ import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.parceler.Parcels;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@ -731,10 +736,24 @@ public abstract class DrawerActivity extends ToolbarActivity
}
final Context context = MainApp.getAppContext();
RemoteOperationResult result = new GetUserInfoRemoteOperation().execute(user.toPlatformAccount(), context);
NextcloudClient nextcloudClient = null;
try {
nextcloudClient = OwnCloudClientManagerFactory
.getDefaultSingleton()
.getNextcloudClientFor(user.toOwnCloudAccount(),
context);
} catch (OperationCanceledException | AuthenticatorException | IOException e) {
Log_OC.e(this, "Error retrieving user quota", e);
}
if (result.isSuccess() && result.getData() != null) {
final UserInfo userInfo = (UserInfo) result.getData().get(0);
if (nextcloudClient == null) {
return;
}
RemoteOperationResult<UserInfo> result = new GetUserInfoRemoteOperation().execute(nextcloudClient);
if (result.isSuccess() && result.getResultData() != null) {
final UserInfo userInfo = result.getResultData();
final Quota quota = userInfo.getQuota();
if (quota != null) {

View file

@ -48,10 +48,12 @@ import com.bumptech.glide.request.target.SimpleTarget;
import com.nextcloud.client.account.User;
import com.nextcloud.client.di.Injectable;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.common.NextcloudClient;
import com.owncloud.android.R;
import com.owncloud.android.databinding.UserInfoLayoutBinding;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.UserInfo;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation;
@ -318,12 +320,21 @@ public class UserInfoActivity extends DrawerActivity implements Injectable {
private void fetchAndSetData() {
Thread t = new Thread(() -> {
RemoteOperation getRemoteUserInfoOperation = new GetUserInfoRemoteOperation();
RemoteOperationResult result = getRemoteUserInfoOperation.execute(user.toPlatformAccount(), this);
NextcloudClient nextcloudClient;
try {
nextcloudClient = OwnCloudClientFactory.createNextcloudClient(user.toPlatformAccount(),
this);
} catch (AccountUtils.AccountNotFoundException e) {
Log_OC.e(this, "Error retrieving user info", e);
return;
}
RemoteOperationResult<UserInfo> result = new GetUserInfoRemoteOperation().execute(nextcloudClient);
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
if (result.isSuccess() && result.getData() != null) {
userInfo = (UserInfo) result.getData().get(0);
if (result.isSuccess() && result.getResultData() != null) {
userInfo = result.getResultData();
runOnUiThread(() -> populateUserInfoUi(userInfo));
} else {

View file

@ -53,10 +53,11 @@ public class RetrieveStatusAsyncTask extends AsyncTask<Void, Void, Status> {
protected com.owncloud.android.lib.resources.users.Status doInBackground(Void... voids) {
try {
NextcloudClient client = clientFactory.createNextcloudClient(user);
RemoteOperationResult result = new GetStatusRemoteOperation().execute(client);
RemoteOperationResult<com.owncloud.android.lib.resources.users.Status> result =
new GetStatusRemoteOperation().execute(client);
if (result.isSuccess()) {
return (com.owncloud.android.lib.resources.users.Status) result.getSingleData();
return result.getResultData();
} else {
return new com.owncloud.android.lib.resources.users.Status(StatusType.OFFLINE, "", "", -1);
}
@ -72,6 +73,5 @@ public class RetrieveStatusAsyncTask extends AsyncTask<Void, Void, Status> {
if (fragment != null && fragment.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
fragment.setStatus(status, fragment.requireContext());
}
}
}

View file

@ -307,7 +307,7 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements View
try {
OwnCloudClient client = clientFactory.create(user);
RemoteOperationResult result =
RemoteOperationResult<String> result =
new DirectEditingCreateFileRemoteOperation(path,
creator.getEditor(),
creator.getId(),
@ -339,7 +339,7 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements View
storageManager.saveFile(temp);
file = storageManager.getFileByPath(path);
return result.getData().get(0).toString();
return result.getResultData();
} catch (ClientFactory.CreationException e) {
Log_OC.e(TAG, "Error creating file from template!", e);
@ -392,7 +392,8 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements View
try {
OwnCloudClient client = clientFactory.create(user);
RemoteOperationResult result = new DirectEditingObtainListOfTemplatesRemoteOperation(creator.getEditor(),
RemoteOperationResult<TemplateList> result =
new DirectEditingObtainListOfTemplatesRemoteOperation(creator.getEditor(),
creator.getId())
.execute(client);
@ -400,7 +401,7 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements View
return new TemplateList();
}
return (TemplateList) result.getSingleData();
return result.getResultData();
} catch (ClientFactory.CreationException e) {
Log_OC.e(TAG, "Could not fetch template", e);

View file

@ -263,14 +263,14 @@ public class SetupEncryptionDialogFragment extends DialogFragment {
return null;
}
GetPrivateKeyOperation privateKeyOperation = new GetPrivateKeyOperation();
RemoteOperationResult privateKeyResult = privateKeyOperation.execute(user.toPlatformAccount(), getContext());
RemoteOperationResult<com.owncloud.android.lib.ocs.responses.PrivateKey> privateKeyResult =
new GetPrivateKeyOperation().execute(user.toPlatformAccount(), getContext());
if (privateKeyResult.isSuccess()) {
Log_OC.d(TAG, "private key successful downloaded for " + user.getAccountName());
keyResult = KEY_EXISTING_USED;
return (String) privateKeyResult.getData().get(0);
return privateKeyResult.getResultData().getKey();
} else {
return null;
}