captcha

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2022 License: GPL-3.0 Imports: 15 Imported by: 2

README

captcha

This micro-lib aim to facilitate your captcha managment.

What the lib does:

  • chose a random challenge
  • get 9 random answers (at least one good and one bad)
  • encrypt answers id with random data so that bot can't learn from previous resource ID
  • generate a signature based on proper answer and timestamp
  • validate user answer based on previous signature
  • ensure the answer is recent enough

What the lib don't do:

  • coffee
  • prevent brutforce in challenge valide duration (but you can add delay to your answer to limit that)
  • generate captcha on its own (optifur dedicated micro-service may be made available later)

What you need to do: create a struct that implement the ChallengeManager interface. The ChallengeManager will define challenge list and what resources are valide/invalid answer

ChallengeManager

ListChallenges
	ListChallenges(kind string) ([]ChallengeType, error)

For a given kind (Eg: audio,visual) return a list of available challenge type. You can return a static list that will always be the same for a given kind, or generate a new challenge each time. If you chose to generate a challenge, make sure you provide enought Metadata and/or Data to be able to generate proper good/bad answers.

GetGoodAnswerIDs/GetBadAnswerIDs
	GetGoodAnswerIDs(challenge ChallengeType, count int) ([]string, error)
	GetBadAnswerIDs(challenge ChallengeType, count int) ([]string, error)

For a challenge, return the requested amount of good/bad resource ID. Those IDs are destined to be linked to file (image file, audio file), but depending on what you want to do you may use anything, event if no file is associated with the ID

GetResourcePath
	GetResourcePath(id string) (string, error)

The concept of the captcha was that each challenge offer 9 resource (audio or visual) to the user. GetResourcePath is here to associate a resource ID to the file path of the resource. Note that if you don't associate an aswer to a file resource you can return "", nil here

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CaptchaController

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

CaptchaController handle captcha generation and validation

func NewCaptchaController

func NewCaptchaController(challengeManager ChallengeManager, key []byte) *CaptchaController

NewCaptchaController init a captchat controller with provided manager When validating a captcha, you must use the same controller as keys are randomly generated

func (*CaptchaController) CheckControl

func (c *CaptchaController) CheckControl(ids []string, ctrl string, validityDuration time.Duration) (bool, error)

CheckControl validate the user answer with the challenge controle string The user answer needs to be in the order of appearance from the challenge returned by NewChallenge

func (*CaptchaController) DecryptID

func (c *CaptchaController) DecryptID(dataB64 string) (string, error)

DecryptID decrypt the id in order to retrieve the base ID without the random part

func (*CaptchaController) EncryptID

func (c *CaptchaController) EncryptID(id string) (string, error)

EncryptID encrypt an id with a rand part so that successif call to captcha will not yield the same id twice

func (*CaptchaController) NewChallenge

func (c *CaptchaController) NewChallenge(kind string, lang string) (*Challenge, error)

NewChallenge generate a new challenge with provided kind kind will be provided to the callenge manager. You probably want it to be "visual" or "audio"

func (*CaptchaController) ResourcePath added in v0.0.2

func (c *CaptchaController) ResourcePath(id string) (string, error)

ResourcePath return the path to the resource with the provided ID

type Challenge

type Challenge struct {
	// Controle is used to validate the user answer
	Controle string `json:"controle"`
	// ResourcesID is an ordered list of available answer to choose from
	ResourcesID []string `json:"resourcesId"`
	// ChallengeType define what kind of challenge the user needs to answer
	ChallengeType ChallengeType `json:"challengeType"`
}

Challenge contains data for use by the end-user

type ChallengeManager

type ChallengeManager interface {
	ListChallenges(kind string, lang string) ([]ChallengeType, error)

	GetGoodAnswerIDs(challenge ChallengeType, count int) ([]string, error)
	GetBadAnswerIDs(challenge ChallengeType, count int) ([]string, error)

	GetResourcePath(id string) (string, error)
}

ChallengeManager define what challenges are available, provide good and bad answers to challenges as well as resource path

type ChallengeType

type ChallengeType struct {
	// Kind of challenge. Eg: "visual", "audio"
	Kind string `json:"kind"`
	// Metadata related to that challenge. Eg: `"name":"dice throw"`, `"want":"ge-15"`
	Metadata map[string]string `json:"metadata"`
	// Any data, more flexible than Metadata
	Data interface{} `json:"data"`
}

ChallengeType define a type of challenge

type CryptoSource

type CryptoSource struct {
}

func (CryptoSource) Int63

func (_ CryptoSource) Int63() int64

func (CryptoSource) Seed

func (_ CryptoSource) Seed(_ int64)

Directories

Path Synopsis
Package mock_captcha is a generated GoMock package.
Package mock_captcha is a generated GoMock package.

Jump to

Keyboard shortcuts

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