recaptcha

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2020 License: MIT Imports: 8 Imported by: 0

README

reCAPTCHA for Go

GoDoc Go Report Card Action

Installation

go get -u github.com/sj14/recaptcha

Usage

The result will always return an error when any kind of failure occured, thus only checking the error is sufficient.

reCAPTCHA v2

See reCAPTCHA v2 docs

HTML
<html>
  <head>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  </head>
  <body>
    <form action="?" method="POST">
      <div class="g-recaptcha" data-sitekey="<YOUR_SITE_KEY>"></div>
      <br/>
      <input type="submit" value="Submit">
    </form>
  </body>
</html>
Go
result, err := recaptcha.VerifyV2(recaptchaSecret, httpRequest)
if err != nil {
    log.Fatalf("recaptcha failed: %v\n", err)
}
// result is not necessary to check, only required for more details
reCAPTCHA v3

See reCAPTCHA v3 docs

HTML
<html>
  <head>
    <script src="https://www.google.com/recaptcha/api.js?render=<YOUR_SITE_KEY>"></script>
    <script>
    grecaptcha.ready(function () {
        grecaptcha.execute('<YOUR_SITE_KEY>', { action: 'register' }).then(function (token) {
            var recaptchaResponse = document.getElementById('g-recaptcha-response');
            recaptchaResponse.value = token;
        });
    });
</script>
</head>
<body>
    <form method="POST" action="/register">
        <input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response">
        <input type="submit" value="Submit">
    </form>
</body>

Go

Without options:

result, err := recaptcha.VerifyV3(recaptchaSecret, httpRequest)
if err != nil {
    log.Fatalf("recaptcha failed: %v\n", err)
}
// result is not necessary to check, only required for more details

With options:

// The default action is empty ("") and thus not checked.
// The default minimum required score is 0.5
result, err := recaptcha.VerifyV3(recaptchaSecret, httpRequest, recaptcha.Action("register"), recaptcha.MinScore(0.7))
if err != nil {
    log.Fatalf("recaptcha failed: %v\n", err)
}
// result is not necessary to check, only required for more details

Documentation

Overview

based on https://www.socketloop.com/tutorials/golang-recaptcha-example

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoCaptcha is returned when the form value 'g-recaptcha-response' is empty
	ErrNoCaptcha = errors.New("missing recaptcha response in request")
	// ErrNoSuccess is returned when the recaptcha request was not successful.
	ErrNoSuccess = errors.New("request was not successful")
	// ErrScore is returned when the calculated score is below the required score (V3 only).
	ErrScore = errors.New("request was below the required score")
	// ErrAction is returned when the action is not the required one (V3 only).
	ErrAction = errors.New("wrong action")
)

Functions

This section is empty.

Types

type OptionV3

type OptionV3 func(*optionsV3)

OptionV3 are optional settings for reCAPTCHA V3 validations.

func Action

func Action(action string) OptionV3

Action sets the required action for a successful validation (default is empty).

func MinScore

func MinScore(min float64) OptionV3

MinScore sets the minimum required score for a successful validation (default: 0.5).

type ResponseV2

type ResponseV2 struct {
	Success     bool      `json:"success"`
	ChallengeTS time.Time `json:"challenge_ts"` // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
	Hostname    string    `json:"hostname"`     // the hostname of the site where the reCAPTCHA was solved
	ErrorCodes  []string  `json:"error-codes"`  // optional
}

ResponseV2 is a reCAPTCHA v2 response.

func VerifyV2

func VerifyV2(secret string, r *http.Request) (*ResponseV2, error)

VerifyV2 verifies a reCAPTCHA v2 request.

type ResponseV3

type ResponseV3 struct {
	Score  float64 `json:"score"`
	Action string  `json:"action"`
	ResponseV2
}

ResponseV3 is a reCAPTCHA v3 response.

func VerifyV3

func VerifyV3(secret string, r *http.Request, opts ...OptionV3) (*ResponseV3, error)

VerifyV3 verifies a reCAPTCHA v3 request.

Jump to

Keyboard shortcuts

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