[+] Turnstile utility class

pull/14/head
Azalea 2024-02-16 23:56:12 -05:00
parent 9faabba361
commit 3d503971ae
1 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,45 @@
package icu.samnyan.aqua.net.utils
import ext.Bool
import ext.HTTP
import ext.Str
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
import io.ktor.http.*
import kotlinx.serialization.Serializable
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
import org.springframework.stereotype.Service
@Configuration
@ConfigurationProperties(prefix = "turnstile")
class TurnstileProperties {
var enable: Bool = false
lateinit var secret: Str
}
@Service
class CaptchaService(val props: TurnstileProperties) {
@Serializable
data class Outcome(val success: Boolean)
suspend fun validate(captcha: Str?, ip: Str): Boolean {
if (!props.enable) return true
if (captcha == null) return false
val outcome: Outcome = HTTP.post("https://challenges.cloudflare.com/turnstile/v0/siteverify") {
setBody(
FormDataContent(Parameters.build {
append("secret", props.secret)
append("response", captcha)
append("remoteip", ip)
})
)
}.body()
return outcome.success
}
}