recaptchav3

package module
v0.0.0-...-fc69f64 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2020 License: MIT Imports: 9 Imported by: 0

README

recaptchav3

GoDoc Go Report Card CircleCI codecov

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsBelowMinScore

func IsBelowMinScore(err error) bool

IsBelowMinScore reports whether the error returned from Result.Verify is due to the reCAPTCHA score being below the minimum.

Types

type Response

type Response struct {
	// Success indicates whether the request was successful, it does not
	// indicate the user should be allowed to continue with their action.
	// See the Verify method.
	Success bool `json:"success"`
	// Score for this request (0.0 - 1.0).
	Score float64 `json:"score"`
	// Action name for this request (important to verify).
	Action string `json:"action"`
	// ChallengeTS contains the timestamp of the challenge load.
	ChallengeTS time.Time `json:"challenge_ts"`
	// Hostname of the site where the reCAPTCHA was solved.
	Hostname string `json:"hostname"`
	// ErrorCodes contains any errors with the request.
	//
	// Reference: https://developers.google.com/recaptcha/docs/verify/#error_code_reference
	//
	//  Error code                  Description
	//  missing-input-secret        The secret parameter is missing.
	//  invalid-input-secret        The secret parameter is invalid or malformed.
	//  missing-input-response      The response parameter is missing.
	//  invalid-input-response      The response parameter is invalid or malformed.
	//  bad-request                 The request is invalid or malformed.
	//  timeout-or-duplicate        The response is no longer valid: either is too old or has been used previously.
	ErrorCodes []string `json:"error-codes"`
	// contains filtered or unexported fields
}

Response represents the reCAPTCHA response from SiteVerify.

func SiteVerify

func SiteVerify(ctx context.Context, secretKey, captchaResponse, remoteIP string) Response

SiteVerify makes a request to https://www.google.com/recaptcha/api/siteverify and returns the response. Use Response.Verify to verify the response.

The remoteIP parameter is optional and may be left blank. If you use a load balancer or another web server to proxy calls to your application make sure to get the correct remote IP. In your HTTP handler, r.RemoteAddr will be the IP of the server calling you, likely that of the load balancer or web server. Instead, use X-Real-IP or the first entry in X-Forwarded-For and make sure your load balancers and web servers are configured to set these headers correctly.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/blueskysystems/recaptchav3"
)

func main() {
	var (
		ctx             = context.Background()
		secretKey       = "secret-key"
		captchaResponse = "captcha-response"
		remoteIP        = ""

		action             = "homepage"
		minScore           = 0.5
		hostnames []string = nil
	)

	response := recaptchav3.SiteVerify(ctx, secretKey, captchaResponse, remoteIP)
	if err := response.Verify(action, minScore, hostnames); err != nil {
		log.Fatal(err)
	}

	fmt.Println("OK")
}

func (Response) Verify

func (r Response) Verify(action string, minScore float64, hostnames []string) error

Verify verifies a response. The hostnames parameter is optional if "Verify the origin of reCAPTCHA solutions" is checked in https://www.google.com/recaptcha/admin under "Settings".

Jump to

Keyboard shortcuts

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