captcha

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

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

Go to latest
Published: Apr 20, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

README

captcha

Middleware captcha a middleware that provides captcha service for Tango.

API Reference

Installation

go get gitea.com/tango/captcha

Usage

// main.go
import (
	"gitea.com/lunny/tango"
	"gitea.com/tango/cache"
	"gitea.com/tango/captcha"
)

type CaptchaAction struct {
	captcha.Captcha
	renders.Renderer
}

func (c *CaptchaAction) Get() {
	c.Render("captcha.html", renders.T{
		"captcha": c.CreateHtml(),
	})
}

func (c *CaptchaAction) Post() string {
	if c.Verify() {
		return "true"
	}
	return "false"
}

func main() {
  	t := tango.Classic()
	t.Use(captcha.New())
	t.Any("/", new(CaptchaAction))
	t.Run()
}
<!-- templates/captcha.tmpl -->
{{.captcha}}

Options

captcha.Captchaer comes with a variety of configuration options:

// ...
t.Use(captcha.New(captcha.Options{
	URLPrefix:			"/captcha/", 	// URL prefix of getting captcha pictures.
	FieldIdName:		"captcha_id", 	// Hidden input element ID.
	FieldCaptchaName:	"captcha", 		// User input value element name in request form.
	ChallengeNums:		6, 				// Challenge number.
	Width:				240,			// Captcha image width.
	Height:				80,				// Captcha image height.
	Expiration:			600, 			// Captcha expiration time in seconds.
	CachePrefix:		"captcha_", 	// Cache key prefix captcha characters.
}, cache))
// ...

License

This project is under Apache v2 License. See the LICENSE file for the full license text.

Documentation

Overview

Package captcha a middleware that provides captcha service for Macaron.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Captcha

type Captcha struct {
	// contains filtered or unexported fields
}

func (*Captcha) CreateCaptcha

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

func (*Captcha) CreateHtml

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

func (*Captcha) SetCaptcha

func (c *Captcha) SetCaptcha(cpt *Captchas, req *http.Request)

func (*Captcha) Verify

func (c *Captcha) Verify() bool

verify from a request

func (*Captcha) VerifyCaptcha

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

type Captchaer

type Captchaer interface {
	SetCaptcha(*Captchas, *http.Request)
}

type Captchas

type Captchas struct {
	Options
}

Captcha represents a captcha service.

func New

func New(opts ...Options) *Captchas

NewCaptcha initializes and returns a captcha with given options.

func (*Captchas) CreateCaptcha

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

create a new captcha id

func (*Captchas) CreateHtml

func (c *Captchas) CreateHtml() template.HTML

tempalte func for output html

func (*Captchas) GenRandChars

func (c *Captchas) GenRandChars() string

func (*Captchas) Handle

func (c *Captchas) Handle(ctx *tango.Context)

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

func (*Captchas) Verify

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

direct verify id and challenge string

func (*Captchas) VerifyReq

func (c *Captchas) 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) *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.

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 {
	Caches *cache.Caches
	// 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
}

Jump to

Keyboard shortcuts

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