Rework key deletion to accept an identifier

This commit is contained in:
Harsh Shandilya 2022-07-18 17:08:49 +05:30
parent 15f2489550
commit c0f04bec77
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
3 changed files with 11 additions and 12 deletions

View file

@ -20,8 +20,8 @@ public interface KeyManager<Key, KeyIdentifier> {
*/ */
public suspend fun addKey(key: Key, replace: Boolean = false): Result<Key, Throwable> public suspend fun addKey(key: Key, replace: Boolean = false): Result<Key, Throwable>
/** Removes [key] from the store. */ /** Finds a key for [identifier] in the store and deletes it. */
public suspend fun removeKey(key: Key): Result<Key, Throwable> public suspend fun removeKey(identifier: KeyIdentifier): Result<Unit, Throwable>
/** /**
* Get a [Key] for the given [id]. The actual semantics of what [id] is are left to individual * Get a [Key] for the given [id]. The actual semantics of what [id] is are left to individual

View file

@ -17,6 +17,7 @@ import app.passwordstore.crypto.errors.KeyNotFoundException
import app.passwordstore.crypto.errors.NoKeysAvailableException import app.passwordstore.crypto.errors.NoKeysAvailableException
import app.passwordstore.util.coroutines.runSuspendCatching import app.passwordstore.util.coroutines.runSuspendCatching
import com.github.michaelbull.result.Result import com.github.michaelbull.result.Result
import com.github.michaelbull.result.unwrap
import java.io.File import java.io.File
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
@ -71,17 +72,15 @@ constructor(
} }
} }
override suspend fun removeKey(key: PGPKey): Result<PGPKey, Throwable> = override suspend fun removeKey(identifier: GpgIdentifier): Result<Unit, Throwable> =
withContext(dispatcher) { withContext(dispatcher) {
runSuspendCatching { runSuspendCatching {
if (!keyDirExists()) throw KeyDirectoryUnavailableException if (!keyDirExists()) throw KeyDirectoryUnavailableException
if (tryParseKeyring(key) == null) throw InvalidKeyException val key = getKeyById(identifier).unwrap()
val keyFile = File(keyDir, "${tryGetId(key)}.$KEY_EXTENSION") val keyFile = File(keyDir, "${tryGetId(key)}.$KEY_EXTENSION")
if (keyFile.exists()) { if (keyFile.exists()) {
if (!keyFile.delete()) throw KeyDeletionFailedException if (!keyFile.delete()) throw KeyDeletionFailedException
} }
key
} }
} }

View file

@ -2,6 +2,7 @@ package app.passwordstore.crypto
import app.passwordstore.crypto.GpgIdentifier.KeyId import app.passwordstore.crypto.GpgIdentifier.KeyId
import app.passwordstore.crypto.GpgIdentifier.UserId import app.passwordstore.crypto.GpgIdentifier.UserId
import app.passwordstore.crypto.KeyUtils.tryGetId
import app.passwordstore.crypto.errors.KeyAlreadyExistsException import app.passwordstore.crypto.errors.KeyAlreadyExistsException
import app.passwordstore.crypto.errors.KeyNotFoundException import app.passwordstore.crypto.errors.KeyNotFoundException
import app.passwordstore.crypto.errors.NoKeysAvailableException import app.passwordstore.crypto.errors.NoKeysAvailableException
@ -93,13 +94,12 @@ class PGPKeyManagerTest {
// Add key using KeyManager // Add key using KeyManager
keyManager.addKey(secretKey).unwrap() keyManager.addKey(secretKey).unwrap()
// Check if the key id returned is correct // Remove key
val keyId = keyManager.getKeyId(keyManager.removeKey(secretKey).unwrap()) keyManager.removeKey(tryGetId(secretKey)!!).unwrap()
assertEquals(KeyId(CryptoConstants.KEY_ID), keyId)
// Check if the keys directory have 0 files // Check that no keys remain
val keysDir = File(filesDir, PGPKeyManager.KEY_DIR_NAME) val keys = keyManager.getAllKeys().unwrap()
assertEquals(0, keysDir.list()?.size) assertEquals(0, keys.size)
} }
@Test @Test