From 21fd05af8cd9b2cdd81ca530381721f1e02a8011 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Fri, 8 Dec 2017 11:21:31 +0100 Subject: [PATCH] codacy --- .../operations/UploadFileOperation.java | 8 +- .../ui/asynctasks/DownloadKeysAsyncTask.java | 111 ------------ .../asynctasks/GenerateNewKeysAsyncTask.java | 160 ------------------ 3 files changed, 4 insertions(+), 275 deletions(-) delete mode 100644 src/main/java/com/owncloud/android/ui/asynctasks/DownloadKeysAsyncTask.java delete mode 100644 src/main/java/com/owncloud/android/ui/asynctasks/GenerateNewKeysAsyncTask.java diff --git a/src/main/java/com/owncloud/android/operations/UploadFileOperation.java b/src/main/java/com/owncloud/android/operations/UploadFileOperation.java index 1df0b1c605..f4a540201d 100644 --- a/src/main/java/com/owncloud/android/operations/UploadFileOperation.java +++ b/src/main/java/com/owncloud/android/operations/UploadFileOperation.java @@ -1013,17 +1013,17 @@ public class UploadFileOperation extends SyncOperation { case FileUploader.LOCAL_BEHAVIOUR_MOVE: String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile); - expectedFile = new File(expectedPath); + File newFile = new File(expectedPath); try { - move(originalFile, expectedFile); + move(originalFile, newFile); } catch (IOException e) { e.printStackTrace(); } getStorageManager().deleteFileInMediaScan(originalFile.getAbsolutePath()); - mFile.setStoragePath(expectedFile.getAbsolutePath()); + mFile.setStoragePath(newFile.getAbsolutePath()); saveUploadedFile(client); - FileDataStorageManager.triggerMediaScan(expectedFile.getAbsolutePath()); + FileDataStorageManager.triggerMediaScan(newFile.getAbsolutePath()); break; } } diff --git a/src/main/java/com/owncloud/android/ui/asynctasks/DownloadKeysAsyncTask.java b/src/main/java/com/owncloud/android/ui/asynctasks/DownloadKeysAsyncTask.java deleted file mode 100644 index 7fd4b80f90..0000000000 --- a/src/main/java/com/owncloud/android/ui/asynctasks/DownloadKeysAsyncTask.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Nextcloud Android client application - * - * @author Tobias Kaminsky - * Copyright (C) 2017 Tobias Kaminsky - * Copyright (C) 2017 Nextcloud GmbH. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * at your option) any later version. - * - * 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package com.owncloud.android.ui.asynctasks; - -import android.accounts.Account; -import android.content.ContentResolver; -import android.content.res.Resources; -import android.os.AsyncTask; - -import com.owncloud.android.datamodel.ArbitraryDataProvider; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.lib.resources.users.GetPrivateKeyOperation; -import com.owncloud.android.lib.resources.users.GetPublicKeyOperation; -import com.owncloud.android.utils.EncryptionUtils; - -/** - * Async task for downloading e2e keys - */ - -public class DownloadKeysAsyncTask extends AsyncTask { - private final static String TAG = DownloadKeysAsyncTask.class.getSimpleName(); - - private OwnCloudClient client; - private Account account; - private Resources resources; - private String packageName; - private ContentResolver contentResolver; - - public DownloadKeysAsyncTask(OwnCloudClient client, Account account, Resources resources, String packageName, - ContentResolver contentResolver) { - this.client = client; - this.account = account; - this.resources = resources; - this.packageName = packageName; - this.contentResolver = contentResolver; - } - - @Override - protected String doInBackground(Void... voids) { - // fetch private/public key - // if available - // - store public key - // - decrypt private key, store unencrypted private key in database - - GetPublicKeyOperation publicKeyOperation = new GetPublicKeyOperation(); - RemoteOperationResult publicKeyResult = publicKeyOperation.execute(client); - - if (publicKeyResult.isSuccess()) { - Log_OC.d(TAG, "public key successful downloaded for " + account.name); - - String publicKeyFromServer = (String) publicKeyResult.getData().get(0); - ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(contentResolver); - arbitraryDataProvider.storeOrUpdateKeyValue(account.name, EncryptionUtils.PUBLIC_KEY, publicKeyFromServer); - } else { - return null; - } - - GetPrivateKeyOperation privateKeyOperation = new GetPrivateKeyOperation(); - RemoteOperationResult privateKeyResult = privateKeyOperation.execute(client); - - if (privateKeyResult.isSuccess()) { - Log_OC.d(TAG, "private key successful downloaded for " + account.name); - -// keyResult = KEY_EXISTING_USED; - return (String) privateKeyResult.getData().get(0); - } else { - return null; - } - } - - @Override - protected void onPostExecute(String privateKey) { - super.onPostExecute(privateKey); - - if (privateKey == null) { - // no public/private key available, generate new - GenerateNewKeysAsyncTask newKeysTask = new GenerateNewKeysAsyncTask(client, account, resources, packageName); - - newKeysTask.execute(); - - } else if (!privateKey.isEmpty()) { -// textView.setText(R.string.end_to_end_encryption_enter_password); -// passwordLayout.setVisibility(View.VISIBLE); -// positiveButton.setVisibility(View.VISIBLE); - } else { - Log_OC.e(TAG, "Got empty private key string"); - } - } -} - diff --git a/src/main/java/com/owncloud/android/ui/asynctasks/GenerateNewKeysAsyncTask.java b/src/main/java/com/owncloud/android/ui/asynctasks/GenerateNewKeysAsyncTask.java deleted file mode 100644 index 539c9ed401..0000000000 --- a/src/main/java/com/owncloud/android/ui/asynctasks/GenerateNewKeysAsyncTask.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Nextcloud Android client application - * - * @author Tobias Kaminsky - * Copyright (C) 2017 Tobias Kaminsky - * Copyright (C) 2017 Nextcloud GmbH. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * at your option) any later version. - * - * 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package com.owncloud.android.ui.asynctasks; - -import android.accounts.Account; -import android.content.res.Resources; -import android.os.AsyncTask; - -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.lib.resources.users.SendCSROperation; -import com.owncloud.android.lib.resources.users.StorePrivateKeyOperation; -import com.owncloud.android.utils.CsrHelper; -import com.owncloud.android.utils.EncryptionUtils; - -import java.security.KeyPair; -import java.security.PrivateKey; -import java.util.ArrayList; - -/** - * Async task for generating new e2e keys - */ - -public class GenerateNewKeysAsyncTask extends AsyncTask { - private static final String TAG = GenerateNewKeysAsyncTask.class.getSimpleName(); - private ArrayList keyWords; - - private OwnCloudClient client; - private Account account; - private Resources resources; - private String packageName; - - public GenerateNewKeysAsyncTask(OwnCloudClient client, Account account, Resources resources, String packageName) { - this.client = client; - this.account = account; - this.resources = resources; - this.packageName = packageName; - } - - @Override - protected void onPreExecute() { - super.onPreExecute(); - -// textView.setText(R.string.end_to_end_encryption_generating_keys); - } - - @Override - protected String doInBackground(Void... voids) { - // - create CSR, push to server, store returned public key in database - // - encrypt private key, push key to server, store unencrypted private key in database - - try { - String publicKey; - - // Create public/private key pair - KeyPair keyPair = EncryptionUtils.generateKeyPair(); - PrivateKey privateKey = keyPair.getPrivate(); - - // create CSR - String urlEncoded = CsrHelper.generateCsrPemEncodedString(keyPair, account.name); - - SendCSROperation operation = new SendCSROperation(urlEncoded); - RemoteOperationResult result = operation.execute(client); - - if (result.isSuccess()) { - Log_OC.d(TAG, "public key success"); - - publicKey = (String) result.getData().get(0); - } else { -// keyResult = KEY_FAILED; - return ""; - } - - keyWords = EncryptionUtils.getRandomWords(12, null); - - StringBuilder stringBuilder = new StringBuilder(); - for (String string : keyWords) { - stringBuilder.append(string); - } - String keyPhrase = stringBuilder.toString(); - - String privateKeyString = EncryptionUtils.encodeBytesToBase64String(privateKey.getEncoded()); - String privatePemKeyString = EncryptionUtils.privateKeyToPEM(privateKey); - String encryptedPrivateKey = EncryptionUtils.encryptPrivateKey(privatePemKeyString, keyPhrase); - - // upload encryptedPrivateKey - StorePrivateKeyOperation storePrivateKeyOperation = new StorePrivateKeyOperation(encryptedPrivateKey); - RemoteOperationResult storePrivateKeyResult = storePrivateKeyOperation.execute(client); - - if (storePrivateKeyResult.isSuccess()) { - Log_OC.d(TAG, "private key success"); - -// arbitraryDataProvider.storeOrUpdateKeyValue(account.name, EncryptionUtils.PRIVATE_KEY, -// privateKeyString); -// arbitraryDataProvider.storeOrUpdateKeyValue(account.name, EncryptionUtils.PUBLIC_KEY, publicKey); - -// keyResult = KEY_CREATED; - return (String) storePrivateKeyResult.getData().get(0); - } - - } catch (Exception e) { - Log_OC.e(TAG, e.getMessage()); - e.printStackTrace(); - } - -// keyResult = KEY_FAILED; - return ""; - } - - @Override - protected void onPostExecute(String s) { - super.onPostExecute(s); - -// if (s.isEmpty()) { -// keyResult = KEY_FAILED; -// -// getDialog().setTitle(R.string.common_error); -// textView.setText(R.string.end_to_end_encryption_unsuccessful); -// positiveButton.setText(R.string.end_to_end_encryption_dialog_close); -// positiveButton.setVisibility(View.VISIBLE); -// } else { -// getDialog().setTitle(R.string.end_to_end_encryption_passphrase_title); -// -// textView.setText(R.string.end_to_end_encryption_keywords_description); -// -// StringBuilder stringBuilder = new StringBuilder(); -// -// for (String string: keyWords) { -// stringBuilder.append(string).append(" "); -// } -// String keys = stringBuilder.toString(); -// -// passphraseTextView.setText(keys); -// -// passphraseTextView.setVisibility(View.VISIBLE); -// positiveButton.setText(R.string.end_to_end_encryption_confirm_button); -// positiveButton.setVisibility(View.VISIBLE); -// } - } -} \ No newline at end of file