From 9d22e4d3a3b7b790e838b6d0bf62e9d5d9f9b459 Mon Sep 17 00:00:00 2001 From: "corrado.mulas" Date: Wed, 20 Nov 2024 00:17:54 +0100 Subject: [PATCH] First commit --- handlers.go | 71 ++++++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/handlers.go b/handlers.go index b3db6c7..690d8e9 100644 --- a/handlers.go +++ b/handlers.go @@ -2,72 +2,59 @@ package main import ( "encoding/json" + "fmt" "net/http" - "os" - "strings" ) -type RegisterRequest struct { - Email string `json:"email"` - Password string `json:"password"` - Captcha string `json:"captcha"` +// RegistrationRequest represents the incoming registration request. +type RegistrationRequest struct { + Email string `json:"email"` + Captcha string `json:"captcha"` } -func RegisterHandler(w http.ResponseWriter, r *http.Request) { +func registerHandler(w http.ResponseWriter, r *http.Request) { + // Allow only POST method if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } - // Parse the request - var req RegisterRequest - err := json.NewDecoder(r.Body).Decode(&req) - if err != nil { + // Parse the request body into the RegistrationRequest struct + var req RegistrationRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, "Invalid request body", http.StatusBadRequest) return } - // Validate email domain - allowedDomains := strings.Split(os.Getenv("ALLOWED_DOMAINS"), ",") - if !isDomainAllowed(req.Email, allowedDomains) { - http.Error(w, "Email domain not allowed", http.StatusForbidden) + // Validate reCAPTCHA token + valid, err := validateRecaptcha(req.Captcha) + if err != nil { + http.Error(w, fmt.Sprintf("CAPTCHA validation failed: %v", err), http.StatusInternalServerError) return } - - // Validate CAPTCHA - if !ValidateCaptcha(req.Captcha) { + if !valid { http.Error(w, "Invalid CAPTCHA", http.StatusUnauthorized) return } - // Register user - overleaf := NewOverleaf(os.Getenv("OL_INSTANCE")) - err = overleaf.Login(os.Getenv("OL_ADMIN_EMAIL"), os.Getenv("OL_ADMIN_PASSWORD")) - if err != nil { - http.Error(w, "Failed to log in as admin", http.StatusInternalServerError) + // Initialize Overleaf instance + overleaf := NewOverleaf("http://localhost:3000") // Replace with the actual Overleaf base URL + + // Optional: Log in as admin (if needed for user creation) + adminEmail := "admin@example.com" // Replace with actual admin email + adminPassword := "adminpassword" // Replace with actual admin password + if err := overleaf.Login(adminEmail, adminPassword); err != nil { + http.Error(w, fmt.Sprintf("Admin login failed: %v", err), http.StatusInternalServerError) return } - err = overleaf.RegisterUser(req.Email, req.Password) - if err != nil { - http.Error(w, "Failed to register user: "+err.Error(), http.StatusInternalServerError) + // Create the user in Overleaf + if err := overleaf.RegisterUser(req.Email); err != nil { + http.Error(w, fmt.Sprintf("Failed to create user: %v", err), http.StatusInternalServerError) return } - w.WriteHeader(http.StatusCreated) - json.NewEncoder(w).Encode(map[string]string{"message": "User registered successfully"}) -} - -func isDomainAllowed(email string, allowedDomains []string) bool { - parts := strings.Split(email, "@") - if len(parts) != 2 { - return false - } - domain := parts[1] - for _, allowed := range allowedDomains { - if domain == allowed { - return true - } - } - return false + // Respond with success + w.WriteHeader(http.StatusOK) + w.Write([]byte("User registered successfully. Please check your email to set your password.")) }