First commit

This commit is contained in:
corrado.mulas
2024-11-20 00:17:54 +01:00
parent 50296df78f
commit 9d22e4d3a3

View File

@@ -2,72 +2,59 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)
type RegisterRequest struct {
// RegistrationRequest represents the incoming registration request.
type RegistrationRequest struct {
Email string `json:"email"`
Password string `json:"password"`
Captcha string `json:"captcha"`
}
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
func registerHandler(w http.ResponseWriter, r *http.Request) {
// Allow only POST method
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Parse the request
var req RegisterRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
// Parse the request body into the RegistrationRequest struct
var req RegistrationRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
// Validate email domain
allowedDomains := strings.Split(os.Getenv("ALLOWED_DOMAINS"), ",")
if !isDomainAllowed(req.Email, allowedDomains) {
http.Error(w, "Email domain not allowed", http.StatusForbidden)
// Validate reCAPTCHA token
valid, err := validateRecaptcha(req.Captcha)
if err != nil {
http.Error(w, fmt.Sprintf("CAPTCHA validation failed: %v", err), http.StatusInternalServerError)
return
}
// Validate CAPTCHA
if !ValidateCaptcha(req.Captcha) {
if !valid {
http.Error(w, "Invalid CAPTCHA", http.StatusUnauthorized)
return
}
// Register user
overleaf := NewOverleaf(os.Getenv("OL_INSTANCE"))
err = overleaf.Login(os.Getenv("OL_ADMIN_EMAIL"), os.Getenv("OL_ADMIN_PASSWORD"))
if err != nil {
http.Error(w, "Failed to log in as admin", http.StatusInternalServerError)
// Initialize Overleaf instance
overleaf := NewOverleaf("http://localhost:3000") // Replace with the actual Overleaf base URL
// Optional: Log in as admin (if needed for user creation)
adminEmail := "admin@example.com" // Replace with actual admin email
adminPassword := "adminpassword" // Replace with actual admin password
if err := overleaf.Login(adminEmail, adminPassword); err != nil {
http.Error(w, fmt.Sprintf("Admin login failed: %v", err), http.StatusInternalServerError)
return
}
err = overleaf.RegisterUser(req.Email, req.Password)
if err != nil {
http.Error(w, "Failed to register user: "+err.Error(), http.StatusInternalServerError)
// Create the user in Overleaf
if err := overleaf.RegisterUser(req.Email); err != nil {
http.Error(w, fmt.Sprintf("Failed to create user: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]string{"message": "User registered successfully"})
}
func isDomainAllowed(email string, allowedDomains []string) bool {
parts := strings.Split(email, "@")
if len(parts) != 2 {
return false
}
domain := parts[1]
for _, allowed := range allowedDomains {
if domain == allowed {
return true
}
}
return false
// Respond with success
w.WriteHeader(http.StatusOK)
w.Write([]byte("User registered successfully. Please check your email to set your password."))
}