Preserve Cryptors of the same cloud if the cloud gets updated

If e.g. a cloud gets updated during token refresh while browsing files we authenticate, save the updated cloud, update the vault and now no longer lock other vaults from the same cloud but update them too.
This commit is contained in:
Julian Raufelder 2022-02-12 22:22:00 +01:00
parent d9801662dd
commit 017ab520a2
No known key found for this signature in database
GPG Key ID: 17EE71F6634E381D
4 changed files with 31 additions and 1 deletions

View File

@ -78,4 +78,13 @@ public class CryptoCloudContentRepositoryFactory implements CloudContentReposito
throw new IllegalStateException(format("Cryptor already registered for vault %s", vault)); throw new IllegalStateException(format("Cryptor already registered for vault %s", vault));
} }
} }
public void updateCloudInCryptor(Vault vault, Cloud cloud) {
try {
Cryptor cryptor = cryptors.get(vault).get();
cryptors.replace(vault, Vault.aCopyOf(vault).withCloud(cloud).build(), cryptor);
} catch (MissingCryptorException e) {
// no-op
}
}
} }

View File

@ -20,6 +20,8 @@ abstract class Cryptors internal constructor() {
abstract fun putIfAbsent(vault: Vault, cryptor: Cryptor): Boolean abstract fun putIfAbsent(vault: Vault, cryptor: Cryptor): Boolean
abstract fun replace(old: Vault, updated: Vault, cryptor: Cryptor)
class Delegating : Cryptors() { class Delegating : Cryptors() {
private val fallback = Default() private val fallback = Default()
@ -65,6 +67,10 @@ abstract class Cryptors internal constructor() {
return delegate().putIfAbsent(vault, cryptor) return delegate().putIfAbsent(vault, cryptor)
} }
override fun replace(old: Vault, updated: Vault, cryptor: Cryptor) {
return delegate().replace(old, updated, cryptor)
}
@Synchronized @Synchronized
private fun delegate(): Cryptors { private fun delegate(): Cryptors {
return delegate ?: fallback return delegate ?: fallback
@ -108,6 +114,11 @@ abstract class Cryptors internal constructor() {
} }
} }
override fun replace(old: Vault, updated: Vault, cryptor: Cryptor) {
cryptors.remove(old)
cryptors[updated] = cryptor
}
fun setOnChangeListener(onChangeListener: Runnable) { fun setOnChangeListener(onChangeListener: Runnable) {
this.onChangeListener = onChangeListener this.onChangeListener = onChangeListener
} }

View File

@ -68,7 +68,7 @@ class CloudRepositoryImpl implements CloudRepository {
Cloud storedCloud = mapper.fromEntity(database.store(mapper.toEntity(cloud))); Cloud storedCloud = mapper.fromEntity(database.store(mapper.toEntity(cloud)));
dispatchingCloudContentRepository.removeCloudContentRepositoryFor(storedCloud); dispatchingCloudContentRepository.updateCloudContentRepositoryFor(storedCloud);
database.clearCache(); database.clearCache();
return storedCloud; return storedCloud;

View File

@ -204,6 +204,16 @@ class DispatchingCloudContentRepository @Inject constructor(
} }
} }
fun updateCloudContentRepositoryFor(cloud: Cloud) {
val clouds = delegates.keys.iterator()
while (clouds.hasNext()) {
val current = clouds.next()
if (cloudIsDelegateOfCryptoCloud(current, cloud)) {
cryptoCloudContentRepositoryFactory.updateCloudInCryptor((current as CryptoCloud).vault, cloud)
}
}
}
private fun cloudIsDelegateOfCryptoCloud(potentialCryptoCloud: Cloud, cloud: Cloud): Boolean { private fun cloudIsDelegateOfCryptoCloud(potentialCryptoCloud: Cloud, cloud: Cloud): Boolean {
if (potentialCryptoCloud is CryptoCloud) { if (potentialCryptoCloud is CryptoCloud) {
val delegate = potentialCryptoCloud.vault.cloud val delegate = potentialCryptoCloud.vault.cloud