package main import ( "bytes" "errors" "fmt" "io" "net/http" "net/url" "strings" "github.com/PuerkitoBio/goquery" ) type Overleaf struct { BaseURL string Client *http.Client CSRF string } // NewOverleaf initializes a new Overleaf instance func NewOverleaf(baseURL string) *Overleaf { return &Overleaf{ BaseURL: strings.TrimSuffix(baseURL, "/"), Client: &http.Client{}, } } // get performs a GET request func (o *Overleaf) get(endpoint string) (*http.Response, error) { fullURL := fmt.Sprintf("%s%s", o.BaseURL, endpoint) resp, err := o.Client.Get(fullURL) if err != nil { return nil, fmt.Errorf("GET request to %s failed: %w", fullURL, err) } if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) return nil, fmt.Errorf("GET request to %s returned status %d: %s", fullURL, resp.StatusCode, string(body)) } return resp, nil } // post performs a POST request func (o *Overleaf) post(endpoint string, data map[string]string) (*http.Response, error) { if o.CSRF == "" { if err := o.obtainCSRF(); err != nil { return nil, fmt.Errorf("failed to obtain CSRF token: %w", err) } } fullURL := fmt.Sprintf("%s%s", o.BaseURL, endpoint) data["_csrf"] = o.CSRF formData := encodeFormData(data) req, err := http.NewRequest("POST", fullURL, bytes.NewBufferString(formData)) if err != nil { return nil, fmt.Errorf("failed to create POST request: %w", err) } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") resp, err := o.Client.Do(req) if err != nil { return nil, fmt.Errorf("POST request to %s failed: %w", fullURL, err) } return resp, nil } // obtainCSRF retrieves the CSRF token func (o *Overleaf) obtainCSRF() error { resp, err := o.get("/login") if err != nil { return err } defer resp.Body.Close() doc, err := goquery.NewDocumentFromReader(resp.Body) if err != nil { return fmt.Errorf("failed to parse HTML document: %w", err) } csrf, exists := doc.Find(`meta[name="ol-csrfToken"]`).Attr("content") if !exists { return errors.New("CSRF token not found in the login page") } o.CSRF = csrf return nil } // Login logs in as the admin user func (o *Overleaf) Login(email, password string) error { resp, err := o.post("/login", map[string]string{ "email": email, "password": password, }) if err != nil { return fmt.Errorf("admin login failed: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) return fmt.Errorf("admin login returned status %d: %s", resp.StatusCode, string(body)) } return nil } // RegisterUser registers a new user func (o *Overleaf) RegisterUser(email string) error { resp, err := o.post("/admin/register", map[string]string{ "email": email, }) if err != nil { return fmt.Errorf("failed to register user: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) return fmt.Errorf("register user returned status %d: %s", resp.StatusCode, string(body)) } return nil } // encodeFormData encodes a map into x-www-form-urlencoded format func encodeFormData(data map[string]string) string { formData := url.Values{} for key, value := range data { formData.Set(key, value) } return formData.Encode() }