91cbe1f2 by slene

add some comments for captcha

1 parent f419c124
1 // an example for use captcha
2 //
3 // ```
4 // package controllers
5 //
6 // import (
7 // "github.com/astaxie/beego"
8 // "github.com/astaxie/beego/cache"
9 // "github.com/astaxie/beego/utils/captcha"
10 // )
11 //
12 // var cpt *captcha.Captcha
13 //
14 // func init() {
15 // store := cache.NewMemoryCache()
16 // cpt = captcha.NewWithFilter("/captcha/", store)
17 // }
18 //
19 // type MainController struct {
20 // beego.Controller
21 // }
22 //
23 // func (this *MainController) Get() {
24 // this.TplNames = "index.tpl"
25 // }
26 //
27 // func (this *MainController) Post() {
28 // this.TplNames = "index.tpl"
29 //
30 // this.Data["Success"] = cpt.VerifyReq(this.Ctx.Request)
31 // }
32 // ```
33 //
34 // template usage
35 //
36 // ```
37 // {{.Success}}
38 // <form action="/" method="post">
39 // {{create_captcha}}
40 // <input name="captcha" type="text">
41 // </form>
42 // ```
1 package captcha 43 package captcha
2 44
3 // modifiy and integrated to Beego in one file from https://github.com/dchest/captcha
4
5 import ( 45 import (
6 "fmt" 46 "fmt"
7 "html/template" 47 "html/template"
...@@ -20,6 +60,7 @@ var ( ...@@ -20,6 +60,7 @@ var (
20 ) 60 )
21 61
22 const ( 62 const (
63 // default captcha attributes
23 challengeNums = 6 64 challengeNums = 6
24 expiration = 600 65 expiration = 600
25 fieldIdName = "captcha_id" 66 fieldIdName = "captcha_id"
...@@ -29,14 +70,28 @@ const ( ...@@ -29,14 +70,28 @@ const (
29 ) 70 )
30 71
31 type Captcha struct { 72 type Captcha struct {
73 // beego cache store
32 store cache.Cache 74 store cache.Cache
75
76 // url prefix for captcha image
33 urlPrefix string 77 urlPrefix string
78
79 // specify captcha id input field name
34 FieldIdName string 80 FieldIdName string
81 // specify captcha result input field name
35 FieldCaptchaName string 82 FieldCaptchaName string
83
84 // captcha image width and height
36 StdWidth int 85 StdWidth int
37 StdHeight int 86 StdHeight int
87
88 // captcha chars nums
38 ChallengeNums int 89 ChallengeNums int
90
91 // captcha expiration seconds
39 Expiration int64 92 Expiration int64
93
94 // cache key prefix
40 CachePrefix string 95 CachePrefix string
41 } 96 }
42 97
...@@ -48,6 +103,7 @@ func (c *Captcha) genRandChars() []byte { ...@@ -48,6 +103,7 @@ func (c *Captcha) genRandChars() []byte {
48 return utils.RandomCreateBytes(c.ChallengeNums, defaultChars...) 103 return utils.RandomCreateBytes(c.ChallengeNums, defaultChars...)
49 } 104 }
50 105
106 // beego filter handler for serve captcha image
51 func (c *Captcha) Handler(ctx *context.Context) { 107 func (c *Captcha) Handler(ctx *context.Context) {
52 var chars []byte 108 var chars []byte
53 109
...@@ -83,6 +139,7 @@ func (c *Captcha) Handler(ctx *context.Context) { ...@@ -83,6 +139,7 @@ func (c *Captcha) Handler(ctx *context.Context) {
83 } 139 }
84 } 140 }
85 141
142 // tempalte func for output html
86 func (c *Captcha) CreateCaptchaHtml() template.HTML { 143 func (c *Captcha) CreateCaptchaHtml() template.HTML {
87 value, err := c.CreateCaptcha() 144 value, err := c.CreateCaptcha()
88 if err != nil { 145 if err != nil {
...@@ -97,6 +154,7 @@ func (c *Captcha) CreateCaptchaHtml() template.HTML { ...@@ -97,6 +154,7 @@ func (c *Captcha) CreateCaptchaHtml() template.HTML {
97 `</a>`, c.FieldIdName, value, c.urlPrefix, value, c.urlPrefix, value)) 154 `</a>`, c.FieldIdName, value, c.urlPrefix, value, c.urlPrefix, value))
98 } 155 }
99 156
157 // create a new captcha id
100 func (c *Captcha) CreateCaptcha() (string, error) { 158 func (c *Captcha) CreateCaptcha() (string, error) {
101 // generate captcha id 159 // generate captcha id
102 id := string(utils.RandomCreateBytes(15)) 160 id := string(utils.RandomCreateBytes(15))
...@@ -112,11 +170,13 @@ func (c *Captcha) CreateCaptcha() (string, error) { ...@@ -112,11 +170,13 @@ func (c *Captcha) CreateCaptcha() (string, error) {
112 return id, nil 170 return id, nil
113 } 171 }
114 172
173 // verify from a request
115 func (c *Captcha) VerifyReq(req *http.Request) bool { 174 func (c *Captcha) VerifyReq(req *http.Request) bool {
116 req.ParseForm() 175 req.ParseForm()
117 return c.Verify(req.Form.Get(c.FieldIdName), req.Form.Get(c.FieldCaptchaName)) 176 return c.Verify(req.Form.Get(c.FieldIdName), req.Form.Get(c.FieldCaptchaName))
118 } 177 }
119 178
179 // direct verify id and challenge string
120 func (c *Captcha) Verify(id string, challenge string) (success bool) { 180 func (c *Captcha) Verify(id string, challenge string) (success bool) {
121 if len(challenge) == 0 || len(id) == 0 { 181 if len(challenge) == 0 || len(id) == 0 {
122 return 182 return
...@@ -147,6 +207,7 @@ func (c *Captcha) Verify(id string, challenge string) (success bool) { ...@@ -147,6 +207,7 @@ func (c *Captcha) Verify(id string, challenge string) (success bool) {
147 return true 207 return true
148 } 208 }
149 209
210 // create a new captcha.Captcha
150 func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha { 211 func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha {
151 cpt := &Captcha{} 212 cpt := &Captcha{}
152 cpt.store = store 213 cpt.store = store
...@@ -171,6 +232,8 @@ func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha { ...@@ -171,6 +232,8 @@ func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha {
171 return cpt 232 return cpt
172 } 233 }
173 234
235 // create a new captcha.Captcha and auto AddFilter for serve captacha image
236 // and add a tempalte func for output html
174 func NewWithFilter(urlPrefix string, store cache.Cache) *Captcha { 237 func NewWithFilter(urlPrefix string, store cache.Cache) *Captcha {
175 cpt := NewCaptcha(urlPrefix, store) 238 cpt := NewCaptcha(urlPrefix, store)
176 239
......
1 // modifiy and integrated to Beego from https://github.com/dchest/captcha
1 package captcha 2 package captcha
2 3
3 import ( 4 import (
...@@ -240,10 +241,16 @@ func getrand() *rand.Rand { ...@@ -240,10 +241,16 @@ func getrand() *rand.Rand {
240 } 241 }
241 242
242 func randIntn(max int) int { 243 func randIntn(max int) int {
244 if max <= 0 {
245 return 0
246 }
243 return getrand().Intn(max) 247 return getrand().Intn(max)
244 } 248 }
245 249
246 func randInt(min, max int) int { 250 func randInt(min, max int) int {
251 if max-min <= 0 {
252 return 0
253 }
247 return getrand().Intn(max-min) + min 254 return getrand().Intn(max-min) + min
248 } 255 }
249 256
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!