1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-09 02:30:55 +00:00
Beego/utils/captcha/captcha.go

271 lines
6.1 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2015-09-14 15:13:51 +00:00
// Package captcha implements generation and verification of image CAPTCHAs.
2014-01-16 13:34:59 +00:00
// 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() {
// // use beego cache system store the captcha data
2014-01-16 13:34:59 +00:00
// store := cache.NewMemoryCache()
// cpt = captcha.NewWithFilter("/captcha/", store)
// }
//
// type MainController struct {
// beego.Controller
// }
//
// func (this *MainController) Get() {
// this.TplName = "index.tpl"
2014-01-16 13:34:59 +00:00
// }
//
// func (this *MainController) Post() {
// this.TplName = "index.tpl"
2014-01-16 13:34:59 +00:00
//
// this.Data["Success"] = cpt.VerifyReq(this.Ctx.Request)
// }
// ```
//
// template usage
//
// ```
// {{.Success}}
// <form action="/" method="post">
// {{create_captcha}}
// <input name="captcha" type="text">
// </form>
// ```
2014-01-16 12:53:35 +00:00
package captcha
import (
"fmt"
"html/template"
"net/http"
"path"
"strings"
2016-01-08 13:29:12 +00:00
"time"
2014-01-16 12:53:35 +00:00
"github.com/astaxie/beego"
"github.com/astaxie/beego/cache"
"github.com/astaxie/beego/context"
2016-03-25 02:56:15 +00:00
"github.com/astaxie/beego/logs"
2014-01-16 12:53:35 +00:00
"github.com/astaxie/beego/utils"
)
var (
defaultChars = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
)
const (
2014-01-16 13:34:59 +00:00
// default captcha attributes
2014-01-16 12:53:35 +00:00
challengeNums = 6
2016-01-08 13:29:12 +00:00
expiration = 600 * time.Second
2015-09-14 15:13:51 +00:00
fieldIDName = "captcha_id"
2014-01-16 12:53:35 +00:00
fieldCaptchaName = "captcha"
cachePrefix = "captcha_"
2014-02-20 05:44:34 +00:00
defaultURLPrefix = "/captcha/"
2014-01-16 12:53:35 +00:00
)
// Captcha struct
2014-01-16 12:53:35 +00:00
type Captcha struct {
2014-01-16 13:34:59 +00:00
// beego cache store
store cache.Cache
// url prefix for captcha image
2014-02-20 05:44:34 +00:00
URLPrefix string
2014-01-16 13:34:59 +00:00
// specify captcha id input field name
2015-09-14 15:13:51 +00:00
FieldIDName string
2014-01-16 13:34:59 +00:00
// specify captcha result input field name
2014-01-16 12:53:35 +00:00
FieldCaptchaName string
2014-01-16 13:34:59 +00:00
// captcha image width and height
StdWidth int
StdHeight int
// captcha chars nums
ChallengeNums int
// captcha expiration seconds
2016-01-08 13:29:12 +00:00
Expiration time.Duration
2014-01-16 13:34:59 +00:00
// cache key prefix
CachePrefix string
2014-01-16 12:53:35 +00:00
}
// generate key string
2014-01-16 12:53:35 +00:00
func (c *Captcha) key(id string) string {
return c.CachePrefix + id
}
// generate rand chars with default chars
2014-01-16 12:53:35 +00:00
func (c *Captcha) genRandChars() []byte {
return utils.RandomCreateBytes(c.ChallengeNums, defaultChars...)
}
2015-09-14 15:13:51 +00:00
// Handler beego filter handler for serve captcha image
2014-01-16 12:53:35 +00:00
func (c *Captcha) Handler(ctx *context.Context) {
var chars []byte
id := path.Base(ctx.Request.RequestURI)
if i := strings.Index(id, "."); i != -1 {
id = id[:i]
}
key := c.key(id)
if len(ctx.Input.Query("reload")) > 0 {
chars = c.genRandChars()
if err := c.store.Put(key, chars, c.Expiration); err != nil {
ctx.Output.SetStatus(500)
ctx.WriteString("captcha reload error")
2016-03-25 02:56:15 +00:00
logs.Error("Reload Create Captcha Error:", err)
2014-01-16 12:53:35 +00:00
return
}
} else {
if v, ok := c.store.Get(key).([]byte); ok {
chars = v
} else {
ctx.Output.SetStatus(404)
ctx.WriteString("captcha not found")
return
}
2014-01-16 12:53:35 +00:00
}
img := NewImage(chars, c.StdWidth, c.StdHeight)
if _, err := img.WriteTo(ctx.ResponseWriter); err != nil {
2016-03-25 02:56:15 +00:00
logs.Error("Write Captcha Image Error:", err)
2014-01-16 12:53:35 +00:00
}
}
2016-01-17 15:48:17 +00:00
// CreateCaptchaHTML template func for output html
2015-09-14 15:13:51 +00:00
func (c *Captcha) CreateCaptchaHTML() template.HTML {
2014-01-16 12:53:35 +00:00
value, err := c.CreateCaptcha()
if err != nil {
2016-03-25 02:56:15 +00:00
logs.Error("Create Captcha Error:", err)
2014-01-16 12:53:35 +00:00
return ""
}
// create 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">`+
2015-09-14 15:13:51 +00:00
`</a>`, c.FieldIDName, value, c.URLPrefix, value, c.URLPrefix, value))
2014-01-16 12:53:35 +00:00
}
2015-09-14 15:13:51 +00:00
// CreateCaptcha create a new captcha id
2014-01-16 12:53:35 +00:00
func (c *Captcha) CreateCaptcha() (string, error) {
// generate captcha id
id := string(utils.RandomCreateBytes(15))
// get the captcha chars
chars := c.genRandChars()
// save to store
if err := c.store.Put(c.key(id), chars, c.Expiration); err != nil {
return "", err
}
return id, nil
}
2015-09-14 15:13:51 +00:00
// VerifyReq verify from a request
2014-01-16 12:53:35 +00:00
func (c *Captcha) VerifyReq(req *http.Request) bool {
req.ParseForm()
2015-09-14 15:13:51 +00:00
return c.Verify(req.Form.Get(c.FieldIDName), req.Form.Get(c.FieldCaptchaName))
2014-01-16 12:53:35 +00:00
}
2015-09-14 15:13:51 +00:00
// Verify direct verify id and challenge string
2014-01-16 12:53:35 +00:00
func (c *Captcha) Verify(id string, challenge string) (success bool) {
if len(challenge) == 0 || len(id) == 0 {
return
}
var chars []byte
key := c.key(id)
if v, ok := c.store.Get(key).([]byte); ok {
2014-01-16 12:53:35 +00:00
chars = v
} else {
return
}
defer func() {
// finally remove it
c.store.Delete(key)
}()
if len(chars) != len(challenge) {
return
}
2014-01-16 12:53:35 +00:00
// verify challenge
for i, c := range chars {
if c != challenge[i]-48 {
return
}
}
return true
}
2015-09-14 15:13:51 +00:00
// NewCaptcha create a new captcha.Captcha
2014-01-16 12:53:35 +00:00
func NewCaptcha(urlPrefix string, store cache.Cache) *Captcha {
cpt := &Captcha{}
cpt.store = store
2015-09-14 15:13:51 +00:00
cpt.FieldIDName = fieldIDName
2014-01-16 12:53:35 +00:00
cpt.FieldCaptchaName = fieldCaptchaName
cpt.ChallengeNums = challengeNums
cpt.Expiration = expiration
cpt.CachePrefix = cachePrefix
cpt.StdWidth = stdWidth
cpt.StdHeight = stdHeight
if len(urlPrefix) == 0 {
2014-02-20 05:44:34 +00:00
urlPrefix = defaultURLPrefix
2014-01-16 12:53:35 +00:00
}
if urlPrefix[len(urlPrefix)-1] != '/' {
urlPrefix += "/"
}
2014-02-20 05:44:34 +00:00
cpt.URLPrefix = urlPrefix
2014-01-16 12:53:35 +00:00
return cpt
}
2015-09-14 15:13:51 +00:00
// NewWithFilter create a new captcha.Captcha and auto AddFilter for serve captacha image
2016-01-17 15:48:17 +00:00
// and add a template func for output html
2014-01-16 12:53:35 +00:00
func NewWithFilter(urlPrefix string, store cache.Cache) *Captcha {
cpt := NewCaptcha(urlPrefix, store)
// create filter for serve captcha image
2014-06-19 12:29:36 +00:00
beego.InsertFilter(cpt.URLPrefix+"*", beego.BeforeRouter, cpt.Handler)
2014-01-16 12:53:35 +00:00
// add to template func map
2016-01-18 07:27:33 +00:00
beego.AddFuncMap("create_captcha", cpt.CreateCaptchaHTML)
2014-01-16 12:53:35 +00:00
return cpt
}