Search in sources :

Example 6 with DecryptedFolderMetadata

use of com.owncloud.android.datamodel.DecryptedFolderMetadata in project android by nextcloud.

the class RemoveRemoteEncryptedFileOperation method run.

/**
 * Performs the remove operation.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result;
    DeleteMethod delete = null;
    String token = null;
    DecryptedFolderMetadata metadata;
    String privateKey = arbitraryDataProvider.getValue(account.name, EncryptionUtils.PRIVATE_KEY);
    try {
        // Lock folder
        LockFileOperation lockFileOperation = new LockFileOperation(parentId);
        RemoteOperationResult lockFileOperationResult = lockFileOperation.execute(client, true);
        if (lockFileOperationResult.isSuccess()) {
            token = (String) lockFileOperationResult.getData().get(0);
        } else if (lockFileOperationResult.getHttpCode() == HttpStatus.SC_FORBIDDEN) {
            throw new RemoteOperationFailedException("Forbidden! Please try again later.)");
        } else {
            throw new RemoteOperationFailedException("Unknown error!");
        }
        // refresh metadata
        GetMetadataOperation getMetadataOperation = new GetMetadataOperation(parentId);
        RemoteOperationResult getMetadataOperationResult = getMetadataOperation.execute(client, true);
        if (getMetadataOperationResult.isSuccess()) {
            // decrypt metadata
            String serializedEncryptedMetadata = (String) getMetadataOperationResult.getData().get(0);
            EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.deserializeJSON(serializedEncryptedMetadata, new TypeToken<EncryptedFolderMetadata>() {
            });
            metadata = EncryptionUtils.decryptFolderMetaData(encryptedFolderMetadata, privateKey);
        } else {
            throw new RemoteOperationFailedException("No Metadata found!");
        }
        // delete file remote
        delete = new DeleteMethod(client.getWebdavUri() + WebdavUtils.encodePath(remotePath));
        int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
        // exhaust the response, although not interesting
        delete.getResponseBodyAsString();
        result = new RemoteOperationResult((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), delete);
        Log_OC.i(TAG, "Remove " + remotePath + ": " + result.getLogMessage());
        // remove file from metadata
        metadata.getFiles().remove(fileName);
        EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.encryptFolderMetadata(metadata, privateKey);
        String serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata);
        // upload metadata
        UpdateMetadataOperation storeMetadataOperation = new UpdateMetadataOperation(parentId, serializedFolderMetadata, token);
        RemoteOperationResult uploadMetadataOperationResult = storeMetadataOperation.execute(client, true);
        if (!uploadMetadataOperationResult.isSuccess()) {
            throw new RemoteOperationFailedException("Metadata not uploaded!");
        }
        // return success
        return result;
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Remove " + remotePath + ": " + result.getLogMessage(), e);
    } finally {
        if (delete != null) {
            delete.releaseConnection();
        }
        // unlock file
        if (token != null) {
            UnlockFileOperation unlockFileOperation = new UnlockFileOperation(parentId, token);
            RemoteOperationResult unlockFileOperationResult = unlockFileOperation.execute(client, true);
            if (!unlockFileOperationResult.isSuccess()) {
                Log_OC.e(TAG, "Failed to unlock " + parentId);
            }
        }
    }
    return result;
}
Also used : DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UpdateMetadataOperation(com.owncloud.android.lib.resources.files.UpdateMetadataOperation) LockFileOperation(com.owncloud.android.lib.resources.files.LockFileOperation) GetMetadataOperation(com.owncloud.android.lib.resources.files.GetMetadataOperation) UnlockFileOperation(com.owncloud.android.lib.resources.files.UnlockFileOperation) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata)

Example 7 with DecryptedFolderMetadata

use of com.owncloud.android.datamodel.DecryptedFolderMetadata in project android by nextcloud.

the class UploadFileOperation method existsFile.

private boolean existsFile(OwnCloudClient client, String remotePath, DecryptedFolderMetadata metadata, boolean encrypted) {
    if (encrypted) {
        String fileName = new File(remotePath).getName();
        for (DecryptedFolderMetadata.DecryptedFile file : metadata.getFiles().values()) {
            if (file.getEncrypted().getFilename().equalsIgnoreCase(fileName)) {
                return true;
            }
        }
        return false;
    } else {
        ExistenceCheckRemoteOperation existsOperation = new ExistenceCheckRemoteOperation(remotePath, mContext, false);
        RemoteOperationResult result = existsOperation.execute(client);
        return result.isSuccess();
    }
}
Also used : ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RandomAccessFile(java.io.RandomAccessFile) OCFile(com.owncloud.android.datamodel.OCFile) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile) File(java.io.File) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata)

Example 8 with DecryptedFolderMetadata

use of com.owncloud.android.datamodel.DecryptedFolderMetadata in project android by nextcloud.

the class EncryptionTestIT method encryptionMetadata.

/**
 * DecryptedFolderMetadata -> EncryptedFolderMetadata -> JSON -> encrypt
 * -> decrypt -> JSON -> EncryptedFolderMetadata -> DecryptedFolderMetadata
 */
@Test
public void encryptionMetadata() throws Exception {
    DecryptedFolderMetadata decryptedFolderMetadata1 = generateFolderMetadata();
    // encrypt
    EncryptedFolderMetadata encryptedFolderMetadata1 = EncryptionUtils.encryptFolderMetadata(decryptedFolderMetadata1, privateKey);
    // serialize
    String encryptedJson = EncryptionUtils.serializeJSON(encryptedFolderMetadata1);
    // de-serialize
    EncryptedFolderMetadata encryptedFolderMetadata2 = EncryptionUtils.deserializeJSON(encryptedJson, new TypeToken<EncryptedFolderMetadata>() {
    });
    // decrypt
    DecryptedFolderMetadata decryptedFolderMetadata2 = EncryptionUtils.decryptFolderMetaData(encryptedFolderMetadata2, privateKey);
    // compare
    assertTrue(compareJsonStrings(EncryptionUtils.serializeJSON(decryptedFolderMetadata1), EncryptionUtils.serializeJSON(decryptedFolderMetadata2)));
}
Also used : EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) Test(org.junit.Test)

Example 9 with DecryptedFolderMetadata

use of com.owncloud.android.datamodel.DecryptedFolderMetadata in project android by nextcloud.

the class EncryptionTestIT method cryptFileWithMetadata.

@Test
public void cryptFileWithMetadata() throws Exception {
    DecryptedFolderMetadata metadata = generateFolderMetadata();
    // n9WXAIXO2wRY4R8nXwmo
    assertTrue(cryptFile("ia7OEEEyXMoRa1QWQk8r", "78f42172166f9dc8fd1a7156b1753353", EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get("ia7OEEEyXMoRa1QWQk8r").getEncrypted().getKey()), EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get("ia7OEEEyXMoRa1QWQk8r").getInitializationVector()), EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get("ia7OEEEyXMoRa1QWQk8r").getAuthenticationTag())));
    // n9WXAIXO2wRY4R8nXwmo
    assertTrue(cryptFile("n9WXAIXO2wRY4R8nXwmo", "825143ed1f21ebb0c3b3c3f005b2f5db", EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get("n9WXAIXO2wRY4R8nXwmo").getEncrypted().getKey()), EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get("n9WXAIXO2wRY4R8nXwmo").getInitializationVector()), EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get("n9WXAIXO2wRY4R8nXwmo").getAuthenticationTag())));
}
Also used : DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) Test(org.junit.Test)

Aggregations

DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)9 EncryptedFolderMetadata (com.owncloud.android.datamodel.EncryptedFolderMetadata)5 OCFile (com.owncloud.android.datamodel.OCFile)4 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)4 HashMap (java.util.HashMap)4 RequiresApi (android.support.annotation.RequiresApi)3 RemoteFile (com.owncloud.android.lib.resources.files.RemoteFile)3 File (java.io.File)3 OnDatatransferProgressListener (com.owncloud.android.lib.common.network.OnDatatransferProgressListener)2 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)2 GetMetadataOperation (com.owncloud.android.lib.resources.files.GetMetadataOperation)2 LockFileOperation (com.owncloud.android.lib.resources.files.LockFileOperation)2 UpdateMetadataOperation (com.owncloud.android.lib.resources.files.UpdateMetadataOperation)2 FileOutputStream (java.io.FileOutputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 Map (java.util.Map)2 Test (org.junit.Test)2 SuppressLint (android.annotation.SuppressLint)1 TypeToken (com.google.gson.reflect.TypeToken)1 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)1