mirror of
https://github.com/ssep1ol/overleaf-registration-be.git
synced 2026-07-21 20:40:21 +00:00
155 lines
3.9 KiB
Go
155 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/http/cookiejar"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
)
|
|
|
|
type Overleaf struct {
|
|
BaseURL string
|
|
Client *http.Client
|
|
CSRF string
|
|
}
|
|
|
|
func NewOverleaf(baseURL string) *Overleaf {
|
|
jar, _ := cookiejar.New(nil) // Create a cookie jar to store cookies
|
|
client := &http.Client{
|
|
Jar: jar, // Attach the cookie jar to the client
|
|
}
|
|
|
|
return &Overleaf{
|
|
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
|
Client: 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
|
|
}
|
|
|
|
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
|
|
formData := encodeFormData(data)
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBufferString(formData))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create POST request: %v", err)
|
|
}
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
csrf, exists := doc.Find(`meta[name="ol-csrfToken"]`).Attr("content")
|
|
if !exists {
|
|
return fmt.Errorf("CSRF token not found in login page")
|
|
}
|
|
|
|
o.CSRF = csrf
|
|
return nil
|
|
}
|
|
|
|
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 {
|
|
log.Printf("Login response: %s", string(body))
|
|
return fmt.Errorf("admin login returned status %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
log.Println("Admin login successful.")
|
|
return nil
|
|
}
|
|
|
|
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)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Log response for debugging
|
|
body, _ := io.ReadAll(resp.Body)
|
|
log.Printf("Register user response: %s", string(body))
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("register user returned status %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
log.Println("User registered successfully.")
|
|
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()
|
|
}
|