First commit

This commit is contained in:
corrado.mulas
2024-11-19 23:59:40 +01:00
parent 36fa1d9db4
commit 50296df78f
4 changed files with 83 additions and 58 deletions

41
recaptcha.go Normal file
View File

@@ -0,0 +1,41 @@
package main
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"os"
)
func validateRecaptcha(token string) (bool, error) {
secret := os.Getenv("RECAPTCHA_SECRET_KEY")
if secret == "" {
return false, errors.New("missing RECAPTCHA_SECRET_KEY")
}
url := "https://www.google.com/recaptcha/api/siteverify"
payload := map[string]string{
"secret": secret,
"response": token,
}
jsonPayload, _ := json.Marshal(payload)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonPayload))
if err != nil {
return false, err
}
defer resp.Body.Close()
var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return false, err
}
success, ok := result["success"].(bool)
if !ok {
return false, errors.New("invalid reCAPTCHA response format")
}
return success, nil
}