diff --git a/internal/route/main.go b/internal/route/main.go index b19a042..58e9067 100644 --- a/internal/route/main.go +++ b/internal/route/main.go @@ -123,7 +123,51 @@ func RoutesPublic(r *mux.Router, bd *gorm.DB) { http.Error(w, "Erreur lors de la génération de la playlist", http.StatusInternalServerError) } }) - setupWebdavRoute(r, bd) + r.PathPrefix("/webdav/").Handler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + // Authentification HTTP Basic avec vérification en base + userEmail, password, ok := req.BasicAuth() + if !ok { + w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } + + var user models.User + result := bd.Where("email = ?", userEmail).First(&user) + if result.Error != nil { + w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } + + err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) + if err != nil { + w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } + + // Protection lecture seule + if req.Method != "GET" && req.Method != "HEAD" && req.Method != "OPTIONS" && req.Method != "PROPFIND" { + http.Error(w, "Read-Only", http.StatusForbidden) + return + } + + log.Printf("✅ WebDAV access for user: %s", userEmail) + + // Headers WebDAV attendus + w.Header().Set("DAV", "1,2") + w.Header().Set("MS-Author-Via", "DAV") + + // Serve le WebDAV + webdavHandler := &webdav.Handler{ + Prefix: "/webdav/", + FileSystem: webdav.Dir("/app/upload"), + LockSystem: webdav.NewMemLS(), + } + + webdavHandler.ServeHTTP(w, req) + })) // WebDAV sécurisé // username := "tonuser" // ton login