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;
}
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();
}
}
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)));
}
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())));
}
Aggregations