First commit

This commit is contained in:
corrado.mulas
2024-11-19 23:59:40 +01:00
parent 36fa1d9db4
commit 50296df78f
4 changed files with 83 additions and 58 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
@@ -27,29 +28,41 @@ func NewOverleaf(baseURL string) *Overleaf {
// get performs a GET request
func (o *Overleaf) get(endpoint string) (*http.Response, error) {
url := fmt.Sprintf("%s%s", o.BaseURL, endpoint)
return o.Client.Get(url)
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, err
return nil, fmt.Errorf("failed to obtain CSRF token: %w", err)
}
}
url := fmt.Sprintf("%s%s", o.BaseURL, endpoint)
fullURL := fmt.Sprintf("%s%s", o.BaseURL, endpoint)
data["_csrf"] = o.CSRF
formData := encodeFormData(data)
req, err := http.NewRequest("POST", url, bytes.NewBufferString(formData))
req, err := http.NewRequest("POST", fullURL, bytes.NewBufferString(formData))
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create POST request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return o.Client.Do(req)
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
@@ -62,30 +75,31 @@ func (o *Overleaf) obtainCSRF() error {
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return err
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")
return errors.New("CSRF token not found in the login page")
}
o.CSRF = csrf
return nil
}
// Login as admin
// 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 err
return fmt.Errorf("admin login failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.New("failed to log in as admin")
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("admin login returned status %d: %s", resp.StatusCode, string(body))
}
return nil
}
@@ -96,22 +110,22 @@ func (o *Overleaf) RegisterUser(email string) error {
"email": email,
})
if err != nil {
return err
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("failed to register user: %s", string(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 {
var formData []string
formData := url.Values{}
for key, value := range data {
formData = append(formData, fmt.Sprintf("%s=%s", key, value))
formData.Set(key, value)
}
return strings.Join(formData, "&")
return formData.Encode()
}