Fix topic routing

This commit is contained in:
Dominic Grimm 2024-02-27 17:50:25 +01:00
parent 2beeb3932f
commit 8e10632ffd
5 changed files with 6 additions and 20 deletions

View file

@ -34,7 +34,7 @@ lazy val root = project
.in(file("."))
.settings(
name := "keycloak-event-listener-mqtt",
version := "0.1.0",
version := "0.2.0",
scalaVersion := scala3Version,
resolvers += "Akka library repository".at("https://repo.akka.io/maven"),
libraryDependencies ++= keycloakDeps,

View file

@ -70,11 +70,10 @@ class MqttEventListenerProvider(
import concurrent.ExecutionContext.Implicits.global
import MqttEventListenerProviderFactory.system
val topic: String = s"${mqttOptions.topic}/${payload.topic}"
val payloadBytes: Array[Byte] = Json.encode(payload).toByteArray
val msg: MqttMessage =
MqttMessage(topic, ByteString.fromArray(payloadBytes))
MqttMessage(mqttOptions.topic, ByteString.fromArray(payloadBytes))
.withRetained(mqttOptions.retained)
val future: Future[Done] = Source.single(msg).runWith(mqttSink)

View file

@ -40,7 +40,7 @@ object MqttOptions:
end fromConfig
end MqttOptions
private final case class MqttOptions(
final case class MqttOptions(
topic: String,
retained: Boolean,
qos: MqttQoS

View file

@ -65,7 +65,7 @@ object Payload extends FromEvent[Payload]:
end fromEvent
end Payload
private final case class Payload(
final case class Payload(
admin: Boolean,
time: Long,
realm: String,
@ -76,17 +76,4 @@ private final case class Payload(
resourcePath: Option[String],
representation: Option[String],
error: Option[String]
) derives Codec:
private def result: String = if error.isDefined then "error" else "success"
def topic: String =
if admin
then
resourceType match
case Some(rType) =>
s"admin/${realm}/${result}/${rType.toLowerCase()}/${operationType.toLowerCase()}"
case None => throw new IllegalStateException
else
s"client/${realm}/${result}/${authDetails.clientId}/${operationType.toLowerCase()}"
end topic
end Payload
) derives Codec

View file

@ -48,7 +48,7 @@ object PayloadAuthDetails extends FromEvent[PayloadAuthDetails]:
end fromEvent
end PayloadAuthDetails
private final case class PayloadAuthDetails(
final case class PayloadAuthDetails(
realmId: String,
clientId: String,
userId: String,