go-webdav/carddav/carddav.go
Conrad Hoffmann 6887b6b812 Support custom user principal and home set paths
Currently, the user principal path and the home set path are both
hardcoded to "/", for both CalDAV and CardDAV. This poses a challenge if
one wishes to run a CardDAV and CalDAV server in the same server.

This commit introduces the concept of a UserPrincipalBackend. This
backend must provide the path of the current user's principal URL from
the given request context.

The CalDAV and CardDAV backends are extended to also function as
UserPrincipalBackend. In addition, they are required to supply the path
of the respective home set (`calendar-home-set` and
`addressbook-home-set`). The CardDAV and CalDAV servers act accordingly.

The individual servers will continue to work as before (including the
option of keeping everything at "/"). If one wishes to run CardDAV and
CalDAV in parallel, the new `webdav.ServeUserPrincipal()` can be used as
a convenience function to serve a common user principal URL for both
servers. The input for this function can be easily computed by the
application by getting the home set paths from the backends and using
`caldav.NewCalendarHomeSet()` and `carddav.NewAddressbookHomeSet()` to
create the home sets.

Note that the storage backend will have to know about these paths as
well. For any non-trivial use case, a storage backend should probably
have access to the same UserPrincipalBackend. That is, however, an
implementation detail and doesn't have to be reflected in the
interfaces.
2022-05-11 11:12:04 +02:00

122 lines
2.5 KiB
Go

// Package carddav provides a client and server CardDAV implementation.
//
// CardDAV is defined in RFC 6352.
package carddav
import (
"time"
"github.com/emersion/go-vcard"
"github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/internal"
)
func NewAddressBookHomeSet(path string) webdav.BackendSuppliedHomeSet {
return &addressbookHomeSet{Href: internal.Href{Path: path}}
}
type AddressDataType struct {
ContentType string
Version string
}
type AddressBook struct {
Path string
Name string
Description string
MaxResourceSize int64
SupportedAddressData []AddressDataType
}
func (ab *AddressBook) SupportsAddressData(contentType, version string) bool {
if len(ab.SupportedAddressData) == 0 {
return contentType == "text/vcard" && version == "3.0"
}
for _, t := range ab.SupportedAddressData {
if t.ContentType == contentType && t.Version == version {
return true
}
}
return false
}
type AddressBookQuery struct {
DataRequest AddressDataRequest
PropFilters []PropFilter
FilterTest FilterTest // defaults to FilterAnyOf
Limit int // <= 0 means unlimited
}
type AddressDataRequest struct {
Props []string
AllProp bool
}
type PropFilter struct {
Name string
Test FilterTest // defaults to FilterAnyOf
// if IsNotDefined is set, TextMatches and Params need to be unset
IsNotDefined bool
TextMatches []TextMatch
Params []ParamFilter
}
type ParamFilter struct {
Name string
// if IsNotDefined is set, TextMatch needs to be unset
IsNotDefined bool
TextMatch *TextMatch
}
type TextMatch struct {
Text string
NegateCondition bool
MatchType MatchType // defaults to MatchContains
}
type FilterTest string
const (
FilterAnyOf FilterTest = "anyof"
FilterAllOf FilterTest = "allof"
)
type MatchType string
const (
MatchEquals MatchType = "equals"
MatchContains MatchType = "contains"
MatchStartsWith MatchType = "starts-with"
MatchEndsWith MatchType = "ends-with"
)
type AddressBookMultiGet struct {
Paths []string
DataRequest AddressDataRequest
}
type AddressObject struct {
Path string
ModTime time.Time
ETag string
Card vcard.Card
}
//SyncQuery is the query struct represents a sync-collection request
type SyncQuery struct {
DataRequest AddressDataRequest
SyncToken string
Limit int // <= 0 means unlimited
}
//SyncResponse contains the returned sync-token for next time
type SyncResponse struct {
SyncToken string
Updated []AddressObject
Deleted []string
}