First commit

This commit is contained in:
corrado.mulas
2024-11-20 04:02:59 +01:00
parent 456575818e
commit d43964d565

View File

@@ -19,10 +19,11 @@ type Overleaf struct {
CSRF string CSRF string
} }
// NewOverleaf initializes a new Overleaf instance with a cookie jar
func NewOverleaf(baseURL string) *Overleaf { func NewOverleaf(baseURL string) *Overleaf {
jar, _ := cookiejar.New(nil) // Create a cookie jar to store cookies jar, _ := cookiejar.New(nil) // Initialize cookie jar
client := &http.Client{ client := &http.Client{
Jar: jar, // Attach the cookie jar to the client Jar: jar, // Attach cookie jar to client
} }
return &Overleaf{ return &Overleaf{
@@ -31,20 +32,26 @@ func NewOverleaf(baseURL string) *Overleaf {
} }
} }
// get performs a GET request // logCookies logs cookies stored in the client's cookie jar for a given endpoint
func (o *Overleaf) get(endpoint string) (*http.Response, error) { func logCookies(client *http.Client, endpoint string) {
fullURL := fmt.Sprintf("%s%s", o.BaseURL, endpoint) u, _ := url.Parse(endpoint)
resp, err := o.Client.Get(fullURL) cookies := client.Jar.Cookies(u)
if err != nil { for _, cookie := range cookies {
return nil, fmt.Errorf("GET request to %s failed: %w", fullURL, err) log.Printf("Cookie for %s: %s=%s", endpoint, cookie.Name, cookie.Value)
} }
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
} }
// get performs a GET request
func (o *Overleaf) get(endpoint string) (*http.Response, error) {
url := fmt.Sprintf("%s%s", o.BaseURL, endpoint)
resp, err := o.Client.Get(url)
if err == nil {
logCookies(o.Client, url) // Log cookies after GET request
}
return resp, err
}
// post performs a POST request
func (o *Overleaf) post(endpoint string, data map[string]string) (*http.Response, error) { func (o *Overleaf) post(endpoint string, data map[string]string) (*http.Response, error) {
url := fmt.Sprintf("%s%s", o.BaseURL, endpoint) url := fmt.Sprintf("%s%s", o.BaseURL, endpoint)
data["_csrf"] = o.CSRF data["_csrf"] = o.CSRF
@@ -56,24 +63,21 @@ func (o *Overleaf) post(endpoint string, data map[string]string) (*http.Response
} }
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Log cookies for debugging resp, err := o.Client.Do(req)
cookies := o.Client.Jar.Cookies(req.URL) if err == nil {
for _, cookie := range cookies { logCookies(o.Client, url) // Log cookies after POST request
log.Printf("Cookie sent to %s: %s=%s", endpoint, cookie.Name, cookie.Value) }
} return resp, err
return o.Client.Do(req)
} }
// 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 fmt.Errorf("failed to load login page: %v", 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 login page HTML: %v", err) return fmt.Errorf("failed to parse login page HTML: %v", err)
@@ -85,32 +89,27 @@ func (o *Overleaf) obtainCSRF() error {
} }
o.CSRF = csrf o.CSRF = csrf
log.Printf("Obtained CSRF token: %s", o.CSRF)
return nil return nil
} }
// Login authenticates as admin
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 { if err := o.obtainCSRF(); err != nil {
return fmt.Errorf("failed to obtain CSRF token: %v", err) 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("failed to send login request: %v", err) return fmt.Errorf("failed to send login request: %v", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
// Log response for debugging
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
log.Printf("Login response: %s", string(body)) 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))
} }
@@ -119,13 +118,13 @@ func (o *Overleaf) Login(email, password string) error {
return nil return nil
} }
// RegisterUser registers a new user
func (o *Overleaf) RegisterUser(email string) error { func (o *Overleaf) RegisterUser(email string) error {
log.Printf("Attempting to register user with email: %s", email) log.Printf("Attempting to register user with email: %s", email)
log.Printf("Using CSRF token: %s", o.CSRF) log.Printf("Using CSRF token: %s", o.CSRF)
resp, err := o.post("/admin/register", map[string]string{ resp, err := o.post("/admin/register", map[string]string{
"email": email, "email": email,
"_csrf": o.CSRF, // Include the CSRF token
}) })
if err != nil { if err != nil {
return fmt.Errorf("failed to send register request: %v", err) return fmt.Errorf("failed to send register request: %v", err)
@@ -146,9 +145,9 @@ func (o *Overleaf) RegisterUser(email string) error {
// encodeFormData encodes a map into x-www-form-urlencoded format // encodeFormData encodes a map into x-www-form-urlencoded format
func encodeFormData(data map[string]string) string { func encodeFormData(data map[string]string) string {
formData := url.Values{} var formData []string
for key, value := range data { for key, value := range data {
formData.Set(key, value) formData = append(formData, fmt.Sprintf("%s=%s", key, value))
} }
return formData.Encode() return strings.Join(formData, "&")
} }