Add private CardDAV synchronization and contact revision recovery
This commit is contained in:
1 parent
433169c7e7
commit
19e12f96a8
32 files changed
+1855
-47
No files matched your search
@@ -33,7 +33,10 @@ func (s *Store) CreateTaskDAVCredential(ctx context.Context, domain, actor, labe
|
||||
|
||||
func (s *Store) CreateDAVCredential(ctx context.Context, domain, actor, label string, write bool, days int, scope string, compatibility ...bool) (TaskDAVCredential, string, error) {
|
||||
compat := len(compatibility) == 1 && compatibility[0]
|
||||
if scope != "tasks" && scope != "calendar" && scope != "all" {
|
||||
if scope == "contacts" && compat {
|
||||
return TaskDAVCredential{}, "", ErrTaskInvalid
|
||||
}
|
||||
if scope != "tasks" && scope != "calendar" && scope != "all" && scope != "contacts" {
|
||||
return TaskDAVCredential{}, "", ErrTaskInvalid
|
||||
}
|
||||
label = strings.TrimSpace(label)
|
||||
@@ -71,12 +74,19 @@ func (s *Store) CreateDAVCredential(ctx context.Context, domain, actor, label st
|
||||
// of accidentally interpreting them as task-only authority.
|
||||
secret = "pw_dav_" + base64.RawURLEncoding.EncodeToString(raw)
|
||||
}
|
||||
if scope == "contacts" {
|
||||
secret = "pw_contactdav_" + base64.RawURLEncoding.EncodeToString(raw)
|
||||
}
|
||||
now := time.Now()
|
||||
c := TaskDAVCredential{ID: uuid.NewString(), Label: label, Write: write, CreatedMS: now.UnixMilli(), ExpiresMS: now.Add(time.Duration(days) * 24 * time.Hour).UnixMilli(), Scope: scope}
|
||||
c.CalendarAccessCompat = compat
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_dav_credentials(id,domain_uuid,owner_uuid,label,secret_hash,write_access,created_ms,expires_ms) VALUES(?,?,?,?,?,?,?,?)`, c.ID, domain, actor, c.Label, taskDAVSecretHash(secret), write, c.CreatedMS, c.ExpiresMS)
|
||||
if err == nil {
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_dav_scopes(credential_id,scope) VALUES(?,?)`, c.ID, scope)
|
||||
if scope == "contacts" {
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_contact_credentials VALUES(?)`, c.ID)
|
||||
} else {
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_dav_scopes(credential_id,scope) VALUES(?,?)`, c.ID, scope)
|
||||
}
|
||||
}
|
||||
if err == nil && compat {
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_dav_compat(credential_id) VALUES(?)`, c.ID)
|
||||
@@ -90,7 +100,7 @@ func (s *Store) ListTaskDAVCredentials(ctx context.Context, domain, actor string
|
||||
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT id,label,write_access,created_ms,expires_ms,COALESCE((SELECT scope FROM organizer_dav_scopes WHERE credential_id=id),'tasks'),EXISTS(SELECT 1 FROM organizer_dav_compat WHERE credential_id=id) FROM organizer_dav_credentials WHERE domain_uuid=? AND owner_uuid=? ORDER BY created_ms DESC LIMIT 100`, domain, actor)
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT id,label,write_access,created_ms,expires_ms,CASE WHEN EXISTS(SELECT 1 FROM organizer_contact_credentials WHERE credential_id=id) THEN 'contacts' ELSE COALESCE((SELECT scope FROM organizer_dav_scopes WHERE credential_id=id),'tasks') END,EXISTS(SELECT 1 FROM organizer_dav_compat WHERE credential_id=id) FROM organizer_dav_credentials WHERE domain_uuid=? AND owner_uuid=? ORDER BY created_ms DESC LIMIT 100`, domain, actor)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -130,7 +140,7 @@ func davCredentialAllowed(ctx context.Context, q interface {
|
||||
QueryRowContext(context.Context, string, ...any) *sql.Row
|
||||
}, domain, actor, id string, write bool, scope string) error {
|
||||
var yes bool
|
||||
if err := q.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_dav_credentials WHERE id=? AND domain_uuid=? AND owner_uuid=? AND expires_ms>? AND (?=0 OR write_access=1) AND COALESCE((SELECT scope FROM organizer_dav_scopes WHERE credential_id=id),'tasks') IN (?, 'all'))`, id, domain, actor, time.Now().UnixMilli(), write, scope).Scan(&yes); err != nil {
|
||||
if err := q.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_dav_credentials WHERE id=? AND domain_uuid=? AND owner_uuid=? AND expires_ms>? AND (?=0 OR write_access=1) AND ((?='contacts' AND EXISTS(SELECT 1 FROM organizer_contact_credentials WHERE credential_id=id)) OR (? IN ('tasks','calendar') AND NOT EXISTS(SELECT 1 FROM organizer_contact_credentials WHERE credential_id=id) AND COALESCE((SELECT scope FROM organizer_dav_scopes WHERE credential_id=id),'tasks') IN (?, 'all'))))`, id, domain, actor, time.Now().UnixMilli(), write, scope, scope, scope).Scan(&yes); err != nil {
|
||||
return err
|
||||
}
|
||||
if !yes {
|
||||
@@ -151,16 +161,19 @@ func (s *Store) CheckDAVCredential(ctx context.Context, domain, actor, id string
|
||||
|
||||
// An empty requested scope is discovery only; callers must filter collections.
|
||||
func (s *Store) AuthenticateDAV(ctx context.Context, domain, id, secret, requested string) (string, bool, string, error) {
|
||||
if len(secret) > 128 || !(strings.HasPrefix(secret, "pw_taskdav_") || strings.HasPrefix(secret, "pw_dav_")) {
|
||||
if len(secret) > 128 || !(strings.HasPrefix(secret, "pw_taskdav_") || strings.HasPrefix(secret, "pw_dav_") || strings.HasPrefix(secret, "pw_contactdav_")) {
|
||||
return "", false, "", ErrAccessDenied
|
||||
}
|
||||
var actor, hash, state, scope string
|
||||
var write bool
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT c.owner_uuid,c.secret_hash,c.write_access,p.state,COALESCE(s.scope,'tasks') FROM organizer_dav_credentials c JOIN principals p ON p.uuid=c.owner_uuid LEFT JOIN organizer_dav_scopes s ON s.credential_id=c.id WHERE c.id=? AND c.domain_uuid=? AND c.expires_ms>?`, id, domain, time.Now().UnixMilli()).Scan(&actor, &hash, &write, &state, &scope)
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT c.owner_uuid,c.secret_hash,c.write_access,p.state,CASE WHEN EXISTS(SELECT 1 FROM organizer_contact_credentials WHERE credential_id=c.id) THEN 'contacts' ELSE COALESCE(s.scope,'tasks') END FROM organizer_dav_credentials c JOIN principals p ON p.uuid=c.owner_uuid LEFT JOIN organizer_dav_scopes s ON s.credential_id=c.id WHERE c.id=? AND c.domain_uuid=? AND c.expires_ms>?`, id, domain, time.Now().UnixMilli()).Scan(&actor, &hash, &write, &state, &scope)
|
||||
if err != nil || subtle.ConstantTimeCompare([]byte(hash), []byte(taskDAVSecretHash(secret))) != 1 {
|
||||
return "", false, "", ErrAccessDenied
|
||||
}
|
||||
if requested != "" && scope != "all" && scope != requested {
|
||||
if strings.HasPrefix(secret, "pw_contactdav_") && scope != "contacts" {
|
||||
return "", false, "", ErrAccessDenied
|
||||
}
|
||||
if requested != "" && scope != requested && !(scope == "all" && (requested == "tasks" || requested == "calendar")) {
|
||||
return "", false, "", ErrAccessDenied
|
||||
}
|
||||
if err = taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
||||
|
||||
Reference in new issue
Block a user