recaptcha

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2021 License: MIT Imports: 6 Imported by: 1

README

Recaptcha Serverside Validation in Golang

CI MI TC GRC

What this?

This is an implementation of server-side validation of recaptcha, implementing in Golang.

Why this?

First I found some implementations, but they don't support multiple instance. Moreover, there's no test code!!. This is why I started coding this.

How to use?

Using this script is very simple:

import "fmt"
import reecap "github.com/hiroaki-yamamoto/recaptcha"

// CheckHuman checks recaptcha token.
func CheckHuman(cliAddr string, token string) bool {
  re := recap.New("[Your secret key]") // Create a new instance
  resp, err := re.Check(cliAddr, token) // Start the validation
  if err != nil {
    fmt.Printf(err)
    return false
  }
  return resp.Success
}

Note that resp has more information not only Success field. For detail, check recaptcha.go

Contribution

Posting Issue & PR is welcome. If you post PR, it's more appreciated, but the corresponding test code of the code you changed is mandatory (In the case that you changed the doc, you don't need to write the test code. the doc is the doc, not code).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IHttpClient added in v1.1.0

type IHttpClient interface {
	PostForm(url string, data url.Values) (resp *http.Response, err error)
}

IHttpClient abstracts http client (net/http) for testing.

type IRecaptcha added in v1.1.0

type IRecaptcha interface {
	Check(remoteIP, response string) (res *Response, err error)
}

IRecaptcha abstracts recaptcha struct for testing / using.

type Recaptcha

type Recaptcha struct {
	SecKey string
	Client IHttpClient
}

Recaptcha is a structure to handle recaptcha

func New

func New(secretKey string) *Recaptcha

New returns an instance of Recaptcha.

func (*Recaptcha) Check

func (r *Recaptcha) Check(remoteIP, response string) (res *Response, err error)

Check whether the response is verified by recaptcha or not.

type Response

type Response struct {
	Success       bool      `json:"success,omitempty"`
	Score         float64   `json:"score,omitempty"`
	Action        string    `json:"action,omitempty"`
	ChallengeTime time.Time `json:"challenge_ts,omitempty"`
	HostName      string    `json:"host,omitempty"`
	Errors        []string  `json:"error-codes,omitempty"`
}

Response represents the response from the server. It includes whethere the captcha is succeeded, the score (v3), action, time of the challenge, the name of the host, and error codes.

type ResponseError

type ResponseError struct {
	Response *http.Response // Note: Response.Body is always closed in this case.
	Body     string
}

ResponseError represents the error of response from the server.

func (*ResponseError) Error

func (r *ResponseError) Error() string

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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