diff --git a/README.md b/README.md index b1d81b0..9761358 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # reCAPTCHA -reCAPTCHA support for Nim, supporting rendering a capctcha and verifying a user's response. +reCAPTCHA support for Nim, supporting rendering a capctcha and verifying a user's response. This library supports reCaptcha from: +* https://www.google.com/recaptcha/admin +* https://recaptcha.net +* https://hcaptcha.com ## Installation @@ -10,14 +13,18 @@ nimble install recaptcha ## Usage -Before using this modul, be sure to [register your site with Google](https://www.google.com/recaptcha/admin) in order to get your client secret and site key. These are required to use this module. +Before using this modul, be sure to register at the reCaptcha providers website in order to get your client secret and site key. These are required to use this module. Register at: +* [Google reCaptcha](https://www.google.com/recaptcha/admin) +* [hCaptcha](https://hcaptcha.com) Once you have your client secret and site key, you should create an instance of `ReCaptcha`: ```nim import recaptcha -let captcha = initReCaptcha(MY_SECRET_KEY, MY_SITE_KEY) +let captcha = initReCaptcha("MY_SECRET_KEY", "MY_SITE_KEY", Google) +#let captcha = initReCaptcha("MY_SECRET_KEY", "MY_SITE_KEY", RecaptchaNet) <-- using recaptcha.net +#let captcha = initReCaptcha("MY_SECRET_KEY", "MY_SITE_KEY", Hcaptcha) <-- using hcaptcha.com ``` You can print out the required HTML code to show the reCAPTCHA element on the page using the `render` method: @@ -25,7 +32,7 @@ You can print out the required HTML code to show the reCAPTCHA element on the pa ```nim import recaptcha -let captcha = initReCaptcha(MY_SECRET_KEY, MY_SITE_KEY) +let captcha = initReCaptcha("MY_SECRET_KEY", "MY_SITE_KEY", Google) echo captcha.render() ``` @@ -37,7 +44,7 @@ Once the user has submitted the captcha back to you, you should verify their res ```nim import recaptcha, asyncdispatch -let captcha = initReCaptcha(MY_SECRET_KEY, MY_SITE_KEY) +let captcha = initReCaptcha("MY_SECRET_KEY", "MY_SITE_KEY", Google) let response = await captcha.verify(THEIR_RESPONSE) ``` @@ -46,14 +53,49 @@ If the user is a valid user, `response` will be `true`. Otherwise, it will be `f 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 `
+""" + +let + secretKey = "123456789" + siteKey = "987654321" + captcha = initReCaptcha(secretKey, siteKey, Google) + +routes: + get "/login": + resp(htmlForm.format(siteKey)) + + post "/verify": + let checkCap = await captcha.verify(@"g-recaptcha-response") + if checkCap: + resp("Welcome") + else: + resp("You failed the captcha") +``` diff --git a/src/recaptcha.nim b/src/recaptcha.nim index 75529f5..f2e6da1 100644 --- a/src/recaptcha.nim +++ b/src/recaptcha.nim @@ -2,14 +2,23 @@ import asyncdispatch, httpclient, json +type + Provider* = enum + Google + RecaptchaNet + Hcaptcha + const VerifyUrl: string = "https://www.google.com/recaptcha/api/siteverify" - VerifyUrlReplace: string = "https://recaptcha.net/recaptcha/api/siteverify" - CaptchaScript: string = r"""""" - CaptchaScriptReplace: string = r"""""" - CaptchaElementStart: string = r"""""" - NoScriptElementStart: string = r"""