mirror of
https://github.com/ssep1ol/overleaf-registration-be.git
synced 2026-07-21 20:40:21 +00:00
First commit
This commit is contained in:
32
captcha.go
32
captcha.go
@@ -1,32 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
)
|
||||
|
||||
type CaptchaResponse struct {
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
func ValidateCaptcha(captcha string) bool {
|
||||
secretKey := os.Getenv("CAPTCHA_SERVER_KEY")
|
||||
if secretKey == "" {
|
||||
return true // Skip validation if CAPTCHA is disabled
|
||||
}
|
||||
|
||||
resp, err := http.PostForm("https://www.google.com/recaptcha/api/siteverify", url.Values{
|
||||
"secret": {secretKey},
|
||||
"response": {captcha},
|
||||
})
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result CaptchaResponse
|
||||
err = json.NewDecoder(resp.Body).Decode(&result)
|
||||
return err == nil && result.Success
|
||||
}
|
||||
20
main.go
20
main.go
@@ -8,21 +8,23 @@ import (
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func loadEnv() {
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Println("No .env file found, using system environment variables")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Load environment variables
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Println("No .env file found, using environment variables")
|
||||
}
|
||||
loadEnv()
|
||||
|
||||
// Register the HTTP handler
|
||||
http.HandleFunc("/register", RegisterHandler)
|
||||
http.HandleFunc("/register", registerHandler)
|
||||
|
||||
// Start the HTTP server
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "3000"
|
||||
port = "3000" // Default to port 3000
|
||||
}
|
||||
log.Printf("Server running on port %s\n", port)
|
||||
|
||||
log.Printf("Starting server on port %s...", port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||
}
|
||||
|
||||
48
overleaf.go
48
overleaf.go
@@ -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()
|
||||
}
|
||||
|
||||
41
recaptcha.go
Normal file
41
recaptcha.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func validateRecaptcha(token string) (bool, error) {
|
||||
secret := os.Getenv("RECAPTCHA_SECRET_KEY")
|
||||
if secret == "" {
|
||||
return false, errors.New("missing RECAPTCHA_SECRET_KEY")
|
||||
}
|
||||
|
||||
url := "https://www.google.com/recaptcha/api/siteverify"
|
||||
payload := map[string]string{
|
||||
"secret": secret,
|
||||
"response": token,
|
||||
}
|
||||
jsonPayload, _ := json.Marshal(payload)
|
||||
|
||||
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonPayload))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
success, ok := result["success"].(bool)
|
||||
if !ok {
|
||||
return false, errors.New("invalid reCAPTCHA response format")
|
||||
}
|
||||
|
||||
return success, nil
|
||||
}
|
||||
Reference in New Issue
Block a user