diff --git a/src/main/kotlin/org/exploit/keeper/controller/external/ComposeController.kt b/src/main/kotlin/org/exploit/keeper/controller/external/ComposeController.kt new file mode 100644 index 00000000..20776ff0 --- /dev/null +++ b/src/main/kotlin/org/exploit/keeper/controller/external/ComposeController.kt @@ -0,0 +1,30 @@ +package org.exploit.keeper.controller.external + +import com.linecorp.armeria.server.ServerBuilder +import jakarta.inject.Singleton +import org.exploit.keeper.constant.common.ErrorType +import org.exploit.keeper.constant.permission.Permission +import org.exploit.keeper.controller.support.ControllerSupport +import org.exploit.keeper.exception.TKeeperException +import org.exploit.keeper.model.api.incoming.Sign +import org.exploit.keeper.service.compose.KeeperComposer +import org.exploit.keeper.util.extension.http.hasPermission +import org.exploit.keeper.util.validation.InputValidator + +@Singleton +class ComposeController( + private val composer: KeeperComposer, + private val support: ControllerSupport +): ExternalController { + override fun register(sb: ServerBuilder) = with(support) { + sb.publicPost("/v2/keeper/compose", Sign::class.java) { ctx, body -> + if (!ctx.hasPermission(Permission.keySign(body.keyId))) + throw TKeeperException(ErrorType.ACCESS_DENIED) + + if (!InputValidator.isValidKeyId(body.keyId)) + throw TKeeperException(ErrorType.INVALID_KEY_ID) + + composer.compose(body) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/exploit/keeper/model/api/outgoing/CalculatedSignature.kt b/src/main/kotlin/org/exploit/keeper/model/api/outgoing/CalculatedSignature.kt index 56e629fd..88a3668b 100644 --- a/src/main/kotlin/org/exploit/keeper/model/api/outgoing/CalculatedSignature.kt +++ b/src/main/kotlin/org/exploit/keeper/model/api/outgoing/CalculatedSignature.kt @@ -1,10 +1,11 @@ package org.exploit.keeper.model.api.outgoing import org.exploit.keeper.model.capability.HasImposters +import org.exploit.keeper.model.outcome.Outcome data class CalculatedSignature( val type: String, val signature64: String? = null, val generation: Int, override val imposters: List, -): HasImposters \ No newline at end of file +): HasImposters, Outcome \ No newline at end of file diff --git a/src/main/kotlin/org/exploit/keeper/model/outcome/Outcome.kt b/src/main/kotlin/org/exploit/keeper/model/outcome/Outcome.kt new file mode 100644 index 00000000..82ae69dd --- /dev/null +++ b/src/main/kotlin/org/exploit/keeper/model/outcome/Outcome.kt @@ -0,0 +1,3 @@ +package org.exploit.keeper.model.outcome + +interface Outcome \ No newline at end of file diff --git a/src/main/kotlin/org/exploit/keeper/service/compose/KeeperComposer.kt b/src/main/kotlin/org/exploit/keeper/service/compose/KeeperComposer.kt new file mode 100644 index 00000000..55b102fc --- /dev/null +++ b/src/main/kotlin/org/exploit/keeper/service/compose/KeeperComposer.kt @@ -0,0 +1,23 @@ +package org.exploit.keeper.service.compose + +import jakarta.inject.Singleton +import org.exploit.keeper.model.api.incoming.Sign +import org.exploit.keeper.model.outcome.Outcome +import org.exploit.keeper.service.compose.spi.Composer +import org.exploit.keeper.service.signature.registry.SigningSessionManager +import org.exploit.keeper.util.extension.equalsIgnoreCase + +@Singleton +class KeeperComposer(private val manager: SigningSessionManager, private val composers: List) { + fun compose(body: Sign): Outcome { + val signature = manager.coordinate(body) + + val command = body.command + + val composer = composers.find { + it.type.equalsIgnoreCase(body.command.type) + } ?: return signature + + return composer.compose(command, signature) + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/exploit/keeper/service/compose/spi/Composer.kt b/src/main/kotlin/org/exploit/keeper/service/compose/spi/Composer.kt new file mode 100644 index 00000000..85ab7927 --- /dev/null +++ b/src/main/kotlin/org/exploit/keeper/service/compose/spi/Composer.kt @@ -0,0 +1,11 @@ +package org.exploit.keeper.service.compose.spi + +import org.exploit.keeper.model.api.outgoing.CalculatedSignature +import org.exploit.keeper.model.command.Command +import org.exploit.keeper.model.outcome.Outcome + +interface Composer { + val type: String + + fun compose(command: Command, signature: CalculatedSignature): Outcome +} \ No newline at end of file