(logger zerolog.Logger, sysConfigVO vo.FullSysConfigVO, token string)
| 68 | } |
| 69 | |
| 70 | func checkGoogleRecaptcha(logger zerolog.Logger, sysConfigVO vo.FullSysConfigVO, token string) error { |
| 71 | if sysConfigVO.EnableGoogleRecaptcha { |
| 72 | if token == "" { |
| 73 | return errors.New("token必填") |
| 74 | } |
| 75 | params := url.Values{} |
| 76 | params.Set("secret", sysConfigVO.GoogleSecretKey) |
| 77 | params.Set("response", token) |
| 78 | |
| 79 | response, err := http.Post("https://recaptcha.net/recaptcha/api/siteverify?"+params.Encode(), "", nil) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | defer response.Body.Close() |
| 84 | |
| 85 | if response.StatusCode != http.StatusOK { |
| 86 | return errors.New("google验证服务无法正常返回") |
| 87 | } |
| 88 | resp, err := io.ReadAll(response.Body) |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | logger.Info().Str("Action", "评论").Msgf("google resp: %s", resp) |
| 93 | |
| 94 | var result map[string]any |
| 95 | err = json.Unmarshal(resp, &result) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | if success, ok := result["success"].(bool); ok { |
| 100 | if success { |
| 101 | if score, ok := result["score"].(float64); ok { |
| 102 | if score > 0.5 { |
| 103 | return nil |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | return errors.New("人机校验不通过") |
| 109 | } |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | // AddComment godoc |
| 114 | // |
no test coverage detected