recaptcha

package
v8.4.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2017 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Client is the default `net/http#Client` instance which is used to send requests to the Google API.

Change Client only if you know what you're doing.

Functions

func GetFormHTML

func GetFormHTML(dataSiteKey string, postActionRelativePath string) string

GetFormHTML can be used on pages where empty form is enough to verify that the client is not a "robot". i.e: GetFormHTML("public_key", "/contact") will return form tag which imports the google API script, with a simple submit button where redirects to the "postActionRelativePath".

The "postActionRelativePath" MUST use the `SiteVerify` or followed by the `New()`'s context.Handler (with the secret key this time) in order to validate if the recaptcha verified.

The majority of applications will use a custom form, this function is here for ridiculous simple use cases.

Example Code:

Method: "POST" | Path: "/contact"

func postContact(ctx context.Context) {
	// [...]
	response := recaptcha.SiteFerify(ctx, recaptchaSecret)

	if response.Success {
		// [your action here, i.e sendEmail(...)]
	}

	ctx.JSON(response)
}

Method: "GET" | Path: "/contact"

func getContact(ctx context.Context) {
	// render the recaptcha form
	ctx.HTML(recaptcha.GetFormHTML(recaptchaPublic, "/contact"))
}

func New

func New(secret string) context.Handler

New accepts the google's recaptcha secret key and returns a middleware that verifies the request by sending a response to the google's API(V2-latest). Secret key can be obtained by https://www.google.com/recaptcha.

Used for communication between your site and Google. Be sure to keep it a secret.

Use `SiteVerify` to verify a request inside another handler if needed.

Types

type Response

type Response struct {
	ChallengeTS time.Time `json:"challenge_ts"`
	Hostname    string    `json:"hostname"`
	ErrorCodes  []string  `json:"error-codes"`
	Success     bool      `json:"success"`
}

Response is the google's recaptcha response as JSON.

func SiteFerify

func SiteFerify(ctx context.Context, secret string) (response Response)

SiteFerify accepts context and the secret key(https://www.google.com/recaptcha)

and returns the google's recaptcha response, if `response.Success` is true

then validation passed.

Use `New` for middleware use instead.

Jump to

Keyboard shortcuts

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