captcha

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package captcha provides verification code management with Redis storage.

It supports creating, verifying, and deleting captcha codes with automatic expiration. Verification is atomic and one-time (code is deleted after successful verification).

Hertz Backend Setup

// Initialize captcha with Redis client
cap := captcha.New(redisClient)

// Send captcha endpoint
h.POST("/captcha/send", func(ctx context.Context, c *app.RequestContext) {
	email := c.Query("email")
	code := help.Random(6) // Generate 6-digit code
	cap.Create(ctx, "login:"+email, code, 5*time.Minute)
	// Send code via email/SMS...
	c.JSON(200, utils.H{"message": "sent"})
})

// Verify captcha endpoint
h.POST("/captcha/verify", func(ctx context.Context, c *app.RequestContext) {
	email := c.Query("email")
	code := c.Query("code")
	if err := cap.Verify(ctx, "login:"+email, code); err != nil {
		c.JSON(400, utils.H{"error": err.Error()})
		return
	}
	c.JSON(200, utils.H{"message": "verified"})
})

Angular Frontend Setup

// Send captcha request
sendCaptcha(email: string) {
  return this.http.post('/captcha/send', null, { params: { email } });
}

// Verify captcha
verifyCaptcha(email: string, code: string) {
  return this.http.post('/captcha/verify', null, { params: { email, code } });
}

Security Notes

  • Codes are deleted after successful verification (one-time use)
  • Use appropriate TTL (e.g., 5 minutes) to limit attack window
  • Consider rate limiting to prevent brute force attacks
  • Use Redis key prefix to namespace different captcha types

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotExists   = errors.New("captcha: code does not exist or expired")
	ErrInvalidCode = errors.New("captcha: invalid code")
)

Errors returned by captcha functions.

Functions

This section is empty.

Types

type Captcha

type Captcha struct {
	// RDb is the Redis client for storing captcha codes.
	RDb *redis.Client
	// Prefix is the key prefix for all captcha keys (default: "captcha").
	Prefix string
}

Captcha provides verification code management with Redis storage.

func New

func New(rdb *redis.Client, options ...Option) *Captcha

New creates a new Captcha instance with the given Redis client.

func (*Captcha) Create

func (x *Captcha) Create(ctx context.Context, name string, code string, ttl time.Duration) string

Create stores a captcha code with the given name and TTL. If a code already exists for this name, it will be overwritten. Returns "OK" on success.

func (*Captcha) Delete

func (x *Captcha) Delete(ctx context.Context, name string) int64

Delete removes a captcha code by name. Returns the number of keys deleted (0 or 1).

func (*Captcha) Exists

func (x *Captcha) Exists(ctx context.Context, name string) bool

Exists checks if a captcha code exists for the given name. Note: This does not consume the code. Use Verify for actual verification.

func (*Captcha) Key

func (x *Captcha) Key(name string) string

Key generates the full Redis key for a captcha name. Format: "{prefix}:{name}"

func (*Captcha) Verify

func (x *Captcha) Verify(ctx context.Context, name string, code string) error

Verify checks if the provided code matches the stored captcha. On successful verification, the code is automatically deleted (one-time use). Returns ErrNotExists if code doesn't exist or expired. Returns ErrInvalidCode if code doesn't match.

type Option added in v1.0.0

type Option func(x *Captcha)

Option is a function that configures a Captcha instance.

func SetPrefix added in v1.0.0

func SetPrefix(v string) Option

SetPrefix sets the Redis key prefix for captcha keys. Default is "captcha", resulting in keys like "captcha:login:user@example.com".

Jump to

Keyboard shortcuts

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