Compare commits

..

1 Commits
v0.8.3 ... main

Author SHA1 Message Date
Alex Dunmow
bcc2aea1d9 caldav: serve principal-URL and, through PrincipalNameBackend, displayname on the principal
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 23:51:22 +08:00
4 changed files with 48 additions and 0 deletions

View File

@ -193,6 +193,12 @@ type PrincipalBackend interface {
ScheduleOutboxPath(ctx context.Context) (string, error)
}
// PrincipalNameBackend is an optional Backend interface naming the current
// user principal, served as its DAV:displayname.
type PrincipalNameBackend interface {
PrincipalDisplayName(ctx context.Context) (string, error)
}
// FreeBusyBackend is an optional Backend interface answering free/busy
// requests POSTed to the schedule outbox (RFC 6638 section 5). The Backend
// must also implement PrincipalBackend.

View File

@ -651,8 +651,20 @@ func (b *backend) propFindUserPrincipal(ctx context.Context, propfind *internal.
calendarHomeSetName: internal.PropFindValue(&calendarHomeSet{
Href: internal.Href{Path: homeSetPath},
}),
internal.PrincipalURLName: internal.PropFindValue(&internal.PrincipalURL{
Href: internal.Href{Path: principalPath},
}),
internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName, internal.PrincipalName)),
}
if nb, ok := b.Backend.(PrincipalNameBackend); ok {
props[internal.DisplayNameName] = func(*internal.RawXMLValue) (interface{}, error) {
name, err := nb.PrincipalDisplayName(ctx)
if err != nil {
return nil, err
}
return &internal.DisplayName{Name: name}, nil
}
}
if pb, ok := b.Backend.(PrincipalBackend); ok {
props[calendarUserAddressSetName] = func(*internal.RawXMLValue) (interface{}, error) {
addrs, err := pb.CalendarUserAddresses(ctx)

View File

@ -285,6 +285,10 @@ func (schedBackend) CalendarUserAddresses(ctx context.Context) ([]string, error)
return []string{"wilfredo@example.com"}, nil
}
func (schedBackend) PrincipalDisplayName(ctx context.Context) (string, error) {
return "Wilfredo", nil
}
func (schedBackend) ScheduleInboxPath(ctx context.Context) (string, error) {
return "/user/inbox/", nil
}
@ -466,6 +470,25 @@ func TestPropFindPrincipalScheduling(t *testing.T) {
}
}
func TestPropFindPrincipalNameAndURL(t *testing.T) {
_, resp := serve(t, &Handler{Backend: schedBackend{}}, "PROPFIND", "/user/", "application/xml", `
<d:propfind xmlns:d="DAV:"><d:prop><d:principal-URL/><d:displayname/></d:prop></d:propfind>`)
for _, want := range []string{
`<principal-URL xmlns="DAV:"><href>/user/</href></principal-URL>`,
`<displayname xmlns="DAV:">Wilfredo</displayname>`,
} {
if !strings.Contains(resp, want) {
t.Errorf("missing %s in response:\n%s", want, resp)
}
}
_, resp = serve(t, &Handler{Backend: testBackend{}}, "PROPFIND", "/user/", "application/xml", `
<d:propfind xmlns:d="DAV:"><d:prop><d:principal-URL/><d:displayname/></d:prop></d:propfind>`)
if !strings.Contains(resp, "principal-URL") || strings.Contains(resp, "Wilfredo") {
t.Errorf("principal-URL is always served, displayname only with PrincipalNameBackend:\n%s", resp)
}
}
func TestOptionsAutoSchedule(t *testing.T) {
res, _ := serve(t, &Handler{Backend: schedBackend{}}, http.MethodOptions, "/user/", "", "")
if dav := res.Header.Get("DAV"); !strings.Contains(dav, "calendar-auto-schedule") {

View File

@ -25,6 +25,7 @@ var (
PrincipalName = xml.Name{Namespace, "principal"}
CurrentUserPrincipalName = xml.Name{Namespace, "current-user-principal"}
PrincipalURLName = xml.Name{Namespace, "principal-URL"}
CurrentUserPrivilegeSetName = xml.Name{Namespace, "current-user-privilege-set"}
SyncTokenName = xml.Name{Space: Namespace, Local: "sync-token"}
@ -417,6 +418,12 @@ func (err *Error) Error() string {
return string(b)
}
// https://tools.ietf.org/html/rfc3744#section-4.2
type PrincipalURL struct {
XMLName xml.Name `xml:"DAV: principal-URL"`
Href Href `xml:"href"`
}
// https://tools.ietf.org/html/rfc4918#section-15.2
type DisplayName struct {
XMLName xml.Name `xml:"DAV: displayname"`