File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ package  utils
2+ 
3+ import  (
4+ "encoding/json" 
5+ "errors" 
6+ "github.com/blobs-io/blobsgame/utils/config" 
7+ "io/ioutil" 
8+ "net/http" 
9+ )
10+ 
11+ const  (
12+ RecaptchaEndpoint  =  "https://www.google.com/recaptcha/api/siteverify" 
13+ )
14+ 
15+ type  RecaptchaResponseBody  struct  {
16+ Success  bool  `json:"success"` 
17+ ChallengeTimestamp  string  `json:"challenge_ts"` 
18+ Hostname  string  `json:"hostname"` 
19+ Score  float32  `json:"score"` 
20+ Action  string  `json:"action"` 
21+ }
22+ 
23+ func  RateCaptcha (token  string ) (RecaptchaResponseBody , error ) {
24+ req , err  :=  http .Get (RecaptchaEndpoint  +  "?secret="  +  config .MainConfig .Tokens .Recaptcha  +  "&response="  +  token )
25+ if  err  !=  nil  {
26+ return  RecaptchaResponseBody {}, err 
27+ }
28+ defer  req .Body .Close ()
29+ if  req .StatusCode  >=  400  {
30+ return  RecaptchaResponseBody {}, errors .New ("an unknown error occurred" )
31+ }
32+ 
33+ resp , err  :=  ioutil .ReadAll (req .Body )
34+ if  err  !=  nil  {
35+ return  RecaptchaResponseBody {}, err 
36+ }
37+ 
38+ var  respBody  RecaptchaResponseBody 
39+ err  =  json .Unmarshal (resp , & respBody )
40+ if  err  !=  nil  {
41+ return  RecaptchaResponseBody {}, err 
42+ }
43+ 
44+ return  respBody , nil 
45+ }
46+ 
47+ func  ValidateCaptcha (response  RecaptchaResponseBody ) bool  {
48+ return  response .Success  &&  response .Score  >=  .6 
49+ }
                         You can’t perform that action at this time. 
           
                  
0 commit comments