v12.2.10 Latest Latest

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

Go to latest
Published: Jan 18, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0




View Source
const Script = "https://hcaptcha.com/1/api.js"

Script is the hCaptcha's javascript source file that should be incldued in the HTML head or body.


View Source
var (
	// ResponseContextKey is the default request's context key that response of a hcaptcha request is kept.
	ResponseContextKey string = "iris.hcaptcha"
	// DefaultFailureHandler is the default HTTP handler that is fired on hcaptcha failures.
	// See `Client.FailureHandler`.
	DefaultFailureHandler = func(ctx *context.Context) {
View Source
var HTMLForm = `` /* 174-byte string literal not displayed */

HTMLForm is the default HTML form for clients. It's totally optional, use your own code for the best possible result depending on your web application. See `ParseForm` and `RenderForm` for more.


func New

func New(secret string, options ...Option) context.Handler

New accepts a hpcatcha secret key and returns a new hcaptcha HTTP Client.

Instructions at: https://docs.hcaptcha.com/.

See its `Handler` and `SiteVerify` for details. See the `WithRemoteIP` and `WithSiteKey` package-level functions too.

func ParseForm

func ParseForm(dataSiteKey, postActionRelativePath string) string

ParseForm parses the `HTMLForm` with the necessary parameters and returns its result for render.

func RenderForm

func RenderForm(ctx *context.Context, dataSiteKey, postActionRelativePath string) (int, error)

RenderForm writes the `HTMLForm` to "w" response writer. See `_examples/auth/hcaptcha/templates/register_form.html` example for a custom form instead.


type Client

type Client struct {
	// FailureHandler if specified, fired when user does not complete hcaptcha successfully.
	// Failure and error codes information are kept as `Response` type
	// at the Request's Context key of "hcaptcha".
	// Defaults to a handler that writes a status code of 429 (Too Many Requests)
	// and without additional information.
	FailureHandler context.Handler

	// Optional checks for siteverify.
	// The user's remote IP address.
	RemoteIP string
	// The sitekey form field you expect to see.
	SiteKey string
	// contains filtered or unexported fields

Client represents the hcaptcha client.

func (*Client) Handler

func (c *Client) Handler(ctx *context.Context)

Handler is the HTTP route middleware featured hcaptcha validation. It calls the `SiteVerify` method and fires the "next" when user completed the hcaptcha successfully,

otherwise it calls the Client's `FailureHandler`.

The hcaptcha's `Response` (which contains any `ErrorCodes`) is saved on the Request's Context (see `GetResponseFromContext`).

type Option

type Option func(*Client)

Option declares an option for the hcaptcha client. See `New` package-level function.

func WithRemoteIP

func WithRemoteIP(remoteIP string) Option

WithRemoteIP sets the remote ip field to the given value. It sends the remoteip form field on "SiteVerify".

func WithSiteKey

func WithSiteKey(siteKey string) Option

WithSiteKey sets the site key field to the given value. It sends the sitekey form field on "SiteVerify".

type Response

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

Response is the hcaptcha JSON response.

func Get

func Get(ctx *context.Context) (Response, bool)

Get returns the hcaptcha `Response` of the current request and reports whether was found or not.

func SiteVerify

func SiteVerify(ctx *context.Context, secret, remoteIP, siteKey string) (response Response)

SiteVerify accepts an Iris Context and a secret key (https://dashboard.hcaptcha.com/settings). It returns the hcaptcha's `Response`. The `response.Success` reports whether the validation passed. Any errors are passed through the `response.ErrorCodes` field.

The remoteIP and siteKey input arguments are optional.

Jump to

Keyboard shortcuts

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