captcha

package module
v0.0.0-...-fb487f6 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: Apache-2.0 Imports: 16 Imported by: 7

README

captcha

Middleware captcha provides captcha service for chi.

Installation
go get gitea.com/go-chi/captcha

Getting Help

License

This project is under the Apache License, Version 2.0. See the LICENSE file for the full license text.

Documentation

Overview

Package captcha a middleware that provides captcha service for chi.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Captchaer

func Captchaer(cpt *Captcha) func(http.Handler) http.Handler

Captchaer is a middleware that maps a captcha.Captcha service into the chi handler chain. An single variadic captcha.Options struct can be optionally provided to configure. This should be register after cache.Cacher.

func Version

func Version() string

Types

type Captcha

type Captcha struct {
	Store            cache.Cache
	SubURL           string
	URLPrefix        string
	FieldIdName      string
	FieldCaptchaName string
	StdWidth         int
	StdHeight        int
	ChallengeNums    int
	Expiration       int64
	CachePrefix      string
	ColorPalette     color.Palette
}

Captcha represents a captcha service.

func NewCaptcha

func NewCaptcha(opts ...Options) *Captcha

NewCaptcha initializes and returns a captcha with given options.

func (*Captcha) CreateCaptcha

func (c *Captcha) CreateCaptcha() (string, error)

create a new captcha id

func (*Captcha) CreateHTML

func (c *Captcha) CreateHTML() template.HTML

CreateHTML outputs HTML for display and fetch new captcha images.

func (*Captcha) Verify

func (c *Captcha) Verify(id string, challenge string) bool

direct verify id and challenge string

func (*Captcha) VerifyReq

func (c *Captcha) VerifyReq(req *http.Request) bool

verify from a request

type Image

type Image struct {
	*image.Paletted
	// contains filtered or unexported fields
}

func NewImage

func NewImage(digits []byte, width, height int, colorPalette color.Palette) *Image

NewImage returns a new captcha image of the given width and height with the given digits, where each digit must be in range 0-9. The digit's color is chosen by random from the colorPalette.

func (*Image) WriteTo

func (m *Image) WriteTo(w io.Writer) (int64, error)

WriteTo writes captcha image in PNG format into the given writer.

type Options

type Options struct {
	// Suburl path. Default is empty.
	SubURL string
	// URL prefix of getting captcha pictures. Default is "/captcha/".
	URLPrefix string
	// Hidden input element ID. Default is "captcha_id".
	FieldIdName string
	// User input value element name in request form. Default is "captcha".
	FieldCaptchaName string
	// Challenge number. Default is 6.
	ChallengeNums int
	// Captcha image width. Default is 240.
	Width int
	// Captcha image height. Default is 80.
	Height int
	// Captcha expiration time in seconds. Default is 600.
	Expiration int64
	// Cache key prefix captcha characters. Default is "captcha_".
	CachePrefix string
	// ColorPalette holds a collection of primary colors used for
	// the captcha's text. If not defined, a random color will be generated.
	ColorPalette color.Palette
}

Jump to

Keyboard shortcuts

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