1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 03:21:29 +00:00

add some comments for captcha

This commit is contained in:
slene 2014-01-16 21:34:59 +08:00
parent f419c12427
commit 91cbe1f29b
2 changed files with 80 additions and 10 deletions

View File

@ -1,7 +1,47 @@
// an example for use captcha
//
// ```
// package controllers
//
// import (
// "github.com/astaxie/beego"
// "github.com/astaxie/beego/cache"
// "github.com/astaxie/beego/utils/captcha"
// )
//
// var cpt *captcha.Captcha
//
// func init() {
// store := cache.NewMemoryCache()
// cpt = captcha.NewWithFilter("/captcha/", store)
// }
//
// type MainController struct {
// beego.Controller
// }
//
// func (this *MainController) Get() {
// this.TplNames = "index.tpl"
// }
//
// func (this *MainController) Post() {
// this.TplNames = "index.tpl"
//
// this.Data["Success"] = cpt.VerifyReq(this.Ctx.Request)
// }
// ```
//
// template usage
//
// ```
// {{.Success}}
// <form action="/" method="post">
// {{create_captcha}}
// <input name="captcha" type="text">
// </form>
// ```
package captcha package captcha
// modifiy and integrated to Beego in one file from https://github.com/dchest/captcha
import ( import (
"fmt" "fmt"
"html/template" "html/template"
@ -20,6 +60,7 @@ var (
) )
const ( const (
// default captcha attributes
challengeNums = 6 challengeNums = 6
expiration = 600 expiration = 600
fieldIdName = "captcha_id" fieldIdName = "captcha_id"
@ -29,14 +70,28 @@ const (
) )
type Captcha struct { type Captcha struct {
// beego cache store
store cache.Cache store cache.Cache
// url prefix for captcha image
urlPrefix string urlPrefix string
// specify captcha id input field name
FieldIdName string FieldIdName string
// specify captcha result input field name
FieldCaptchaName string FieldCaptchaName string
// captcha image width and height
StdWidth int StdWidth int
StdHeight int StdHeight int
// captcha chars nums
ChallengeNums int ChallengeNums int
// captcha expiration seconds
Expiration int64 Expiration int64
// cache key prefix
CachePrefix string CachePrefix string
} }
@ -48,6 +103,7 @@ func (c *Captcha) genRandChars() []byte {
return utils.RandomCreateBytes(c.ChallengeNums, defaultChars...) return utils.RandomCreateBytes(c.ChallengeNums, defaultChars...)
} }
// beego filter handler for serve captcha image
func (c *Captcha) Handler(ctx *context.Context) { func (c *Captcha) Handler(ctx *context.Context) {
var chars []byte var chars []byte
@ -83,6 +139,7 @@ func (c *Captcha) Handler(ctx *context.Context) {
} }
} }
// tempalte func for output html
func (c *Captcha) CreateCaptchaHtml() template.HTML { func (c *Captcha) CreateCaptchaHtml() template.HTML {
value, err := c.CreateCaptcha() value, err := c.CreateCaptcha()
if err != nil { if err != nil {
@ -97,6 +154,7 @@ func (c *Captcha) CreateCaptchaHtml() template.HTML {
`</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
func (c *Captcha) CreateCaptcha() (string, error) { func (c *Captcha) CreateCaptcha() (string, error) {
// generate captcha id // generate captcha id
id := string(utils.RandomCreateBytes(15)) id := string(utils.RandomCreateBytes(15))
@ -112,11 +170,13 @@ func (c *Captcha) CreateCaptcha() (string, error) {
return id, nil return id, nil
} }
// verify from a request
func (c *Captcha) VerifyReq(req *http.Request) bool { func (c *Captcha) VerifyReq(req *http.Request) bool {
req.ParseForm() 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
func (c *Captcha) Verify(id string, challenge string) (success bool) { func (c *Captcha) Verify(id string, challenge string) (success bool) {
if len(challenge) == 0 || len(id) == 0 { if len(challenge) == 0 || len(id) == 0 {
return return
@ -147,6 +207,7 @@ func (c *Captcha) Verify(id string, challenge string) (success bool) {
return true return true
} }
// create a new captcha.Captcha
func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha { func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha {
cpt := &Captcha{} cpt := &Captcha{}
cpt.store = store cpt.store = store
@ -171,6 +232,8 @@ func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha {
return cpt return cpt
} }
// 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 { func NewWithFilter(urlPrefix string, store cache.Cache) *Captcha {
cpt := NewCaptcha(urlPrefix, store) cpt := NewCaptcha(urlPrefix, store)

View File

@ -1,3 +1,4 @@
// modifiy and integrated to Beego from https://github.com/dchest/captcha
package captcha package captcha
import ( import (
@ -240,10 +241,16 @@ func getrand() *rand.Rand {
} }
func randIntn(max int) int { func randIntn(max int) int {
if max <= 0 {
return 0
}
return getrand().Intn(max) return getrand().Intn(max)
} }
func randInt(min, max int) int { func randInt(min, max int) int {
if max-min <= 0 {
return 0
}
return getrand().Intn(max-min) + min return getrand().Intn(max-min) + min
} }