First commit

This commit is contained in:
corrado.mulas
2024-11-20 03:33:07 +01:00
parent fb8b2ccfee
commit 207ed263cd

View File

@@ -2,10 +2,11 @@ package main
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"net/http/cookiejar"
"net/url" "net/url"
"strings" "strings"
@@ -18,11 +19,16 @@ type Overleaf struct {
CSRF string CSRF string
} }
// NewOverleaf initializes a new Overleaf instance
func NewOverleaf(baseURL string) *Overleaf { func NewOverleaf(baseURL string) *Overleaf {
// Create a new HTTP client with a cookie jar
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
}
return &Overleaf{ return &Overleaf{
BaseURL: strings.TrimSuffix(baseURL, "/"), BaseURL: strings.TrimSuffix(baseURL, "/"),
Client: &http.Client{}, Client: client,
} }
} }
@@ -65,42 +71,57 @@ func (o *Overleaf) post(endpoint string, data map[string]string) (*http.Response
return resp, nil return resp, nil
} }
// obtainCSRF retrieves the CSRF token
func (o *Overleaf) obtainCSRF() error { func (o *Overleaf) obtainCSRF() error {
// Perform a GET request to retrieve the login page
resp, err := o.get("/login") resp, err := o.get("/login")
if err != nil { if err != nil {
return err return fmt.Errorf("failed to load login page: %v", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
// Parse the HTML to find the CSRF token
doc, err := goquery.NewDocumentFromReader(resp.Body) doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil { if err != nil {
return fmt.Errorf("failed to parse HTML document: %w", err) return fmt.Errorf("failed to parse login page HTML: %v", err)
} }
csrf, exists := doc.Find(`meta[name="ol-csrfToken"]`).Attr("content") csrf, exists := doc.Find(`meta[name="ol-csrfToken"]`).Attr("content")
if !exists { if !exists {
return errors.New("CSRF token not found in the login page") return fmt.Errorf("CSRF token not found in login page")
} }
o.CSRF = csrf o.CSRF = csrf
return nil return nil
} }
// Login logs in as the admin user
func (o *Overleaf) Login(email, password string) error { func (o *Overleaf) Login(email, password string) error {
// Retrieve CSRF token
if err := o.obtainCSRF(); err != nil {
return fmt.Errorf("failed to obtain CSRF token: %v", err)
}
// Log the CSRF token for debugging
log.Printf("Obtained CSRF token: %s", o.CSRF)
// Create the login request
resp, err := o.post("/login", map[string]string{ resp, err := o.post("/login", map[string]string{
"email": email, "email": email,
"password": password, "password": password,
"_csrf": o.CSRF, // Include the CSRF token
}) })
if err != nil { if err != nil {
return fmt.Errorf("admin login failed: %w", err) return fmt.Errorf("failed to send login request: %v", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { // Log response for debugging
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
log.Printf("Login response: %s", string(body))
return fmt.Errorf("admin login returned status %d: %s", resp.StatusCode, string(body)) return fmt.Errorf("admin login returned status %d: %s", resp.StatusCode, string(body))
} }
log.Println("Admin login successful.")
return nil return nil
} }