validateRecaptcha

suspend fun RoutingContext.validateRecaptcha(config: RecaptchaConfig, params: Parameters): Boolean

Validates the reCAPTCHA response token from form parameters within a Ktor RoutingContext.

If reCAPTCHA is configured and the token is missing or invalid, this function responds with an HTTP 400 Bad Request and returns false. Returns true when validation succeeds or reCAPTCHA is not configured.

Cancellation is not swallowed: if the call is cancelled while Google is being contacted, for example because the client disconnected, the kotlinx.coroutines.CancellationException propagates instead of being reported as a failed verification.

Return

true if validation passed or reCAPTCHA is disabled, false otherwise.

Parameters

config

the RecaptchaConfig providing keys and enabled status.

params

the form Parameters containing the g-recaptcha-response token.

Throws

kotlinx.coroutines.CancellationException

if the surrounding coroutine is cancelled.