Files
overleaf-registration-be/captcha.go
corrado.mulas a2e4ad87db First commit
2024-11-19 22:32:50 +01:00

33 lines
633 B
Go

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
}