From 8c6e0be80329f4ae268062973acc93067e0c14a9 Mon Sep 17 00:00:00 2001 From: SuperBatata Date: Thu, 21 Nov 2024 14:50:19 +0100 Subject: [PATCH] feat: added remove key reference endpoint --- .../kotlin/id/walt/crypto/keys/aws/AWSKey.kt | 2 +- .../webwallet/service/SSIKit2WalletService.kt | 29 +++++++++++++++++++ .../walt/webwallet/service/WalletService.kt | 1 + .../web/controllers/KeyController.kt | 18 +++++++++++- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/aws/AWSKey.kt b/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/aws/AWSKey.kt index 831e15037..0ece095a0 100644 --- a/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/aws/AWSKey.kt +++ b/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/aws/AWSKey.kt @@ -287,7 +287,7 @@ class AWSKey( config = config ) - val awsKmsUrl = "kms.${config.region}.amazonaws.com" + val awsKmsUrl = "kms.${config.auth.region}.amazonaws.com" logger.debug { "Calling AWS KMS ($awsKmsUrl) - TrentService.ScheduleKeyDeletion" } diff --git a/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/service/SSIKit2WalletService.kt b/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/service/SSIKit2WalletService.kt index 63f48a4a5..717f0ba84 100644 --- a/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/service/SSIKit2WalletService.kt +++ b/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/service/SSIKit2WalletService.kt @@ -547,6 +547,35 @@ class SSIKit2WalletService( } ) + + override suspend fun removeKey(alias: String): Boolean = runCatching { + val key = KeysService.get(walletId, alias) + key?.let { + val resolvedKey = KeyManager.resolveSerializedKey(it.document) + KeysService.delete(walletId, alias) + eventUseCase.log( + action = EventType.Key.Delete, + originator = "wallet", + tenant = tenant, + accountId = accountId, + walletId = walletId, + data = eventUseCase.keyEventData( + id = alias, + algorithm = resolvedKey.keyType.name, + kmsType = EventDataNotAvailable + ) + ) + } ?: throw IllegalArgumentException("Key not found for alias: $alias") + + }.fold( + onSuccess = { true }, + onFailure = { + logger.error(it) { "Failed to delete key: ${it.message}" } + throw IllegalArgumentException("Failed to delete key: ${it.message}") + } + ) + + override fun getHistory(limit: Int, offset: Long): List = WalletOperationHistories.selectAll() .where { WalletOperationHistories.wallet eq walletId.toJavaUuid() } diff --git a/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/service/WalletService.kt b/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/service/WalletService.kt index d802bd24e..7c16025c5 100644 --- a/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/service/WalletService.kt +++ b/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/service/WalletService.kt @@ -60,6 +60,7 @@ abstract class WalletService(val tenant: String, val accountId: Uuid, val wallet abstract suspend fun getKeyMeta(alias: String): JsonObject abstract suspend fun importKey(jwkOrPem: String): String abstract suspend fun deleteKey(alias: String): Boolean + abstract suspend fun removeKey(alias: String): Boolean // History abstract fun getHistory(limit: Int = 10, offset: Long = 0): List diff --git a/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/web/controllers/KeyController.kt b/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/web/controllers/KeyController.kt index 54f4401fc..2a402d1aa 100644 --- a/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/web/controllers/KeyController.kt +++ b/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/web/controllers/KeyController.kt @@ -221,7 +221,7 @@ fun Application.keys() = walletRoute { } delete({ - summary = "Delete a specific key" + summary = "Delete a specific key (hard delete)" response { HttpStatusCode.Accepted to { description = "Key deleted" } HttpStatusCode.BadRequest to { description = "Key could not be deleted" } @@ -232,6 +232,22 @@ fun Application.keys() = walletRoute { val success = getWalletService().deleteKey(keyId) context.respond(if (success) HttpStatusCode.Accepted else HttpStatusCode.BadRequest) } + + delete("remove", { + summary = "Remove a specific key (soft delete)" + response { + HttpStatusCode.Accepted to { description = "Key removed" } + HttpStatusCode.BadRequest to { description = "Failed to remove the key" } + } + }) { + val keyId = context.parameters["keyId"] ?: return@delete context.respond( + HttpStatusCode.BadRequest, + "Key ID is missing" + ) + + val success = getWalletService().removeKey(keyId) + context.respond(if (success) HttpStatusCode.Accepted else HttpStatusCode.BadRequest) + } } } }