chargepoint-assessment/src/main/kotlin/net/thermetics/chargepoint/authorization/KafkaAuthorizationConsumer.kt

31 lines
1.4 KiB
Kotlin

package net.thermetics.chargepoint.authorization
import net.thermetics.chargepoint.config.KAFKA_AUTH_REPLIES
import net.thermetics.chargepoint.config.KAFKA_AUTH_REQUESTS
import net.thermetics.chargepoint.models.AuthStatus
import net.thermetics.chargepoint.models.KafkaAuthRequest
import net.thermetics.chargepoint.models.KafkaAuthResponse
import org.springframework.kafka.annotation.KafkaListener
import org.springframework.messaging.handler.annotation.SendTo
/**
* The [KafkaAuthorizationConsumer] would be a separate application in the real system,
* connected to the web frontend API over Kafka. In this demonstration application, it runs
* in the same process as the web API by default, but all communication is done via Kafka,
* and it thus can run as a separate application as well.
*/
class KafkaAuthorizationConsumer {
@KafkaListener(id = "transaction-auth-consumer", topics = [KAFKA_AUTH_REQUESTS])
@SendTo(KAFKA_AUTH_REPLIES)
fun authenticate(message: KafkaAuthRequest): KafkaAuthResponse {
println("message received over kafka: $message")
println("Sending back to ${message.identifier} over kafka reply template")
return when (Whitelist.AllowedCards[message.identifier]) {
true -> KafkaAuthResponse(AuthStatus.Accepted)
false -> KafkaAuthResponse(AuthStatus.Rejected)
null -> KafkaAuthResponse(AuthStatus.Unknown)
}
}
}