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
}
// NewOverleaf initializes a new Overleaf instance with a cookie jar
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{
Jar: jar, // Attach the cookie jar to the client
Jar: jar, // Attach cookie jar to client
}
return &Overleaf{
@@ -31,20 +32,26 @@ func NewOverleaf(baseURL string) *Overleaf {
}
}
// 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)
// logCookies logs cookies stored in the client's cookie jar for a given endpoint
func logCookies(client *http.Client, endpoint string) {
u, _ := url.Parse(endpoint)
cookies := client.Jar.Cookies(u)
for _, cookie := range cookies {
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) {
url := fmt.Sprintf("%s%s", o.BaseURL, endpoint)
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")
// Log cookies for debugging
cookies := o.Client.Jar.Cookies(req.URL)
for _, cookie := range cookies {
log.Printf("Cookie sent to %s: %s=%s", endpoint, cookie.Name, cookie.Value)
}
return o.Client.Do(req)
resp, err := o.Client.Do(req)
if err == nil {
logCookies(o.Client, url) // Log cookies after POST request
}
return resp, err
}
// obtainCSRF retrieves the CSRF token
func (o *Overleaf) obtainCSRF() error {
// Perform a GET request to retrieve the login page
resp, err := o.get("/login")
if err != nil {
return fmt.Errorf("failed to load login page: %v", err)
}
defer resp.Body.Close()
// Parse the HTML to find the CSRF token
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return fmt.Errorf("failed to parse login page HTML: %v", err)
@@ -85,32 +89,27 @@ func (o *Overleaf) obtainCSRF() error {
}
o.CSRF = csrf
log.Printf("Obtained CSRF token: %s", o.CSRF)
return nil
}
// Login authenticates as admin
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{
"email": email,
"password": password,
"_csrf": o.CSRF, // Include the CSRF token
})
if err != nil {
return fmt.Errorf("failed to send login request: %v", err)
}
defer resp.Body.Close()
// Log response for debugging
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
log.Printf("Login response: %s", 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
}
// RegisterUser registers a new user
func (o *Overleaf) RegisterUser(email string) error {
log.Printf("Attempting to register user with email: %s", email)
log.Printf("Using CSRF token: %s", o.CSRF)
resp, err := o.post("/admin/register", map[string]string{
"email": email,
"_csrf": o.CSRF, // Include the CSRF token
})
if err != nil {
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
func encodeFormData(data map[string]string) string {
formData := url.Values{}
var formData []string
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, "&")
}