summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crypto-common/src/main/kotlin/app/passwordstore/crypto/KeyManager.kt4
-rw-r--r--crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt7
-rw-r--r--crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt12
3 files changed, 11 insertions, 12 deletions
diff --git a/crypto-common/src/main/kotlin/app/passwordstore/crypto/KeyManager.kt b/crypto-common/src/main/kotlin/app/passwordstore/crypto/KeyManager.kt
index 31e1710d..cb3df75e 100644
--- a/crypto-common/src/main/kotlin/app/passwordstore/crypto/KeyManager.kt
+++ b/crypto-common/src/main/kotlin/app/passwordstore/crypto/KeyManager.kt
@@ -20,8 +20,8 @@ public interface KeyManager<Key, KeyIdentifier> {
*/
public suspend fun addKey(key: Key, replace: Boolean = false): Result<Key, Throwable>
- /** Removes [key] from the store. */
- public suspend fun removeKey(key: Key): Result<Key, Throwable>
+ /** Finds a key for [identifier] in the store and deletes it. */
+ 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
diff --git a/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt b/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt
index b834164d..be2ec474 100644
--- a/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt
+++ b/crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt
@@ -17,6 +17,7 @@ import app.passwordstore.crypto.errors.KeyNotFoundException
import app.passwordstore.crypto.errors.NoKeysAvailableException
import app.passwordstore.util.coroutines.runSuspendCatching
import com.github.michaelbull.result.Result
+import com.github.michaelbull.result.unwrap
import java.io.File
import javax.inject.Inject
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) {
runSuspendCatching {
if (!keyDirExists()) throw KeyDirectoryUnavailableException
- if (tryParseKeyring(key) == null) throw InvalidKeyException
+ val key = getKeyById(identifier).unwrap()
val keyFile = File(keyDir, "${tryGetId(key)}.$KEY_EXTENSION")
if (keyFile.exists()) {
if (!keyFile.delete()) throw KeyDeletionFailedException
}
-
- key
}
}
diff --git a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt
index 5c2ee7ef..b491206b 100644
--- a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt
+++ b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt
@@ -2,6 +2,7 @@ package app.passwordstore.crypto
import app.passwordstore.crypto.GpgIdentifier.KeyId
import app.passwordstore.crypto.GpgIdentifier.UserId
+import app.passwordstore.crypto.KeyUtils.tryGetId
import app.passwordstore.crypto.errors.KeyAlreadyExistsException
import app.passwordstore.crypto.errors.KeyNotFoundException
import app.passwordstore.crypto.errors.NoKeysAvailableException
@@ -93,13 +94,12 @@ class PGPKeyManagerTest {
// Add key using KeyManager
keyManager.addKey(secretKey).unwrap()
- // Check if the key id returned is correct
- val keyId = keyManager.getKeyId(keyManager.removeKey(secretKey).unwrap())
- assertEquals(KeyId(CryptoConstants.KEY_ID), keyId)
+ // Remove key
+ keyManager.removeKey(tryGetId(secretKey)!!).unwrap()
- // Check if the keys directory have 0 files
- val keysDir = File(filesDir, PGPKeyManager.KEY_DIR_NAME)
- assertEquals(0, keysDir.list()?.size)
+ // Check that no keys remain
+ val keys = keyManager.getAllKeys().unwrap()
+ assertEquals(0, keys.size)
}
@Test