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 (
"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
}

View File

@ -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)
}

View File

@ -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 {

View File

@ -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