-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptcha.go
More file actions
42 lines (36 loc) · 859 Bytes
/
captcha.go
File metadata and controls
42 lines (36 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"encoding/json"
"log"
"net/http"
"net/url"
"os"
)
func verifyHCaptcha(token string) bool {
secret := os.Getenv("HCAPTCHA_SECRET")
if secret == "" {
log.Println("⚠️ HCAPTCHA_SECRET not set in environment")
return false
}
form := url.Values{}
form.Add("secret", secret)
form.Add("response", token)
resp, err := http.PostForm("https://hcaptcha.com/siteverify", form)
if err != nil {
log.Println("hCaptcha request failed:", err)
return false
}
defer resp.Body.Close()
var result struct {
Success bool `json:"success"`
ErrorCodes []string `json:"error-codes"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
log.Println("hCaptcha decode error:", err)
return false
}
if !result.Success {
log.Println("hCaptcha failed:", result.ErrorCodes)
}
return result.Success
}