Added comments for BaseCaptcha. Fixed comments for the Captcha interface.

This commit is contained in:
Alexander Andreev 2022-08-29 00:02:58 +04:00
parent d24b710ee5
commit 2c22ff00e6
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -18,25 +18,24 @@ func NewIntAnswer() Answer {
return (Answer(ans.String())) return (Answer(ans.String()))
} }
// Captcha interface that should be implemented by captcha. // Captcha interface that should be implemented by a CAPTCHA.
type Captcha interface { type Captcha interface {
// Image generates and returns an image of captcha. // Image generates and returns a pointer to an image of CAPTCHA.
Image(style string) *image.Image Image(style string) *image.Image
// GetAnswer returns a pregenerated answer. // Answer returns a pregenerated answer.
Answer() Answer Answer() Answer
// Solve compares a stored answer with a passed one. // Solve compares a stored answer with a passed one.
// Sets field Solved to true if they are equal.
Solve(answer Answer) bool Solve(answer Answer) bool
// IsSolved returns field IsSolved. // IsSolved returns if a CAPTCHA is solved or not.
IsSolved() bool IsSolved() bool
// Expiry returns a date after what captcha will expire. // Expiry returns a date after what CAPTCHA will expire.
Expiry() time.Time Expiry() time.Time
} }
// BaseCaptcha is a base implementation of a CAPTCHA. // BaseCaptcha is a base implementation of a CAPTCHA.
// //
// All derivatives that embed this struct only need to // All derivatives that embed this struct only need to implement
// implement Image() method. // an Image() method.
type BaseCaptcha struct { type BaseCaptcha struct {
answer Answer answer Answer
solved bool solved bool
@ -53,6 +52,8 @@ func (c *BaseCaptcha) Image(style string) *image.Image {
return nil return nil
} }
// Answer generates an integer answer for a CAPTCHA or just returns
// an existing one.
func (c *BaseCaptcha) Answer() Answer { func (c *BaseCaptcha) Answer() Answer {
if c.answer == "" { if c.answer == "" {
c.answer = NewIntAnswer() c.answer = NewIntAnswer()
@ -60,6 +61,8 @@ func (c *BaseCaptcha) Answer() Answer {
return c.answer return c.answer
} }
// Solve sets solved field to true if given answer is right and returns a result
// of a check.
func (c *BaseCaptcha) Solve(answer Answer) bool { func (c *BaseCaptcha) Solve(answer Answer) bool {
c.solved = c.answer == answer c.solved = c.answer == answer
return c.solved return c.solved
@ -73,6 +76,8 @@ func (c *BaseCaptcha) Expiry() time.Time {
return c.expireIn return c.expireIn
} }
// ExpiryDate returns a date when CAPTCHA expires. It adds a passed
// expiry duration to a current time.
func ExpiryDate(expiry time.Duration) time.Time { func ExpiryDate(expiry time.Duration) time.Time {
return time.Now().Add(expiry) return time.Now().Add(expiry)
} }