Skip to content

Client and Server code to process hCaptcha challenges

Notifications You must be signed in to change notification settings

Fantom-Factory/afHcaptcha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hCaptcha v0.0.8


Written in: Fantom pod: v0.0.8 Licence: ISC

Overview

hCaptcha is a support library that aids Fantom-Factory in the development of other libraries, frameworks and applications. Though you are welcome to use it, you may find features are missing and the documentation incomplete.

Client and Server code to process hCaptcha responses.

Requires a hCaptcha account to be configured for your domain. See:

Install

Install hCaptcha with the Fantom Pod Manager ( FPM ):

C:\> fpm install afHcaptcha

Or install hCaptcha with fanr:

C:\> fanr install -r http://eggbox.fantomfactory.org/fanr/ afHcaptcha

To use in a Fantom project, add a dependency to build.fan:

depends = ["sys 1.0", ..., "afHcaptcha 0.0"]

Documentation

Full API & fandocs are available on the Eggbox - the Fantom Pod Repository.

Quick Start

Client side code:

using afHcaptcha::CaptchaClient

...

siteKey  := "..."    // siteKey = your unique hCaptcha key
enabled  := true     // enabled = false to disable hCaptcha during dev
captcha  := captchaClient(siteKey, enabled)

...

containerId := "divId"  // id of where hCaptcha is to be rendered
captchaId   := captcha.render(containerId)

...

response    := captcha.getResponse(captchaId)
if (response == null)
    Win.cur.alert("If you're a human, complete the captcha!")
else
    // set a hidden form value to send response to the Server
    doc.elemById("captchaInput").setAttr("value", response)

Then when processing form values on the server:

using afIoc::Inject
using afBedSheet::HttpRequest
using afEfanXtra::BeforeRender
using afHcaptcha::CaptchaServer
...

@Inject const HttpRequest    httpReq
@Inject const CaptchaServer  captcha

@BeforeRender
Void onBeforeRender() {
    // inject hCaptcha scripts into the page
    captcha.injectJsCaptcha()
}

Void onProcessForm() {
    // grab the hCaptcha response from the client form
    response := httpReq.body.form["captchaInput"]

    // verify it on the hCaptcha server
    success  := captcha.verifyCaptcha(response, false)
    if (success == false)
        throw Err("Captcha failed")
}