hcaptcha

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 7 Imported by: 0

README


id: hcaptcha

HCaptcha

Release Discord Test Security Linter

A simple HCaptcha middleware to prevent bot attacks.

:::note

Requires Go 1.21 and above

:::

Install

:::caution

This middleware only supports Fiber v3.

:::

go get -u github.com/gofiber/fiber/v3
go get -u github.com/gofiber/contrib/hcaptcha

Signature

hcaptcha.New(config hcaptcha.Config) fiber.Handler

Config

Property Type Description Default
SecretKey string The secret key you obtained from the HCaptcha admin panel. This field must not be empty. ""
ResponseKeyFunc func(fiber.Ctx) (string, error) ResponseKeyFunc should return the token that captcha provides upon successful solving. By default, it gets the token from the body by parsing a JSON request and returns the hcaptcha_token field. hcaptcha.DefaultResponseKeyFunc
SiteVerifyURL string This property specifies the API resource used for token authentication. https://api.hcaptcha.com/siteverify

Example

package main

import (
    "github.com/gofiber/contrib/hcaptcha"
    "github.com/gofiber/fiber/v3"
    "log"
)

const (
    TestSecretKey = "0x0000000000000000000000000000000000000000"
    TestSiteKey   = "20000000-ffff-ffff-ffff-000000000002"
)

func main() {
    app := fiber.New()
    captcha := hcaptcha.New(hcaptcha.Config{
        // Must set the secret key
        SecretKey: TestSecretKey,
    })
	
    app.Get("/api/", func(c fiber.Ctx) error {
        return c.JSON(fiber.Map{
            "hcaptcha_site_key": TestSiteKey,
        })
    })
	
    app.Post("/api/robots-excluded", func(c fiber.Ctx) error {
        return c.SendString("You are not a robot")
    }, captcha)
	
    log.Fatal(app.Listen(":3000"))
}

Documentation

Overview

Package hcaptcha is a simple middleware that checks for an HCaptcha UUID and then validates it. It returns an error if the UUID is not valid (the request may have been sent by a robot).

Index

Constants

View Source
const DefaultSiteVerifyURL = "https://api.hcaptcha.com/siteverify"

DefaultSiteVerifyURL is the default URL for the HCaptcha API

Variables

This section is empty.

Functions

func DefaultResponseKeyFunc

func DefaultResponseKeyFunc(c fiber.Ctx) (string, error)

DefaultResponseKeyFunc is the default function to get the HCaptcha token from the request body

func New

func New(config Config) fiber.Handler

New creates a new HCaptcha middleware handler.

Types

type Config

type Config struct {
	// SecretKey is the secret key you get from HCaptcha when you create a new application
	SecretKey string
	// ResponseKeyFunc should return the generated pass UUID from the ctx, which will be validated
	ResponseKeyFunc func(fiber.Ctx) (string, error)
	// SiteVerifyURL is the endpoint URL where the program should verify the given token
	// default value is: "https://api.hcaptcha.com/siteverify"
	SiteVerifyURL string
}

Config defines the config for HCaptcha middleware.

type HCaptcha

type HCaptcha struct {
	Config
}

HCaptcha is a middleware handler that checks for an HCaptcha UUID and then validates it.

func (*HCaptcha) Validate

func (h *HCaptcha) Validate(c fiber.Ctx) error

Validate checks for an HCaptcha UUID and then validates it.

Jump to

Keyboard shortcuts

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