recaptcha

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2018 License: MIT Imports: 7 Imported by: 0

README

GoDoc Build Status Go Report Card

recaptcha

Google's reCAPTCHA Golang implementation.

Install

To get the package, execute:

go get gopkg.in/romanyx/recaptcha.v1

To import this package, add the following line to your code:

import "gopkg.in/romanyx/recaptcha.v1"

Refer to it as recaptcha.

For more details, see the API documentation.

Example

package main

import (
	"fmt"

	import "gopkg.in/romanyx/recaptcha.v1"
)

func main() {
	r := recaptcha.New("secret")
	res, err := r.Verify("response") // g-recaptcha-response parameter
	if err != nil {
		switch err {
		case recaptcha.ErrMissingInputSecret:
			fmt.Println(err)
		case recaptcha.ErrInvalidInputSecret:
			fmt.Println(err)
		case recaptcha.ErrMissingInputResponse:
			fmt.Println(err)
		case recaptcha.ErrInvalidInputResponse:
			fmt.Println(err)
		case recaptcha.ErrBadRequest:
			fmt.Println(err)
		case recaptcha.ErrUnsucceeded:
			// This triggers when response.Success is equal false.
			fmt.Println(err)
		default:
			fmt.Printf("unknown error: %s\n", err)
		}
	}

	// If err is equal to nil, then verification has been successed.
	if err == nil {
		fmt.Printf("%+v\n", res)
	}
}

Contributing

Please feel free to submit issues, fork the repository and send pull requests!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingInputSecret when the secret parameter is missing.
	ErrMissingInputSecret = errors.New("missing-input-secret")
	// ErrInvalidInputSecret when the secret parameter is invalid or malformed.
	ErrInvalidInputSecret = errors.New("invalid-input-secret")
	// ErrMissingInputResponse when the response parameter is missing.
	ErrMissingInputResponse = errors.New("missing-input-response")
	// ErrInvalidInputResponse when the response parameter is invalid or malformed.
	ErrInvalidInputResponse = errors.New("invalid-input-response")
	// ErrBadRequest when the request is invalid or malformed.
	ErrBadRequest = errors.New("bad-request")
	// ErrUnsucceeded when response success value equals to false.
	ErrUnsucceeded = errors.New("unsucceeded status")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client struct to verify captcha.

func New

func New(secret string, options ...Option) *Client

New returns initialized Client.

func (*Client) Verify

func (cli *Client) Verify(gRecaptchaResponse string) (*Response, error)

Verify verifies reCaptcha response received from frontend.

func (*Client) VerifyWithIP

func (cli *Client) VerifyWithIP(gRecaptchaResponse, remoteIP string) (*Response, error)

VerifyWithIP verifies reCaptcha response received from frontend with optional remoteip parameter.

type Option

type Option func(*Client)

Option for initializer.

func SetHTTPClient

func SetHTTPClient(httpClient *http.Client) Option

SetHTTPClient sets httpClient. Used in tests for stubing.

func SetTimeout

func SetTimeout(timeout time.Duration) Option

SetTimeout sets timeout for the http client.

type Response

type Response struct {
	Success     bool        `json:"success"`
	ChallengeTs challengeTs `json:"challenge_ts"`
	Hostname    string      `json:"hostname"`
	ErrorCodes  []string    `json:"error-codes"`
}

Response holds data from reCaptcha API response.

Jump to

Keyboard shortcuts

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