2022-06-24 23:09:46 +04:00
|
|
|
package captcha
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"image"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-08-17 21:46:10 +04:00
|
|
|
maxIntAnswer = 999999
|
2022-06-24 23:09:46 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Answer string
|
|
|
|
|
2022-08-17 21:46:10 +04:00
|
|
|
func NewIntAnswer() Answer {
|
|
|
|
ans, _ := rand.Int(rand.Reader, big.NewInt(maxIntAnswer))
|
|
|
|
return (Answer(ans.String()))
|
|
|
|
}
|
|
|
|
|
2022-07-30 15:37:05 +04:00
|
|
|
// Captcha interface that should be implemented by captcha.
|
|
|
|
type Captcha interface {
|
|
|
|
// Image generates and returns an image of captcha.
|
2022-08-17 21:47:27 +04:00
|
|
|
Image(style string) *image.Image
|
2022-06-27 00:43:12 +04:00
|
|
|
// GetAnswer returns a pregenerated answer.
|
2022-06-24 23:09:46 +04:00
|
|
|
GetAnswer() Answer
|
2022-06-27 00:43:12 +04:00
|
|
|
// Solve compares a stored answer with a passed one.
|
|
|
|
// Sets field Solved to true if they are equal.
|
2022-06-24 23:09:46 +04:00
|
|
|
Solve(answer Answer) bool
|
2022-06-27 00:43:12 +04:00
|
|
|
// IsSolved returns field IsSolved.
|
2022-06-24 23:09:46 +04:00
|
|
|
IsSolved() bool
|
2022-06-27 00:43:12 +04:00
|
|
|
// Expiry returns a date after what captcha will expire.
|
2022-06-26 20:47:25 +04:00
|
|
|
Expiry() time.Time
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
2022-07-30 15:37:05 +04:00
|
|
|
// BaseCaptcha is a base implementation of a CAPTCHA.
|
2022-06-27 00:43:12 +04:00
|
|
|
//
|
|
|
|
// All derivatives that embed this struct only need to
|
2022-08-19 02:11:06 +04:00
|
|
|
// implement Image() method.
|
2022-07-30 15:37:05 +04:00
|
|
|
type BaseCaptcha struct {
|
2022-08-19 02:21:57 +04:00
|
|
|
answer Answer
|
|
|
|
solved bool
|
|
|
|
expireIn time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBaseCaptcha(expiry time.Duration) *BaseCaptcha {
|
|
|
|
return &BaseCaptcha{
|
|
|
|
expireIn: ExpiryDate(expiry),
|
|
|
|
}
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
2022-07-30 15:37:05 +04:00
|
|
|
func (c *BaseCaptcha) Image(style string) *image.Image {
|
2022-06-26 23:53:52 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-30 15:37:05 +04:00
|
|
|
func (c *BaseCaptcha) GetAnswer() Answer {
|
2022-08-19 02:21:57 +04:00
|
|
|
if c.answer == "" {
|
|
|
|
c.answer = NewIntAnswer()
|
2022-06-27 00:44:11 +04:00
|
|
|
}
|
2022-08-19 02:21:57 +04:00
|
|
|
return c.answer
|
2022-06-26 22:02:38 +04:00
|
|
|
}
|
|
|
|
|
2022-08-19 02:11:06 +04:00
|
|
|
func (c *BaseCaptcha) Solve(answer Answer) bool {
|
2022-08-19 02:21:57 +04:00
|
|
|
c.solved = c.answer == answer
|
|
|
|
return c.solved
|
2022-08-19 02:11:06 +04:00
|
|
|
}
|
|
|
|
|
2022-07-30 15:37:05 +04:00
|
|
|
func (c *BaseCaptcha) IsSolved() bool {
|
2022-08-19 02:21:57 +04:00
|
|
|
return c.solved
|
2022-06-26 22:02:38 +04:00
|
|
|
}
|
|
|
|
|
2022-07-30 15:37:05 +04:00
|
|
|
func (c *BaseCaptcha) Expiry() time.Time {
|
2022-08-19 02:21:57 +04:00
|
|
|
return c.expireIn
|
2022-06-26 22:02:38 +04:00
|
|
|
}
|
|
|
|
|
2022-06-26 20:47:25 +04:00
|
|
|
func ExpiryDate(expiry time.Duration) time.Time {
|
|
|
|
return time.Now().Add(expiry)
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|