mirror of
https://github.com/ssep1ol/overleaf-registration-be.git
synced 2026-07-21 20:40:21 +00:00
First commit
This commit is contained in:
@@ -34,7 +34,7 @@ func registerHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate reCAPTCHA token
|
// Validate Turnstile token
|
||||||
valid, err := validateRecaptcha(req.Captcha)
|
valid, err := validateRecaptcha(req.Captcha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("CAPTCHA validation failed: %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("CAPTCHA validation failed: %v", err), http.StatusInternalServerError)
|
||||||
|
|||||||
18
recaptcha.go
18
recaptcha.go
@@ -8,36 +8,36 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func validateRecaptcha(captchaToken string) (bool, error) {
|
func validateRecaptcha(token string) (bool, error) {
|
||||||
secret := os.Getenv("CAPTCHA_SERVER_KEY")
|
secret := os.Getenv("TURNSTILE_SECRET_KEY")
|
||||||
if secret == "" {
|
if secret == "" {
|
||||||
return false, fmt.Errorf("CAPTCHA_SERVER_KEY is not set")
|
return false, fmt.Errorf("TURNSTILE_SECRET_KEY is not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
url := "https://www.google.com/recaptcha/api/siteverify"
|
url := "https://challenges.cloudflare.com/turnstile/v0/siteverify"
|
||||||
payload := map[string]string{
|
payload := map[string]string{
|
||||||
"secret": secret,
|
"secret": secret,
|
||||||
"response": captchaToken,
|
"response": token,
|
||||||
}
|
}
|
||||||
payloadBytes, _ := json.Marshal(payload)
|
payloadBytes, _ := json.Marshal(payload)
|
||||||
|
|
||||||
resp, err := http.Post(url, "application/json", bytes.NewBuffer(payloadBytes))
|
resp, err := http.Post(url, "application/json", bytes.NewBuffer(payloadBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("reCAPTCHA API request failed: %v", err)
|
return false, fmt.Errorf("Turnstile API request failed: %v", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
var result map[string]interface{}
|
var result map[string]interface{}
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||||
return false, fmt.Errorf("failed to parse reCAPTCHA API response: %v", err)
|
return false, fmt.Errorf("Failed to parse Turnstile API response: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
success, ok := result["success"].(bool)
|
success, ok := result["success"].(bool)
|
||||||
if !ok || !success {
|
if !ok || !success {
|
||||||
if errors, exists := result["error-codes"]; exists {
|
if errors, exists := result["error-codes"]; exists {
|
||||||
fmt.Printf("reCAPTCHA error codes: %+v\n", errors)
|
return false, fmt.Errorf("Turnstile validation failed: %v", errors)
|
||||||
}
|
}
|
||||||
return false, fmt.Errorf("reCAPTCHA validation failed")
|
return false, fmt.Errorf("Turnstile validation failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, nil
|
return true, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user