recaptcha.nim

reCAPTCHA support for Nim, including rendering the noCAPTCHA element and verifying a user’s response.

reCAPTCHA is a free service that protects your website from spam and abuse. reCAPTCHA uses an advanced risk analysis engine and adaptive CAPTCHAs to keep automated software from engaging in abusive activities on your site. It does this while letting your valid users pass through with ease.

reCAPTCHA offers more than just spam protection. Every time our CAPTCHAs are solved, that human effort helps digitize text, annotate images, and build machine learning datasets. This in turn helps preserve books, improve maps, and solve hard AI problems.

Installation

nimble install recaptcha

Usage

Before using this module, be sure to register your site with Google in order to get your client secret and site key. These are required to use this module.

Once you have your client secret and site key, you should create an instance of ReCaptcha:

import recaptcha

let captcha = initReCaptcha(MY_SECRET_KEY, MY_SITE_KEY)

You can print out the required HTML code to show the reCAPTCHA element on the page using the render method:

import recaptcha

let captcha = initReCaptcha(MY_SECRET_KEY, MY_SITE_KEY)

echo captcha.render()

(or you can use the $ stringify shortcut to achieve the same thing)

Once the user has submitted the captcha back to you, you should verify their response:

import recaptcha, asyncdispatch

let captcha = initReCaptcha(MY_SECRET_KEY, MY_SITE_KEY)

let response = await captcha.verify(THEIR_RESPONSE)

If the user is a valid user, response will be true. Otherwise, it will be false.

Should there be an issue with the data sent to the reCAPTCHA service, an exception of type CaptchaVerificationError will be thrown.

In most cases, captchas are used within a HTML <form> element. In this case, the user’s response will be available within the g-recaptcha-response form parameter.

You may also optionally pass the user’s IP address along to reCAPTCHA during verification as an extra check:

import recaptcha, asyncdispatch

let captcha = initReCaptcha(MY_SECRET_KEY, MY_SITE_KEY)

let response = await captcha.verify(THEIR_RESPONSE, USERS_IP_ADDRESS)