Added support for different styles for CAPTCHA.
This commit is contained in:
parent
0957102169
commit
96c13ec6b2
@ -25,7 +25,7 @@ func NewDwellingCaptcha(expiry time.Duration) *DwellingCaptcha {
|
|||||||
ExpireIn: captcha.ExpiryDate(expiry)}}
|
ExpireIn: captcha.ExpiryDate(expiry)}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DwellingCaptcha) generateImage() *image.Image {
|
func (c *DwellingCaptcha) generateImage(style string) *image.Image {
|
||||||
ctx := gg.NewContext(dwImageWidth, dwImageHeight)
|
ctx := gg.NewContext(dwImageWidth, dwImageHeight)
|
||||||
|
|
||||||
// fill background
|
// fill background
|
||||||
@ -65,9 +65,9 @@ func (c *DwellingCaptcha) generateImage() *image.Image {
|
|||||||
return &c.Image
|
return &c.Image
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DwellingCaptcha) GetImage() *image.Image {
|
func (c *DwellingCaptcha) GetImage(style string) *image.Image {
|
||||||
if c.Image == nil {
|
if c.Image == nil {
|
||||||
return c.generateImage()
|
return c.generateImage(style)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &c.Image
|
return &c.Image
|
||||||
|
@ -17,8 +17,8 @@ func New(data string, captcha captcha.ICaptcha) (captcha.ICaptcha, captcha.ID) {
|
|||||||
return captchaDb.New(data, captcha)
|
return captchaDb.New(data, captcha)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Image(id captcha.ID) (*image.Image, error) {
|
func Image(id captcha.ID, style string) (*image.Image, error) {
|
||||||
return captchaDb.Image(id)
|
return captchaDb.Image(id, style)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Solve(id captcha.ID, answer captcha.Answer) (bool, error) {
|
func Solve(id captcha.ID, answer captcha.Answer) (bool, error) {
|
||||||
|
@ -24,8 +24,9 @@ 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 := pcaptcha.ID(server.GetURLParam(r, "captcha"))
|
captchaID := pcaptcha.ID(server.GetURLParam(r, "captcha"))
|
||||||
|
captchaStyle := r.URL.Query().Get("style")
|
||||||
|
|
||||||
captchaImage, err := captcha.Image(captchaID)
|
captchaImage, err := captcha.Image(captchaID, captchaStyle)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
return
|
return
|
||||||
|
@ -19,10 +19,10 @@ type Answer string
|
|||||||
// ICaptcha interface that should be implemented by captcha.
|
// ICaptcha interface that should be implemented by captcha.
|
||||||
type ICaptcha interface {
|
type ICaptcha interface {
|
||||||
// generateImage is used to create a captcha image.
|
// generateImage is used to create a captcha image.
|
||||||
generateImage() *image.Image
|
generateImage(style string) *image.Image
|
||||||
// GetImage() returns an image of captcha. Calls
|
// GetImage() returns an image of captcha. Calls
|
||||||
// generateImage() method if image doesn't exist.
|
// generateImage() method if image doesn't exist.
|
||||||
GetImage() *image.Image
|
GetImage(style string) *image.Image
|
||||||
// GetAnswer returns a pregenerated answer.
|
// GetAnswer returns a pregenerated answer.
|
||||||
GetAnswer() Answer
|
GetAnswer() Answer
|
||||||
// Solve compares a stored answer with a passed one.
|
// Solve compares a stored answer with a passed one.
|
||||||
@ -50,13 +50,13 @@ type Captcha struct {
|
|||||||
ExpireIn time.Time
|
ExpireIn time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Captcha) generateImage() *image.Image {
|
func (c *Captcha) generateImage(style string) *image.Image {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Captcha) GetImage() *image.Image {
|
func (c *Captcha) GetImage(style string) *image.Image {
|
||||||
if c.Image == nil {
|
if c.Image == nil {
|
||||||
return c.generateImage()
|
return c.generateImage(style)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &c.Image
|
return &c.Image
|
||||||
|
@ -78,9 +78,9 @@ func (cdb *CaptchaDB) New(data string, captcha ICaptcha) (ICaptcha, ID) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Image returns image for a captcha.
|
// Image returns image for a captcha.
|
||||||
func (cdb *CaptchaDB) Image(id ID) (*image.Image, error) {
|
func (cdb *CaptchaDB) Image(id ID, style string) (*image.Image, error) {
|
||||||
if c, ok := cdb.DB[id]; ok {
|
if c, ok := cdb.DB[id]; ok {
|
||||||
return c.GetImage(), nil
|
return c.GetImage(style), nil
|
||||||
}
|
}
|
||||||
return nil, errorNotFound
|
return nil, errorNotFound
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user