本文整理汇总了Golang中github.com/dchest/captcha.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: showForm
func showForm(wr *web.Context) (captid *CaptchaId) {
// Patch until set another secure cookie works again
// Generate its id as string
captitle := captcha.New()
// Generate captcha image removing previous
dir, err := ioutil.ReadDir("static/images/captchas")
if err == nil {
for i := range dir {
os.Remove("static/images/captchas/" + dir[i].Name())
}
}
captbyte := gen("static/images/captchas/" + captitle + "-.png")
//captbyte = gen("static/images/captchas/" + captid.CaptId + "-.png")
//wr.SetSecureCookie("captstr", captbyte, 20000)
// Adding captcha sequence as string to struct
captid = &CaptchaId{
CaptId: captitle,
CaptByte: captbyte,
}
log.Println("DEBUG Captcha: Created new captcha ", captid.CaptId)
return captid
}
示例2: NewCaptcha
// generate a new captcha
func (cs *CaptchaServer) NewCaptcha(w http.ResponseWriter, r *http.Request) {
// obtain session
sess, err := cs.store.Get(r, cs.sessionName)
if err != nil {
// failed to obtain session
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// new captcha
id := captcha.New()
// do we want to interpret as json?
use_json := r.URL.Query().Get("t") == "json"
// image url
url := fmt.Sprintf("%simg/%s.png", cs.prefix, id)
if use_json {
// send json
enc := json.NewEncoder(w)
enc.Encode(map[string]string{"id": id, "url": url})
} else {
// set captcha id
sess.Values["captcha_id"] = id
// save session
sess.Save(r, w)
// rediect to image
http.Redirect(w, r, url, http.StatusFound)
}
}
示例3: new_captcha_json
// create a new captcha, return as json object
func (self httpFrontend) new_captcha_json(wr http.ResponseWriter, r *http.Request) {
captcha_id := captcha.New()
resp := make(map[string]string)
// the captcha id
resp["id"] = captcha_id
// url of the image
resp["url"] = fmt.Sprintf("%s%s.png", self.prefix, captcha_id)
enc := json.NewEncoder(wr)
enc.Encode(resp)
}
示例4: NewCaptcha
func NewCaptcha(db *periwinkle.Tx) *Captcha {
o := Captcha{
ID: captcha.New(),
Value: string(captcha.RandomDigits(defaultLen)),
}
if err := db.Create(&o).Error; err != nil {
dbError(err)
}
return &o
}
示例5: New
// New creates a new capcha and returns the ID
func New() Captcha {
d := struct {
id string
}{
captcha.New(),
}
return Captcha{
id: d.id,
}
}
示例6: captchaServer
func captchaServer(w http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "%s", captcha.New())
return
} else if req.Method == "POST" {
if captcha.VerifyString(req.FormValue("captchaId"), req.FormValue("captchaSolution")) {
} else {
}
}
}
示例7: newCaptcha
func newCaptcha(writer http.ResponseWriter, request *http.Request) {
clientId := request.URL.Query().Get("clientId")
privateKey := userStore.Get(clientId, false)
if privateKey == "" {
writer.WriteHeader(401)
return
}
captchaId := captcha.New()
validationStore.Set(captchaId, clientId)
io.WriteString(writer, captchaId)
}
示例8: Login
func (a Auth) Login() revel.Result {
a.RenderArgs["needCaptcha"] = "true"
a.RenderArgs["openRegister"] = "true"
Captcha := struct {
CaptchaId string
}{
captcha.New(),
}
a.RenderArgs["Captcha"] = Captcha
log.Println("captchaId:", Captcha.CaptchaId)
return a.RenderTemplate("home/login.html")
}
示例9: RegisterForm
func RegisterForm(w http.ResponseWriter, req *http.Request, ctx *models.Context) (err error) {
if ctx.User != nil {
http.Redirect(w, req, reverse("logout"), http.StatusSeeOther)
return nil
}
ctx.Data["title"] = "Register"
ctx.Data["cap"] = captcha.New()
return T("register.html").Execute(w, map[string]interface{}{
"ctx": ctx,
"fbLoginLink": FbConfig().AuthCodeURL(models.GenUUID()),
"glLoginLink": GlConfig().AuthCodeURL(models.GenUUID()),
})
}
示例10: showFormHandler
func showFormHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
d := struct {
CaptchaId string
}{
captcha.New(),
}
if err := formTemplate.Execute(w, &d); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
示例11: GetImageCaptcha
func GetImageCaptcha(c *gin.Context) {
session := sessions.Default(c)
captchaId := captcha.New()
bytes := captcha.RandomDigits(4)
captcheString := util.GetCaptchaString(bytes)
session.Set(constant.LOGIN_CAPTCHA, captcheString)
Logger.Debug("new captcha:%v", captcheString)
session.Save()
image := captcha.NewImage(captchaId, bytes, captcha.StdWidth/2, captcha.StdHeight/2)
_, err := image.WriteTo(c.Writer)
if err != nil {
Logger.Error("error occured when writing captcha image to response")
}
}
示例12: new_captcha
func (self httpFrontend) new_captcha(wr http.ResponseWriter, r *http.Request) {
s, err := self.store.Get(r, self.name)
if err == nil {
captcha_id := captcha.New()
s.Values["captcha_id"] = captcha_id
s.Save(r, wr)
redirect_url := fmt.Sprintf("%scaptcha/%s.png", self.prefix, captcha_id)
// redirect to the image
http.Redirect(wr, r, redirect_url, 302)
} else {
// todo: send a "this is broken" image
wr.WriteHeader(500)
}
}
示例13: Challenge
func (self *CaptchaContext) Challenge(w http.ResponseWriter, r *http.Request, edge string) *ProxyError {
captchaId := r.PostFormValue("captchaId")
solution := r.PostFormValue("captchaSolution")
// Verify the input.
if captcha.VerifyString(captchaId, solution) {
token, err := self.GenerateToken()
if err != nil {
return &ProxyError{
Code: 500,
Msg: "Unable to generate token value.",
Err: err,
}
}
// Strip the port off, since I cannot use them in the cookie.
parts := strings.Split(edge, ":")
http.SetCookie(w, &http.Cookie{
Name: HumanCookieName,
Value: token,
Expires: time.Now().Add(self.ValidDuration),
Domain: "." + parts[0],
Path: "/",
})
http.Redirect(w, r, r.URL.Path, 302)
return nil
}
// Deal with displaying the Captcha.
if strings.HasPrefix(r.URL.Path, "/captcha/") {
self.Generator.ServeHTTP(w, r)
return nil
} else {
if err := captchaTemplate.Execute(w, &struct{ CaptchaId string }{captcha.New()}); err != nil {
return &ProxyError{
Code: 500,
Msg: "Unable to generate captcha page.",
Err: err,
}
}
return nil
}
}
示例14: renderPost
// renderPost builds on 'render' but is specifically for showing a post's page.
// Namely, it handles the population of the "add comment" form.
func renderPost(w http.ResponseWriter, post *Post,
formError, formAuthor, formEmail, formComment string) {
render(w, "post",
struct {
*Post
FormCaptchaId string
FormError string
FormAuthor string
FormEmail string
FormComment string
}{
Post: post,
FormCaptchaId: captcha.New(),
FormError: formError,
FormAuthor: formAuthor,
FormEmail: formEmail,
FormComment: formComment,
})
}
示例15: signinHandler
// URL: /signin
// 处理用户登录,如果登录成功,设置Cookie
func signinHandler(handler *Handler) {
// 如果已经登录了,跳转到首页
_, has := currentUser(handler)
if has {
handler.Redirect("/")
}
next := handler.Request.FormValue("next")
form := wtforms.NewForm(
wtforms.NewHiddenField("next", next),
wtforms.NewTextField("username", "用户名", "", &wtforms.Required{}),
wtforms.NewPasswordField("password", "密码", &wtforms.Required{}),
wtforms.NewTextField("captcha", "验证码", "", wtforms.Required{}),
wtforms.NewHiddenField("captchaId", ""),
)
if handler.Request.Method == "POST" {
if form.Validate(handler.Request) {
// 检查验证码
if !captcha.VerifyString(form.Value("captchaId"), form.Value("captcha")) {
form.AddError("captcha", "验证码错误")
form.SetValue("captcha", "")
handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
return
}
c := handler.DB.C(USERS)
user := User{}
err := c.Find(bson.M{"username": form.Value("username")}).One(&user)
if err != nil {
form.AddError("username", "该用户不存在")
form.SetValue("captcha", "")
handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
return
}
if !user.IsActive {
form.AddError("username", "邮箱没有经过验证,如果没有收到邮件,请联系管理员")
form.SetValue("captcha", "")
handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
return
}
if user.Password != encryptPassword(form.Value("password"), user.Salt) {
form.AddError("password", "密码和用户名不匹配")
form.SetValue("captcha", "")
handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
return
}
session, _ := store.Get(handler.Request, "user")
session.Values["username"] = user.Username
session.Save(handler.Request, handler.ResponseWriter)
if form.Value("next") == "" {
http.Redirect(handler.ResponseWriter, handler.Request, "/", http.StatusFound)
} else {
http.Redirect(handler.ResponseWriter, handler.Request, next, http.StatusFound)
}
return
}
}
form.SetValue("captcha", "")
handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
}