arcaptcha

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: MIT Imports: 6 Imported by: 0

README

GoDoc License Twitter

Arcaptcha

Arcaptcha library implementation in Golang to verify captcha.

Guide

Installation
go get -u github.com/arcaptcha/arcaptcha-go
Usage

Register on Arcaptcha, create website and get your own SiteKey and SecretKey

website := arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY")
//'arcaptcha-response' is created for each captcha
//After you put captcha widget in your website, you can get 'arcaptcha-response' from form
result, err := website.Verify("arcaptcha-response")
if err != nil {
// error in sending or receiving API request
// handle error
}
if !result.Success {
// captcha not verified
// can see result.ErrorCodes to find what's wrong
// throw specific error
}
// it's ok
Use in middleware:
package main

import (
	"net/http"

	"github.com/arcaptcha/arcaptcha-go"
)

var website *arcaptcha.Website

func main() {
	website = arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY")

	myHandler := http.HandlerFunc(handler)
	http.Handle("/", verifyCaptcha(myHandler))
	http.ListenAndServe(":8000", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
	// handle request
}

func verifyCaptcha(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		r.ParseForm()
		response := r.FormValue("arcaptcha-response")
		result, err := website.Verify(response)
		if err != nil {
			// error in sending or receiving API request
			// handle error
		}
		if !result.Success {
			// captcha not verified
			// can see result.ErrorCodes to find what's wrong
			// throw specific error
			return
		}
		// it's ok
		next.ServeHTTP(w, r)
	})
}

you can set timeout for verify request:

website := arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY")
website.SetTimeout(1*time.Second)
result, err := website.Verify("arcaptcha-response") // returns "context deadline" error if it takes more than 1 second

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type VerifyResp

type VerifyResp struct {
	Success     bool     `json:"success"`
	ChallengeTS string   `json:"challenge_ts,omitempty"`
	Hostname    string   `json:"hostname,omitempty"`
	ErrorCodes  []string `json:"error-codes,omitempty"`
}

VerifyResp represents verify API response error codes are available in https://docs.arcaptcha.ir/en/API/Verify

type Website

type Website struct {
	SiteKey   string
	SecretKey string
	// contains filtered or unexported fields
}

func NewWebsite

func NewWebsite(siteKey, secretKey string) *Website

NewWebsite creates a new Website

func (*Website) SetTimeout added in v1.2.0

func (w *Website) SetTimeout(timeout time.Duration)

func (*Website) SetVerifyUrl added in v1.1.1

func (w *Website) SetVerifyUrl(url string)

func (*Website) Verify

func (w *Website) Verify(response string) (VerifyResp, error)

Verify calls arcaptcha verify API and returns result.

if an error occurs while sending or receiving the request, returns error. server side errors are available in VerifyResp.ErrorCodes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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