recaptcha

package module
v0.0.0-...-52e9c3e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2020 License: MIT Imports: 6 Imported by: 0

README

go-recaptcha

https://godoc.org/github.com/dpapathanasiou/go-recaptcha

About

This package handles reCaptcha (API versions 2 and 3) form submissions in Go.

Usage

Install the package in your environment:

go get github.com/dpapathanasiou/go-recaptcha

To use it within your own code, import github.com/dpapathanasiou/go-recaptcha and call:

recaptcha.Init (recaptchaPrivateKey)

once, to set the reCaptcha private key for your domain, then:

recaptcha.Confirm (clientIpAddress, recaptchaResponse)
reCAPTCHA v2

For each reCaptcha form input you need to check, using the values obtained by reading the form's POST parameters (the recaptchaResponse in the above corresponds to the value of g-recaptcha-response sent by the reCaptcha server.)

The recaptcha.Confirm() function returns either true (i.e., the captcha was completed correctly) or false, along with any errors (from the HTTP io read or the attempt to unmarshal the JSON reply).

reCAPTCHA v3

Version 3 works differently: instead of interrupting page visitors with a prompt, it runs in the background, computing a score.

This repo has been updated to handle the score and action in the response, but the usage example is still in terms of version 2.

Usage Example

Included with this repo is example.go, a simple HTTP server which creates the reCaptcha form and tests the input.

See the instructions for running the example for more details.

Donate

If you find this work useful, please consider making a donation:

Bitcoin Donate 14TM4ADKJbaGEi8Qr8dh4KfPBQmjTshkZ2

QR code

Documentation

Overview

Package recaptcha handles reCaptcha (http://www.google.com/recaptcha) form submissions

This package is designed to be called from within an HTTP server or web framework which offers reCaptcha form inputs and requires them to be evaluated for correctness

Edit the recaptchaPrivateKey constant before building and using

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Confirm

func Confirm(remoteip, response string) (result bool, err error)

Confirm is the public interface function that validates the reCAPTCHA token. It accepts the client ip address and the token returned to the client after completing the challenge. It returns a boolean value indicating whether or not the client token is authentic, meaning the challenge was answered correctly.

func Init

func Init(key string)

Init allows the webserver or code evaluating the reCAPTCHA token input to set the reCAPTCHA private key (string) value, which will be different for every domain.

Types

type RecaptchaResponse

type RecaptchaResponse struct {
	Success     bool      `json:"success"`
	Score       float64   `json:"score"`
	Action      string    `json:"action"`
	ChallengeTS time.Time `json:"challenge_ts"`
	Hostname    string    `json:"hostname"`
	ErrorCodes  []string  `json:"error-codes"`
}

func Check

func Check(remoteip, response string) (RecaptchaResponse, error)

Check will construct the request to the verification API, send it, and return the result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL