Files
pages/internal/server/task_dav_credentials.go
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

76 lines
2.1 KiB
Go

package server
import (
"encoding/json"
"io"
"net/http"
"pageswiki/internal/store"
"strings"
)
// Issuing/revoking credentials requires a normal login session. A DAV password
// is deliberately not valid on this management API or any other Wiki endpoint.
func (s *Server) handleTaskDAVCredentials(w http.ResponseWriter, r *http.Request, actor string, parts []string) {
domain := s.domainUUIDForRequest(r)
endpoint := calendarDAV
if strings.HasPrefix(r.URL.Path, tasksAPI) {
endpoint = taskDAVRoot
}
if len(parts) == 1 && r.Method == "GET" {
items, e := s.store.ListTaskDAVCredentials(r.Context(), domain, actor)
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, map[string]any{"items": items, "url": endpoint})
return
}
if len(parts) == 2 && r.Method == "DELETE" {
if e := s.store.RevokeTaskDAVCredential(r.Context(), domain, actor, parts[1]); e != nil {
taskError(w, e)
return
}
w.WriteHeader(204)
return
}
if len(parts) != 1 || r.Method != "POST" {
w.WriteHeader(405)
return
}
var body struct {
Label string `json:"label"`
Write bool `json:"write"`
Days int `json:"days"`
Scope string `json:"scope"`
CalendarAccessCompat bool `json:"calendar_access_compat"`
}
d := json.NewDecoder(http.MaxBytesReader(w, r.Body, 2048))
d.DisallowUnknownFields()
if d.Decode(&body) != nil || d.Decode(&struct{}{}) != io.EOF {
taskError(w, store.ErrTaskInvalid)
return
}
if body.Days == 0 {
body.Days = 90
}
if body.Scope == "" {
body.Scope = "tasks" // Preserve the legacy API's authority boundary.
}
credential, password, e := s.store.CreateDAVCredential(r.Context(), domain, actor, body.Label, body.Write, body.Days, body.Scope, body.CalendarAccessCompat)
if e != nil {
taskError(w, e)
return
}
if body.Scope != "tasks" {
endpoint = calendarDAV
}
if body.Scope == "contacts" {
endpoint = contactDAV
}
response := map[string]any{"credential": credential, "username": credential.ID, "password": password, "url": endpoint}
if body.Scope == "tasks" {
response["collection"] = taskDAVCollection
}
writeEventJSON(w, 201, response)
}