From 8053952122b382c37407ffad21af04cd4522cbd1 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 26 Jun 2022 23:53:52 +0400 Subject: [PATCH] Captcha implementation was moved off to pkg directory to be able to be used as an external library. --- internal/captcha/dwelling_captcha.go | 26 ++++++++++++++++++++++---- internal/captcha/instance.go | 13 +++++++------ internal/handlers/handlers.go | 11 ++++++----- {internal => pkg}/captcha/captcha.go | 12 ++++++++++++ {internal => pkg}/captcha/db.go | 0 5 files changed, 47 insertions(+), 15 deletions(-) rename {internal => pkg}/captcha/captcha.go (84%) rename {internal => pkg}/captcha/db.go (100%) diff --git a/internal/captcha/dwelling_captcha.go b/internal/captcha/dwelling_captcha.go index e3e8f7d..a660c62 100644 --- a/internal/captcha/dwelling_captcha.go +++ b/internal/captcha/dwelling_captcha.go @@ -3,6 +3,7 @@ package captcha import ( "crypto/rand" "image" + "justcaptcha/pkg/captcha" "math/big" "time" @@ -15,14 +16,14 @@ const ( ) type DwellingCaptcha struct { - Captcha + captcha.Captcha } func NewDwellingCaptcha(expiry time.Duration) *DwellingCaptcha { return &DwellingCaptcha{ - Captcha: Captcha{ - Answer: GenerateAnswer(), - ExpireIn: ExpiryDate(expiry)}} + Captcha: captcha.Captcha{ + Answer: captcha.GenerateAnswer(), + ExpireIn: captcha.ExpiryDate(expiry)}} } func (c *DwellingCaptcha) generateImage() *image.Image { @@ -72,3 +73,20 @@ func (c *DwellingCaptcha) GetImage() *image.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 +} diff --git a/internal/captcha/instance.go b/internal/captcha/instance.go index 8315a13..e05d19a 100644 --- a/internal/captcha/instance.go +++ b/internal/captcha/instance.go @@ -2,29 +2,30 @@ package captcha import ( "image" + "justcaptcha/pkg/captcha" "time" ) -var captchaDb CaptchaDB = CaptchaDB{ - DB: make(map[ID]ICaptcha)} +var captchaDb captcha.CaptchaDB = captcha.CaptchaDB{ + DB: make(map[captcha.ID]captcha.ICaptcha)} func Init(expiry time.Duration) { 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) } -func Image(id ID) (*image.Image, error) { +func Image(id captcha.ID) (*image.Image, error) { 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) } -func IsSolved(id ID) (bool, error) { +func IsSolved(id captcha.ID) (bool, error) { return captchaDb.IsSolved(id) } diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index d2d1538..b5db72f 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -4,6 +4,7 @@ import ( "fmt" "image/png" "justcaptcha/internal/captcha" + pcaptcha "justcaptcha/pkg/captcha" "justcaptcha/pkg/server" "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) { - captchaID := captcha.ID(server.GetURLParam(r, "captcha")) + captchaID := pcaptcha.ID(server.GetURLParam(r, "captcha")) captchaImage, err := captcha.Image(captchaID) 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) { r.ParseForm() - captchaID := captcha.ID(server.GetURLParam(r, "captcha")) - answer := captcha.Answer(r.FormValue("answer")) + captchaID := pcaptcha.ID(server.GetURLParam(r, "captcha")) + 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 { w.WriteHeader(http.StatusNotFound) 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) { - captchaID := captcha.ID(server.GetURLParam(r, "captcha")) + captchaID := pcaptcha.ID(server.GetURLParam(r, "captcha")) solved, err := captcha.IsSolved(captchaID) if err != nil { diff --git a/internal/captcha/captcha.go b/pkg/captcha/captcha.go similarity index 84% rename from internal/captcha/captcha.go rename to pkg/captcha/captcha.go index 9df4794..476dcdc 100644 --- a/internal/captcha/captcha.go +++ b/pkg/captcha/captcha.go @@ -33,6 +33,18 @@ type Captcha struct { 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 { c.Solved = c.Answer == answer return c.Solved diff --git a/internal/captcha/db.go b/pkg/captcha/db.go similarity index 100% rename from internal/captcha/db.go rename to pkg/captcha/db.go