captcha

package
v0.0.0-...-97e0de2 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: GPL-3.0 Imports: 6 Imported by: 2

Documentation

Overview

Package captcha wraps github.com/dchest/captcha and provides an sqlite store for it.

Initialize the captcha package:

captcha.Initialize("captcha.sqlite3")

Register the captcha handler in your HTTP router:

router.Handler(http.MethodGet, "/captcha/:fn", captcha.Handler())

Parse the captcha template string along with your HTML templates:

t = template.Must(t.Parse(captcha.TemplateString))

Execute the template:

{{template "captcha" .Captcha}}

Pass captcha data to your template:

type MyTemplateData struct {
    Captcha captcha.TemplateData
    // ...

Create a captcha in your GET handler:

myTemplateData.Captcha.ID = captcha.New()

In your POST handler, call Verify after validating other input because Verify invalidates the captcha. If you're executing the template again, you must create a new captcha.

id := r.PostFormValue("captcha-id")
answer := r.PostFormValue("captcha-answer")
if !captcha.Verify(id, answer) {
    data.Captcha.ID = captcha.New()
    data.Captcha.Err = true
    html.MyTemplate.Execute(w, data)
    return
}

Index

Constants

View Source
const TemplateString = `` /* 1019-byte string literal not displayed */

Variables

This section is empty.

Functions

func Handler

func Handler() http.Handler

func Initialize

func Initialize(sqlitedb string) error

func New

func New() (id string)

func Verify

func Verify(id string, digits string) bool

Verify checks and invalidates the captcha.

Types

type TemplateData

type TemplateData struct {
	Answer string // old answer, maybe incorrect
	Err    bool
	ID     string
}

Jump to

Keyboard shortcuts

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