mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 13:30:56 +00:00
golint captcha
This commit is contained in:
parent
2389bc72f9
commit
7b81617a95
@ -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(`<input type="hidden" name="%s" value="%s">`+
|
||||
`<a class="captcha" href="javascript:">`+
|
||||
`<img onclick="this.src=('%s%s.png?reload='+(new Date()).getTime())" class="captcha-img" src="%s%s.png">`+
|
||||
`</a>`, c.FieldIdName, value, c.URLPrefix, value, c.URLPrefix, value))
|
||||
`</a>`, 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
|
||||
}
|
||||
|
@ -240,6 +240,7 @@ var font = [][]byte{
|
||||
},
|
||||
}
|
||||
|
||||
// Image struct
|
||||
type Image struct {
|
||||
*image.Paletted
|
||||
numWidth int
|
||||
|
Loading…
Reference in New Issue
Block a user