diff --git a/utils/captcha/captcha.go b/utils/captcha/captcha.go index 14c6c3b3..3616cc7e 100644 --- a/utils/captcha/captcha.go +++ b/utils/captcha/captcha.go @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +// Package captcha implements generation and verification of image CAPTCHAs. // an example for use captcha // // ``` @@ -78,7 +79,7 @@ const ( // default captcha attributes challengeNums = 6 expiration = 600 - fieldIdName = "captcha_id" + fieldIDName = "captcha_id" fieldCaptchaName = "captcha" cachePrefix = "captcha_" defaultURLPrefix = "/captcha/" @@ -93,7 +94,7 @@ type Captcha struct { URLPrefix string // specify captcha id input field name - FieldIdName string + FieldIDName string // specify captcha result input field name FieldCaptchaName string @@ -121,7 +122,7 @@ func (c *Captcha) genRandChars() []byte { return utils.RandomCreateBytes(c.ChallengeNums, defaultChars...) } -// beego filter handler for serve captcha image +// Handler beego filter handler for serve captcha image func (c *Captcha) Handler(ctx *context.Context) { var chars []byte @@ -156,8 +157,8 @@ func (c *Captcha) Handler(ctx *context.Context) { } } -// tempalte func for output html -func (c *Captcha) CreateCaptchaHtml() template.HTML { +// CreateCaptchaHTML tempalte func for output html +func (c *Captcha) CreateCaptchaHTML() template.HTML { value, err := c.CreateCaptcha() if err != nil { beego.Error("Create Captcha Error:", err) @@ -168,10 +169,10 @@ func (c *Captcha) CreateCaptchaHtml() template.HTML { return template.HTML(fmt.Sprintf(``+ ``+ ``+ - ``, c.FieldIdName, value, c.URLPrefix, value, c.URLPrefix, value)) + ``, c.FieldIDName, value, c.URLPrefix, value, c.URLPrefix, value)) } -// create a new captcha id +// CreateCaptcha create a new captcha id func (c *Captcha) CreateCaptcha() (string, error) { // generate captcha id id := string(utils.RandomCreateBytes(15)) @@ -187,13 +188,13 @@ func (c *Captcha) CreateCaptcha() (string, error) { return id, nil } -// verify from a request +// VerifyReq verify from a request func (c *Captcha) VerifyReq(req *http.Request) bool { req.ParseForm() - return c.Verify(req.Form.Get(c.FieldIdName), req.Form.Get(c.FieldCaptchaName)) + return c.Verify(req.Form.Get(c.FieldIDName), req.Form.Get(c.FieldCaptchaName)) } -// direct verify id and challenge string +// Verify direct verify id and challenge string func (c *Captcha) Verify(id string, challenge string) (success bool) { if len(challenge) == 0 || len(id) == 0 { return @@ -227,11 +228,11 @@ func (c *Captcha) Verify(id string, challenge string) (success bool) { return true } -// create a new captcha.Captcha +// NewCaptcha create a new captcha.Captcha func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha { cpt := &Captcha{} cpt.store = store - cpt.FieldIdName = fieldIdName + cpt.FieldIDName = fieldIDName cpt.FieldCaptchaName = fieldCaptchaName cpt.ChallengeNums = challengeNums cpt.Expiration = expiration @@ -252,7 +253,7 @@ func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha { return cpt } -// create a new captcha.Captcha and auto AddFilter for serve captacha image +// NewWithFilter create a new captcha.Captcha and auto AddFilter for serve captacha image // and add a tempalte func for output html func NewWithFilter(urlPrefix string, store cache.Cache) *Captcha { cpt := NewCaptcha(urlPrefix, store) @@ -261,7 +262,7 @@ func NewWithFilter(urlPrefix string, store cache.Cache) *Captcha { beego.InsertFilter(cpt.URLPrefix+"*", beego.BeforeRouter, cpt.Handler) // add to template func map - beego.AddFuncMap("create_captcha", cpt.CreateCaptchaHtml) + beego.AddFuncMap("create_captcha", cpt.CreateCaptchaHTML()) return cpt } diff --git a/utils/captcha/image.go b/utils/captcha/image.go index 8e9039d0..1057192a 100644 --- a/utils/captcha/image.go +++ b/utils/captcha/image.go @@ -240,6 +240,7 @@ var font = [][]byte{ }, } +// Image struct type Image struct { *image.Paletted numWidth int