RecaptchaService
Provides Google reCAPTCHA verification and HTML widget rendering for Ktor applications.
Includes server-side token verification via the Google reCAPTCHA API, a Ktor route-level validation extension, and kotlinx.html helpers for embedding the reCAPTCHA script and widget.
Holds a long-lived HttpClient, built by the first verification rather than when the object loads, so an application that never verifies a token (reCAPTCHA disabled, or only rendering forms) never starts one. Call close on application shutdown to release its resources. A later verification builds a new client, so an application that stops and starts again in the same JVM (Ktor auto-reload, successive testApplications) keeps verifying.
Functions
Checks config the way validateRecaptcha, loadRecaptchaScript and recaptchaWidget do, and returns whether reCAPTCHA is fully configured: enabled, with both keys present and non-blank.
Adds the Google reCAPTCHA JavaScript to the HTML HEAD if reCAPTCHA is enabled and both the site key and secret key are configured (kept in lockstep with server-side verification).
Renders the reCAPTCHA widget <div> in the HTML body if reCAPTCHA is enabled and both the site key and secret key are configured (kept in lockstep with server-side verification).
Validates the reCAPTCHA response token from form parameters within a Ktor RoutingContext.