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
1 changed files with 13 additions and 8 deletions

View File

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