aboutsummaryrefslogtreecommitdiff
path: root/crypto-pgpainless
diff options
context:
space:
mode:
Diffstat (limited to 'crypto-pgpainless')
-rw-r--r--crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt28
-rw-r--r--crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt9
2 files changed, 23 insertions, 14 deletions
diff --git a/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt b/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
index 8c54d516..3cc68ab3 100644
--- a/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
+++ b/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
@@ -10,6 +10,12 @@ import androidx.annotation.VisibleForTesting
import com.github.michaelbull.result.Result
import dev.msfjarvis.aps.crypto.KeyUtils.tryGetId
import dev.msfjarvis.aps.crypto.KeyUtils.tryParseKeyring
+import dev.msfjarvis.aps.crypto.errors.InvalidKeyException
+import dev.msfjarvis.aps.crypto.errors.KeyAlreadyExistsException
+import dev.msfjarvis.aps.crypto.errors.KeyDeletionFailedException
+import dev.msfjarvis.aps.crypto.errors.KeyDirectoryUnavailableException
+import dev.msfjarvis.aps.crypto.errors.KeyNotFoundException
+import dev.msfjarvis.aps.crypto.errors.NoKeysAvailableException
import dev.msfjarvis.aps.util.coroutines.runSuspendCatching
import java.io.File
import javax.inject.Inject
@@ -29,16 +35,16 @@ constructor(
override suspend fun addKey(key: PGPKey, replace: Boolean): Result<PGPKey, Throwable> =
withContext(dispatcher) {
runSuspendCatching {
- if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
- if (tryParseKeyring(key) == null) throw KeyManagerException.InvalidKeyException
+ if (!keyDirExists()) throw KeyDirectoryUnavailableException
+ if (tryParseKeyring(key) == null) throw InvalidKeyException
val keyFile = File(keyDir, "${tryGetId(key)}.$KEY_EXTENSION")
if (keyFile.exists()) {
// Check for replace flag first and if it is false, throw an error
if (!replace)
- throw KeyManagerException.KeyAlreadyExistsException(
+ throw KeyAlreadyExistsException(
tryGetId(key)?.toString() ?: "Failed to retrieve key ID"
)
- if (!keyFile.delete()) throw KeyManagerException.KeyDeletionFailedException
+ if (!keyFile.delete()) throw KeyDeletionFailedException
}
keyFile.writeBytes(key.contents)
@@ -50,11 +56,11 @@ constructor(
override suspend fun removeKey(key: PGPKey): Result<PGPKey, Throwable> =
withContext(dispatcher) {
runSuspendCatching {
- if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
- if (tryParseKeyring(key) == null) throw KeyManagerException.InvalidKeyException
+ if (!keyDirExists()) throw KeyDirectoryUnavailableException
+ if (tryParseKeyring(key) == null) throw InvalidKeyException
val keyFile = File(keyDir, "${tryGetId(key)}.$KEY_EXTENSION")
if (keyFile.exists()) {
- if (!keyFile.delete()) throw KeyManagerException.KeyDeletionFailedException
+ if (!keyFile.delete()) throw KeyDeletionFailedException
}
key
@@ -64,9 +70,9 @@ constructor(
override suspend fun getKeyById(id: GpgIdentifier): Result<PGPKey, Throwable> =
withContext(dispatcher) {
runSuspendCatching {
- if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
+ if (!keyDirExists()) throw KeyDirectoryUnavailableException
val keyFiles = keyDir.listFiles()
- if (keyFiles.isNullOrEmpty()) throw KeyManagerException.NoKeysAvailableException
+ if (keyFiles.isNullOrEmpty()) throw NoKeysAvailableException
val keys = keyFiles.map { file -> PGPKey(file.readBytes()) }
val matchResult =
@@ -92,14 +98,14 @@ constructor(
return@runSuspendCatching matchResult
}
- throw KeyManagerException.KeyNotFoundException("$id")
+ throw KeyNotFoundException("$id")
}
}
override suspend fun getAllKeys(): Result<List<PGPKey>, Throwable> =
withContext(dispatcher) {
runSuspendCatching {
- if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
+ if (!keyDirExists()) throw KeyDirectoryUnavailableException
val keyFiles = keyDir.listFiles()
if (keyFiles.isNullOrEmpty()) return@runSuspendCatching emptyList()
keyFiles.map { keyFile -> PGPKey(keyFile.readBytes()) }.toList()
diff --git a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt
index 650f3d89..1439d52d 100644
--- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt
+++ b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt
@@ -5,6 +5,9 @@ import com.github.michaelbull.result.unwrapError
import dev.msfjarvis.aps.crypto.GpgIdentifier.KeyId
import dev.msfjarvis.aps.crypto.GpgIdentifier.UserId
import dev.msfjarvis.aps.crypto.TestUtils.getArmoredPrivateKeyWithMultipleIdentities
+import dev.msfjarvis.aps.crypto.errors.KeyAlreadyExistsException
+import dev.msfjarvis.aps.crypto.errors.KeyNotFoundException
+import dev.msfjarvis.aps.crypto.errors.NoKeysAvailableException
import java.io.File
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
@@ -69,7 +72,7 @@ class PGPKeyManagerTest {
keyManager.addKey(key, false).unwrap()
val error = keyManager.addKey(key, false).unwrapError()
- assertIs<KeyManagerException.KeyAlreadyExistsException>(error)
+ assertIs<KeyAlreadyExistsException>(error)
}
@Test
@@ -142,7 +145,7 @@ class PGPKeyManagerTest {
// Check returned key
val error = keyManager.getKeyById(keyId).unwrapError()
- assertIs<KeyManagerException.KeyNotFoundException>(error)
+ assertIs<KeyNotFoundException>(error)
assertEquals("No key found with id: $keyId", error.message)
}
@@ -151,7 +154,7 @@ class PGPKeyManagerTest {
scope.runTest {
// Check returned key
val error = keyManager.getKeyById(KeyId(0x08edf7567183ce44)).unwrapError()
- assertIs<KeyManagerException.NoKeysAvailableException>(error)
+ assertIs<NoKeysAvailableException>(error)
assertEquals("No keys were found", error.message)
}