Files
overleaf-registration-be/handlers.go
corrado.mulas 68de7bf0b1 First commit
2024-11-20 00:46:54 +01:00

121 lines
3.0 KiB
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/joho/godotenv"
)
// RegistrationRequest represents the incoming registration request.
type RegistrationRequest struct {
Email string `json:"email"`
Captcha string `json:"captcha"`
}
func registerHandler(w http.ResponseWriter, r *http.Request) {
// Allow only POST requests
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Parse the request body
var req RegistrationRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, fmt.Sprintf("Invalid request body: %v", err), http.StatusBadRequest)
return
}
// Validate email
if !validateEmailDomain(req.Email) {
http.Error(w, "Email domain is not allowed", http.StatusBadRequest)
return
}
// Validate reCAPTCHA token
valid, err := validateRecaptcha(req.Captcha)
if err != nil {
http.Error(w, fmt.Sprintf("CAPTCHA validation failed: %v", err), http.StatusInternalServerError)
return
}
if !valid {
http.Error(w, "Invalid CAPTCHA", http.StatusUnauthorized)
return
}
// Initialize Overleaf instance
baseURL := os.Getenv("OL_INSTANCE")
if baseURL == "" {
http.Error(w, "OL_INSTANCE is not set", http.StatusInternalServerError)
return
}
overleaf := NewOverleaf(baseURL)
// Admin login using environment variables
adminEmail := os.Getenv("OL_ADMIN_EMAIL")
adminPassword := os.Getenv("OL_ADMIN_PASSWORD")
if adminEmail == "" || adminPassword == "" {
http.Error(w, "OL_ADMIN_EMAIL or OL_ADMIN_PASSWORD is not set", http.StatusInternalServerError)
return
}
if err := overleaf.Login(adminEmail, adminPassword); err != nil {
http.Error(w, fmt.Sprintf("Admin login failed: %v", err), http.StatusInternalServerError)
return
}
// 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
}
// Respond with success
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{
"message": "User registered successfully. Please check your email to set your password.",
})
}
func validateEmailDomain(email string) bool {
allowedDomains := os.Getenv("ALLOWED_DOMAINS")
if allowedDomains == "" {
return false
}
parts := strings.Split(email, "@")
if len(parts) != 2 {
return false // Invalid email format
}
domain := parts[1]
allowed := strings.Split(allowedDomains, ",")
for _, allowedDomain := range allowed {
if strings.TrimSpace(domain) == strings.TrimSpace(allowedDomain) {
return true
}
}
return false
}
func validateEnvVars(vars []string) error {
for _, v := range vars {
if os.Getenv(v) == "" {
return fmt.Errorf("environment variable %s is not set", v)
}
}
return nil
}
func loadEnv() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found, using system environment variables")
}
}