recaptcha

package module
v3.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2018 License: MIT Imports: 6 Imported by: 0

README

recaptcha-go

Build Status

Google reCAPTCHA v2 & v3 form submittion verification in golang

Usage

The API has changed form last version hence the new major version change.
Old API is still available using the package gopkg.in/ezzarghili/recaptcha-go.v2 although it does not provide all options available in this version
As always install the package in your environment by using a stable API version, see latest version in release page.

go get -u gopkg.in/ezzarghili/recaptcha-go.v3
recaptcha v2 API
import "gopkg.in/ezzarghili/recaptcha-go.v3"
func main(){
    captcha := recaptcha.NewReCAPTCHA(recaptchaSecret, recaptcha.V2, timeout) // for v2 API get your secret from https://www.google.com/recaptcha/admin 
}

Now everytime you need to verify a V2 API client with no special options request use

err := captcha.Verify(recaptchaResponse)
if err != nil {
    // do something with err (log?)
}
// proceed

For specific options use the VerifyWithOptions method
Availavle options for the v2 api are:

	Hostname       string
	ApkPackageName string
	ResponseTime   float64
	RemoteIP       string

Other v3 options are ignored and method will return nil when succeeded

err := captcha.VerifyWithOptions(recaptchaResponse, , VerifyOption{RemoteIP: "123.123.123.123"})
if err != nil {
    // do something with err (log?)
}
// proceed
recaptcha v3 API
import "github.com/ezzarghili/recaptcha-go.v3"
func main(){
    captcha := recaptcha.NewReCAPTCHA(recaptchaSecret, recaptcha.V3, timeout) // for v3 API use https://g.co/recaptcha/v3 (apperently the same admin UI at the time of writing)
}

Now everytime you need to verify a V2 API client with no special options request use

err := captcha.Verify(recaptchaResponse)
if err != nil {
    // do something with err (log?)
}
// proceed

For specific options use the VerifyWithOptions method
Availavle options for the v3 api are:

	Treshold       float32 
	Action         string  
	Hostname       string
	ApkPackageName string
	ResponseTime   float64
	RemoteIP       string
err := captcha.VerifyWithOptions(recaptchaResponse, , VerifyOption{Action: "hompage", Treshold: 0.8})
if err != nil {
    // do something with err (log?)
}
// proceed

while recaptchaResponse is the form value with name g-recaptcha-response sent back by recaptcha server and set for you in the form when user answers the challenge

Both recaptcha.Verify and recaptcha.VerifyWithOptions return a error or nil if successful

Use the error to check for issues with the secret, connection with the server, options mismatches and incorrect solution.

This version made timeout explcit to make sure users have the possiblity to set the underling http client timeout suitable for their implemetation.

Run Tests

Use the standard go means of running test. You can also check examples of usable in the tests.

go test
Issues with this library

If you have some problems with using this library, bug reports or enhancement please open an issue in the issues tracker.

License

Let's go with something permitive should we ?

MIT

Documentation

Index

Constants

View Source
const (
	// V2 recaptcha api v2
	V2 VERSION = iota
	// V3 recaptcha api v3, more details can be found here : https://developers.google.com/recaptcha/docs/v3
	V3
	DEFAULT_TRESHOLD float32 = 0.5
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ReCAPTCHA

type ReCAPTCHA struct {
	Client        netClient
	Secret        string
	ReCAPTCHALink string
	Version       VERSION
	Timeout       uint
	// contains filtered or unexported fields
}

ReCAPTCHA recpatcha holder struct, make adding mocking code simpler

func NewReCAPTCHA

func NewReCAPTCHA(ReCAPTCHASecret string, version VERSION, timeout uint) (ReCAPTCHA, error)

NewReCAPTCHA Create new ReCAPTCHA with the v2 reCAPTCHA secret optained from https://www.google.com/recaptcha/admin or https://www.google.com/recaptcha/admin

func (*ReCAPTCHA) Verify

func (r *ReCAPTCHA) Verify(challengeResponse string) error

Verify returns (true, nil) if no error the client answered the challenge correctly and have correct remoteIP

func (*ReCAPTCHA) VerifyWithOptions

func (r *ReCAPTCHA) VerifyWithOptions(challengeResponse string, options VerifyOption) error

VerifyWithOptions returns (true, nil) if no error the client answered the challenge correctly and have correct remoteIP

type VERSION

type VERSION int8

VERSION the recaptcha api version

type VerifyOption

type VerifyOption struct {
	Treshold       float32 // ignored in v2 recaptcha
	Action         string  // ignored in v2 recaptcha
	Hostname       string
	ApkPackageName string
	ResponseTime   float64
	RemoteIP       string
}

VerifyOption verification options expected for the challenge

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳