Files
Anya Tokugawa 19e12f96a8
Build and publish container images / images (push) Successful in 13m39s
Release container images / release (push) Successful in 18s
Add private CardDAV synchronization and contact revision recovery
2026-11-02 08:41:15 +09:00

136 lines
3.6 KiB
Go

package middleware
import (
"context"
"log"
"net/http"
"strings"
"time"
)
type davDiagnosticKey struct{}
type davDiagnostic struct{ outcome, target string }
// UUID collections cannot be classified as VEVENT/VTODO from the URL alone.
func SetDAVTaskTarget(r *http.Request, resource bool) {
if d, ok := r.Context().Value(davDiagnosticKey{}).(*davDiagnostic); ok {
d.target = "task_collection"
if resource {
d.target = "task_resource"
}
}
}
// SetDAVAuthOutcome accepts only fixed diagnostic categories, never identities
// or error strings. The value is server-side only, not a response header.
func SetDAVAuthOutcome(r *http.Request, outcome string) {
switch outcome {
case "basic_accepted", "basic_rejected", "session_accepted", "anonymous", "missing", "invalid_authorization", "multiple_authorization":
default:
return
}
if d, ok := r.Context().Value(davDiagnosticKey{}).(*davDiagnostic); ok {
d.outcome = outcome
}
}
func davLogTarget(path string) string {
if path == "/.well-known/carddav" {
return "carddav_discovery"
}
if path == "/_/api/v1/carddav" || strings.HasPrefix(path, "/_/api/v1/carddav/") {
return "carddav"
}
const root = "/_/api/v1/caldav"
p := strings.TrimSuffix(path, "/")
switch p {
case "/.well-known/caldav":
return "well_known"
case root:
return "home"
case root + "/tasks":
return "task_home"
case root + "/tasks/default":
return "task_collection"
}
if strings.HasPrefix(path, root+"/tasks/default/") {
return "task_resource"
}
if strings.HasPrefix(path, root+"/") {
if strings.Contains(strings.TrimPrefix(p, root+"/"), "/") {
return "calendar_resource"
}
return "calendar_collection"
}
return ""
}
func davAuthScheme(r *http.Request) string {
values := r.Header.Values("Authorization")
if len(values) > 1 {
return "multiple"
}
if len(values) == 0 || values[0] == "" {
return "none"
}
if _, _, ok := r.BasicAuth(); ok {
return "basic"
}
if strings.HasPrefix(strings.ToLower(values[0]), "basic ") {
return "malformed_basic"
}
if strings.HasPrefix(strings.ToLower(values[0]), "bearer ") {
return "bearer"
}
return "other"
}
type davLogWriter struct {
http.ResponseWriter
status int
}
func (w *davLogWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter }
func (w *davLogWriter) WriteHeader(status int) {
if w.status != 0 {
return
}
if status >= 200 || status == 101 {
w.status = status
}
w.ResponseWriter.WriteHeader(status)
}
func (w *davLogWriter) Write(b []byte) (int, error) {
if w.status == 0 {
w.WriteHeader(http.StatusOK)
}
return w.ResponseWriter.Write(b)
}
// DAV logs replace raw-path access logs: no usernames, credential IDs, resource
// names, query strings, cookies, Authorization values or calendar bodies.
func logDAVRequest(w http.ResponseWriter, r *http.Request, next http.Handler, target string, start time.Time) {
d := &davDiagnostic{outcome: "not_checked"}
r = r.WithContext(context.WithValue(r.Context(), davDiagnosticKey{}, d))
rec := &davLogWriter{ResponseWriter: w}
next.ServeHTTP(rec, r)
if d.target != "" {
target = d.target
}
status := rec.status
if status == 0 {
status = http.StatusOK
}
method := r.Method
switch method {
case "OPTIONS", "PROPFIND", "REPORT", "GET", "HEAD", "PUT", "DELETE", "POST", "MKCALENDAR", "PROPPATCH":
default:
method = "other"
}
log.Printf("caldav request method=%s target=%s status=%d auth=%s outcome=%s challenge=%t compatibility=%t request_id=%q duration=%s",
method, target, status, davAuthScheme(r), d.outcome,
rec.Header().Get("WWW-Authenticate") != "",
rec.Header().Get("X-PagesWiki-CalDAV-Profile") == "limited-compatibility",
rec.Header().Get("X-Request-ID"), time.Since(start))
}