Captcha implementation was moved off to pkg directory to be able to be used as an external library.

This commit is contained in:
Alexander Andreev 2022-06-26 23:53:52 +04:00
parent a2f4ce30f5
commit 8053952122
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
5 changed files with 47 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package captcha
import ( import (
"crypto/rand" "crypto/rand"
"image" "image"
"justcaptcha/pkg/captcha"
"math/big" "math/big"
"time" "time"
@ -15,14 +16,14 @@ const (
) )
type DwellingCaptcha struct { type DwellingCaptcha struct {
Captcha captcha.Captcha
} }
func NewDwellingCaptcha(expiry time.Duration) *DwellingCaptcha { func NewDwellingCaptcha(expiry time.Duration) *DwellingCaptcha {
return &DwellingCaptcha{ return &DwellingCaptcha{
Captcha: Captcha{ Captcha: captcha.Captcha{
Answer: GenerateAnswer(), Answer: captcha.GenerateAnswer(),
ExpireIn: ExpiryDate(expiry)}} ExpireIn: captcha.ExpiryDate(expiry)}}
} }
func (c *DwellingCaptcha) generateImage() *image.Image { func (c *DwellingCaptcha) generateImage() *image.Image {
@ -72,3 +73,20 @@ func (c *DwellingCaptcha) GetImage() *image.Image {
return &c.Image return &c.Image
} }
func (c *DwellingCaptcha) GetAnswer() captcha.Answer {
return c.Answer
}
func (c *DwellingCaptcha) Solve(answer captcha.Answer) bool {
c.Solved = c.Answer == answer
return c.Solved
}
func (c *DwellingCaptcha) IsSolved() bool {
return c.Solved
}
func (c *DwellingCaptcha) Expiry() time.Time {
return c.ExpireIn
}

View File

@ -2,29 +2,30 @@ package captcha
import ( import (
"image" "image"
"justcaptcha/pkg/captcha"
"time" "time"
) )
var captchaDb CaptchaDB = CaptchaDB{ var captchaDb captcha.CaptchaDB = captcha.CaptchaDB{
DB: make(map[ID]ICaptcha)} DB: make(map[captcha.ID]captcha.ICaptcha)}
func Init(expiry time.Duration) { func Init(expiry time.Duration) {
captchaDb.SetExpiry(expiry) captchaDb.SetExpiry(expiry)
} }
func New(data string, captcha ICaptcha) (ICaptcha, ID) { func New(data string, captcha captcha.ICaptcha) (captcha.ICaptcha, captcha.ID) {
return captchaDb.New(data, captcha) return captchaDb.New(data, captcha)
} }
func Image(id ID) (*image.Image, error) { func Image(id captcha.ID) (*image.Image, error) {
return captchaDb.Image(id) return captchaDb.Image(id)
} }
func Solve(id ID, answer Answer) (bool, error) { func Solve(id captcha.ID, answer captcha.Answer) (bool, error) {
return captchaDb.Solve(id, answer) return captchaDb.Solve(id, answer)
} }
func IsSolved(id ID) (bool, error) { func IsSolved(id captcha.ID) (bool, error) {
return captchaDb.IsSolved(id) return captchaDb.IsSolved(id)
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"image/png" "image/png"
"justcaptcha/internal/captcha" "justcaptcha/internal/captcha"
pcaptcha "justcaptcha/pkg/captcha"
"justcaptcha/pkg/server" "justcaptcha/pkg/server"
"net/http" "net/http"
) )
@ -22,7 +23,7 @@ func (h *CaptchaHandlers) New(w http.ResponseWriter, r *http.Request) {
} }
func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) { func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) {
captchaID := captcha.ID(server.GetURLParam(r, "captcha")) captchaID := pcaptcha.ID(server.GetURLParam(r, "captcha"))
captchaImage, err := captcha.Image(captchaID) captchaImage, err := captcha.Image(captchaID)
if err != nil { if err != nil {
@ -43,10 +44,10 @@ func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) {
func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) { func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) {
r.ParseForm() r.ParseForm()
captchaID := captcha.ID(server.GetURLParam(r, "captcha")) captchaID := pcaptcha.ID(server.GetURLParam(r, "captcha"))
answer := captcha.Answer(r.FormValue("answer")) answer := pcaptcha.Answer(r.FormValue("answer"))
ok, err := captcha.Solve(captcha.ID(captchaID), answer) ok, err := captcha.Solve(pcaptcha.ID(captchaID), answer)
if err != nil { if err != nil {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
@ -58,7 +59,7 @@ func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) {
} }
func (h *CaptchaHandlers) IsSolved(w http.ResponseWriter, r *http.Request) { func (h *CaptchaHandlers) IsSolved(w http.ResponseWriter, r *http.Request) {
captchaID := captcha.ID(server.GetURLParam(r, "captcha")) captchaID := pcaptcha.ID(server.GetURLParam(r, "captcha"))
solved, err := captcha.IsSolved(captchaID) solved, err := captcha.IsSolved(captchaID)
if err != nil { if err != nil {

View File

@ -33,6 +33,18 @@ type Captcha struct {
ExpireIn time.Time ExpireIn time.Time
} }
func (c *Captcha) generateImage() *image.Image {
return nil
}
func (c *Captcha) GetImage() *image.Image {
if c.Image == nil {
return c.generateImage()
}
return &c.Image
}
func (c *Captcha) Solve(answer Answer) bool { func (c *Captcha) Solve(answer Answer) bool {
c.Solved = c.Answer == answer c.Solved = c.Answer == answer
return c.Solved return c.Solved