package main import ( "encoding/json" "net/http" "os" "strings" ) type RegisterRequest struct { Email string `json:"email"` Password string `json:"password"` Captcha string `json:"captcha"` } func RegisterHandler(w http.ResponseWriter, r *http.Request) { 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 { 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) return } // Validate CAPTCHA if !ValidateCaptcha(req.Captcha) { 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) return } err = overleaf.RegisterUser(req.Email, req.Password) if err != nil { http.Error(w, "Failed to register user: "+err.Error(), 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 }