captcha

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2023 License: MIT Imports: 7 Imported by: 0

README

justcaptcha

A simple CAPTCHA implementation.

An example using built-in "dwelling" implementation.

Create a new CAPTCHA:

c := dwcaptcha.NewDwellingCaptcha(expiry)
_, id := inmemdb.New(someAdditionalDataUsedInIDGenerationUsuallyIPAddr, c)

Get an image for a CAPTCHA:

i := inmemdb.Image(captchaID, captchaStyle)
if i == nil {
    ... // do something if there was no image returned
}

jpeg.Encode(w, *i, &jpeg.Options{Quality: 20})

Solve a CAPTCHA:

if ok := inmemdb.Solve(captchaID, answer); !ok {
    ... // not solved
}
// solved
...

Check is CAPTCHA was solved:

if ok := inmemdb.IsSolved(captchaID); !ok {
    ... // not solved
}
// solved
...

Documentation

Index

Constants

View Source
const DefaultExpiredScanInterval = 60 * time.Second

Variables

This section is empty.

Functions

This section is empty.

Types

type Answer

type Answer string

func NewIntAnswer

func NewIntAnswer() Answer

type BaseCaptcha

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

BaseCaptcha is a base implementation of a CAPTCHA.

All derivatives that embed this struct only need to implement an Image() method.

func NewBaseCaptcha

func NewBaseCaptcha(expiry time.Duration) *BaseCaptcha

func (*BaseCaptcha) Answer

func (c *BaseCaptcha) Answer() Answer

func (*BaseCaptcha) Expiry

func (c *BaseCaptcha) Expiry() time.Time

func (*BaseCaptcha) Image

func (c *BaseCaptcha) Image(style string) *image.Image

func (*BaseCaptcha) IsSolved

func (c *BaseCaptcha) IsSolved() bool

func (*BaseCaptcha) Solve

func (c *BaseCaptcha) Solve(answer Answer) bool

type Captcha

type Captcha interface {
	// Image generates an image of a CAPTCHA according to a passed style
	// and returns a pointer to it.
	Image(style string) *image.Image
	// Answer returns a pregenerated answer.
	Answer() Answer
	// Solve compares a stored answer with a passed one.
	Solve(answer Answer) bool
	// IsSolved returns if a CAPTCHA is solved or not.
	IsSolved() bool
	// Expiry returns a date after what CAPTCHA will expire.
	Expiry() time.Time
}

Captcha interface that should be implemented by a CAPTCHA.

type DB

type DB interface {
	New(data string, captcha Captcha) (Captcha, ID)
	GetExpiry() time.Duration
	SetExpiry(expiry time.Duration)
	Image(id ID, style string) *image.Image
	Solve(id ID, answer Answer) bool
	IsSolved(id ID) bool
	Remove(id ID)
}

DB interface with all necessary methods.

type ID

type ID string

ID is a CAPTCHA identifier.

func NewID

func NewID(additionalData string, answer Answer) ID

NewID generates an ID as a sha256 hash of additionalData (usually IP-address), current time, answer and more, it adds a set of random bytes and encodes all of it with base64 in raw URL variant.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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