mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 13:10:54 +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
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package captcha implements generation and verification of image CAPTCHAs.
|
||||||
// an example for use captcha
|
// an example for use captcha
|
||||||
//
|
//
|
||||||
// ```
|
// ```
|
||||||
@ -78,7 +79,7 @@ const (
|
|||||||
// default captcha attributes
|
// default captcha attributes
|
||||||
challengeNums = 6
|
challengeNums = 6
|
||||||
expiration = 600
|
expiration = 600
|
||||||
fieldIdName = "captcha_id"
|
fieldIDName = "captcha_id"
|
||||||
fieldCaptchaName = "captcha"
|
fieldCaptchaName = "captcha"
|
||||||
cachePrefix = "captcha_"
|
cachePrefix = "captcha_"
|
||||||
defaultURLPrefix = "/captcha/"
|
defaultURLPrefix = "/captcha/"
|
||||||
@ -93,7 +94,7 @@ type Captcha struct {
|
|||||||
URLPrefix string
|
URLPrefix string
|
||||||
|
|
||||||
// specify captcha id input field name
|
// specify captcha id input field name
|
||||||
FieldIdName string
|
FieldIDName string
|
||||||
// specify captcha result input field name
|
// specify captcha result input field name
|
||||||
FieldCaptchaName string
|
FieldCaptchaName string
|
||||||
|
|
||||||
@ -121,7 +122,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
|
// Handler 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
|
||||||
|
|
||||||
@ -156,8 +157,8 @@ func (c *Captcha) Handler(ctx *context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// tempalte func for output html
|
// CreateCaptchaHTML 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 {
|
||||||
beego.Error("Create Captcha Error:", err)
|
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">`+
|
return template.HTML(fmt.Sprintf(`<input type="hidden" name="%s" value="%s">`+
|
||||||
`<a class="captcha" href="javascript:">`+
|
`<a class="captcha" href="javascript:">`+
|
||||||
`<img onclick="this.src=('%s%s.png?reload='+(new Date()).getTime())" class="captcha-img" src="%s%s.png">`+
|
`<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) {
|
func (c *Captcha) CreateCaptcha() (string, error) {
|
||||||
// generate captcha id
|
// generate captcha id
|
||||||
id := string(utils.RandomCreateBytes(15))
|
id := string(utils.RandomCreateBytes(15))
|
||||||
@ -187,13 +188,13 @@ func (c *Captcha) CreateCaptcha() (string, error) {
|
|||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// verify from a request
|
// VerifyReq 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
|
// Verify 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
|
||||||
@ -227,11 +228,11 @@ func (c *Captcha) Verify(id string, challenge string) (success bool) {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new captcha.Captcha
|
// NewCaptcha 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
|
||||||
cpt.FieldIdName = fieldIdName
|
cpt.FieldIDName = fieldIDName
|
||||||
cpt.FieldCaptchaName = fieldCaptchaName
|
cpt.FieldCaptchaName = fieldCaptchaName
|
||||||
cpt.ChallengeNums = challengeNums
|
cpt.ChallengeNums = challengeNums
|
||||||
cpt.Expiration = expiration
|
cpt.Expiration = expiration
|
||||||
@ -252,7 +253,7 @@ func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha {
|
|||||||
return cpt
|
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
|
// 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)
|
||||||
@ -261,7 +262,7 @@ func NewWithFilter(urlPrefix string, store cache.Cache) *Captcha {
|
|||||||
beego.InsertFilter(cpt.URLPrefix+"*", beego.BeforeRouter, cpt.Handler)
|
beego.InsertFilter(cpt.URLPrefix+"*", beego.BeforeRouter, cpt.Handler)
|
||||||
|
|
||||||
// add to template func map
|
// add to template func map
|
||||||
beego.AddFuncMap("create_captcha", cpt.CreateCaptchaHtml)
|
beego.AddFuncMap("create_captcha", cpt.CreateCaptchaHTML())
|
||||||
|
|
||||||
return cpt
|
return cpt
|
||||||
}
|
}
|
||||||
|
@ -240,6 +240,7 @@ var font = [][]byte{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Image struct
|
||||||
type Image struct {
|
type Image struct {
|
||||||
*image.Paletted
|
*image.Paletted
|
||||||
numWidth int
|
numWidth int
|
||||||
|
Loading…
Reference in New Issue
Block a user