captcha

package
v6.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: Apache-2.0 Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Captcha

This package provides services to add a captcha to your web applications. The following CAPTCHA services are supported.

  • Google ReCAPTCHA v2

Examples

Google ReCAPTCHA v2
import "github.com/app-nerds/kit/v6/captcha"

captchaService := captcha.NewGoogleRecaptchaService(captcha.GoogleRecaptchaServiceConfig{
  CaptchaSecret: "secret",
})

if verifyCaptchaResponse, err := captchaService.VerifyCaptcha(captchaTokenFromFrontEnd, ipAddress); err != nil {
   // error
}

if !verifyCaptchaResponse.Success {
  // No bueno!
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCaptchaFailed = fmt.Errorf("captcha failed")

ErrCaptchaFailed is returned when a CAPTCHA fails

Functions

This section is empty.

Types

type CaptchaService

type CaptchaService interface {
	VerifyCaptcha(token string, ip string) (VerifyCaptchaResponse, error)
}

CaptchaService describes methods for working with Google Captcha

type GoogleRecaptchaService

type GoogleRecaptchaService struct {
	CaptchaSecret string
	HttpClient    restclient.HTTPClientInterface
}

GoogleRecaptchaService provides methods for working with Google Recaptcha

func NewGoogleRecaptchaService

func NewGoogleRecaptchaService(config GoogleRecaptchaServiceConfig) *GoogleRecaptchaService

NewGoogleRecaptchaService creates a new Captcha service that uses Google Recaptcha

func (*GoogleRecaptchaService) VerifyCaptcha

func (s *GoogleRecaptchaService) VerifyCaptcha(token string, ip string) (VerifyCaptchaResponse, error)

VerifyCaptcha verifies the captcha request with the provider and returns a response

type GoogleRecaptchaServiceConfig

type GoogleRecaptchaServiceConfig struct {
	CaptchaSecret string
}

GoogleRecaptchaServiceConfig is used to configure a GoogleRecaptchaService

type VerifyCaptchaRequest

type VerifyCaptchaRequest struct {
	Secret   string `json:"secret"`
	Token    string `json:"response"`
	RemoteIP string `json:"remoteip"`
}

VerifyCaptchaRequest is used to request a Captcha verification

func (VerifyCaptchaRequest) ToJSON

func (r VerifyCaptchaRequest) ToJSON() []byte

ToJSON converts this VerifyCaptchaRequest to JSON

func (VerifyCaptchaRequest) ToQueryString

func (r VerifyCaptchaRequest) ToQueryString() []byte

ToQueryString returns a query string from this request's parameters

type VerifyCaptchaResponse

type VerifyCaptchaResponse struct {
	Success            bool      `json:"success"`
	ChallengeTimestamp time.Time `json:"challenge_ts"`
	HostName           string    `json:"hostname"`
	ErrorCodes         []string  `json:"error-codes"`
}

VerifyCaptchaResponse is the response from a Captcha verification request.

func NewVerifyCaptchaResponseFromReader

func NewVerifyCaptchaResponseFromReader(reader io.Reader) (VerifyCaptchaResponse, error)

NewVerifyCaptchaResponseFromReader creates a new VerifyCaptchaResponse struct.

Jump to

Keyboard shortcuts

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