func()

in captcha-google-v2/recaptcha.go [86:117]


func (c *Captcha) Verify(captcha, userInput string) (pass bool) {
	if len(userInput) == 0 {
		return false
	}
	cli := &http.Client{}
	cli.Timeout = 10 * time.Second
	siteVerifyEndpoint := c.Config.SiteVerifyEndpoint
	if siteVerifyEndpoint == "" {
		siteVerifyEndpoint = "https://www.google.com/recaptcha/api/siteverify"
	}
	resp, err := cli.PostForm(siteVerifyEndpoint, map[string][]string{
		"secret":   {c.Config.SecretKey},
		"response": {userInput},
	})
	if err != nil {
		log.Errorf("verify captcha error %s", err.Error())
		return false
	}
	defer resp.Body.Close()
	all, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Errorf("verify captcha, read body error %s", err.Error())
		return false
	}
	r := &GoogleCaptchaResponse{}
	_ = json.Unmarshal(all, r)
	if r.Success {
		return true
	}
	log.Debugf("user input is wrong %s, user input is %s", string(all), userInput)
	return false
}