carddav: add current-user-principal to server

This commit is contained in:
Simon Ser 2020-01-20 10:56:25 +01:00
parent d8ce7d353d
commit 3a61646ab4
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 28 additions and 23 deletions

View File

@ -223,6 +223,10 @@ func (b *backend) propfindAddressBook(propfind *internal.Propfind, ab *AddressBo
addressBookHomeSetName: func(*internal.RawXMLValue) (interface{}, error) { addressBookHomeSetName: func(*internal.RawXMLValue) (interface{}, error) {
return &addressbookHomeSet{Href: "/"}, nil return &addressbookHomeSet{Href: "/"}, nil
}, },
// TODO: this should be set on all resources
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
return &internal.CurrentUserPrincipal{Href: "/"}, nil
},
} }
if ab.MaxResourceSize > 0 { if ab.MaxResourceSize > 0 {

View File

@ -1,8 +1,8 @@
package webdav package webdav
import ( import (
"encoding/xml"
"net/http" "net/http"
"fmt"
"github.com/emersion/go-webdav/internal" "github.com/emersion/go-webdav/internal"
) )
@ -24,18 +24,20 @@ func (c *Client) SetBasicAuth(username, password string) {
} }
func (c *Client) FindCurrentUserPrincipal() (string, error) { func (c *Client) FindCurrentUserPrincipal() (string, error) {
name := xml.Name{"DAV:", "current-user-principal"} propfind := internal.NewPropNamePropfind(internal.CurrentUserPrincipalName)
propfind := internal.NewPropNamePropfind(name)
resp, err := c.c.PropfindFlat("/", propfind) resp, err := c.c.PropfindFlat("/", propfind)
if err != nil { if err != nil {
return "", err return "", err
} }
var prop currentUserPrincipal var prop internal.CurrentUserPrincipal
if err := resp.DecodeProp(&prop); err != nil { if err := resp.DecodeProp(&prop); err != nil {
return "", err return "", err
} }
if prop.Unauthenticated != nil {
return "", fmt.Errorf("webdav: unauthenticated")
}
return prop.Href, nil return prop.Href, nil
} }

View File

@ -1,10 +0,0 @@
package webdav
import (
"encoding/xml"
)
type currentUserPrincipal struct {
XMLName xml.Name `xml:"DAV: current-user-principal"`
Href string `xml:"href"`
}

View File

@ -11,6 +11,17 @@ import (
const Namespace = "DAV:" const Namespace = "DAV:"
var (
ResourceTypeName = xml.Name{"DAV:", "resourcetype"}
DisplayNameName = xml.Name{"DAV:", "displayname"}
GetContentLengthName = xml.Name{"DAV:", "getcontentlength"}
GetContentTypeName = xml.Name{"DAV:", "getcontenttype"}
GetLastModifiedName = xml.Name{"DAV:", "getlastmodified"}
GetETagName = xml.Name{"DAV:", "getetag"}
CurrentUserPrincipalName = xml.Name{"DAV:", "current-user-principal"}
)
type Status struct { type Status struct {
Code int Code int
Text string Text string
@ -302,15 +313,6 @@ type GetLastModified struct {
LastModified Time `xml:",chardata"` LastModified Time `xml:",chardata"`
} }
var (
ResourceTypeName = xml.Name{"DAV:", "resourcetype"}
DisplayNameName = xml.Name{"DAV:", "displayname"}
GetContentLengthName = xml.Name{"DAV:", "getcontentlength"}
GetContentTypeName = xml.Name{"DAV:", "getcontenttype"}
GetLastModifiedName = xml.Name{"DAV:", "getlastmodified"}
GetETagName = xml.Name{"DAV:", "getetag"}
)
// https://tools.ietf.org/html/rfc4918#section-14.5 // https://tools.ietf.org/html/rfc4918#section-14.5
type Error struct { type Error struct {
XMLName xml.Name `xml:"DAV: error"` XMLName xml.Name `xml:"DAV: error"`
@ -322,3 +324,10 @@ type DisplayName struct {
XMLName xml.Name `xml:"DAV: displayname"` XMLName xml.Name `xml:"DAV: displayname"`
Name string `xml:",chardata"` Name string `xml:",chardata"`
} }
// https://tools.ietf.org/html/rfc5397#section-3
type CurrentUserPrincipal struct {
XMLName xml.Name `xml:"DAV: current-user-principal"`
Href string `xml:"href,omitempty"`
Unauthenticated *struct{} `xml:"unauthenticated,omitempty"`
}