First commit

This commit is contained in:
corrado.mulas
2024-11-19 22:32:50 +01:00
parent 21d8aea921
commit a2e4ad87db
7 changed files with 268 additions and 0 deletions

32
captcha.go Normal file
View File

@@ -0,0 +1,32 @@
package main
import (
"encoding/json"
"net/http"
"net/url"
"os"
)
type CaptchaResponse struct {
Success bool `json:"success"`
}
func ValidateCaptcha(captcha string) bool {
secretKey := os.Getenv("CAPTCHA_SERVER_KEY")
if secretKey == "" {
return true // Skip validation if CAPTCHA is disabled
}
resp, err := http.PostForm("https://www.google.com/recaptcha/api/siteverify", url.Values{
"secret": {secretKey},
"response": {captcha},
})
if err != nil {
return false
}
defer resp.Body.Close()
var result CaptchaResponse
err = json.NewDecoder(resp.Body).Decode(&result)
return err == nil && result.Success
}