Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,29 +193,31 @@ new user of the same name gets in.
Users can let other users into a folder of their home, or the whole home:
**People** in the folder adds someone by username, who can then view what
is in it, or edit it too (upload, create folders, rename, overwrite and
delete in it, but not delete or rename the shared folder itself). Sharing
it with them again changes how; nested folders shared both ways give the
more of the two. It is listed under **Shared** on both sides, where its
delete in it, but not delete or rename the shared folder itself), until
the owner stops it or for a day, a week, a month or 3 months. Sharing it
with them again changes how and for how long; nested folders shared both
ways give the more of the two. It is listed under **Shared** on both sides, where its
owner can stop sharing it and its user can leave it, and a superadmin can
end any share without seeing where it is. Users find what is shared with
them under **Shared**, and over WebDAV on the way to it through `/home/`,
with nothing else of the owner's home.

A share follows its folder when it is renamed or moved within the home,
and ends when the folder is deleted (a restored folder is no longer
shared), moved out of the home or replaced by another, or when either
and ends at its end date, when the folder is deleted (a restored folder is
no longer shared), moved out of the home or replaced by another, or when either
user is deleted. It lets its user do only what the owner may do and share
there, and rules for the same paths come first, as for homes. What they
add counts against the owner's quota; what they delete goes to the trash,
where both see it. Scripts:

```sh
curl -H "Authorization: Bearer $TOKEN" -d action=grant -d path=/home/bob/photos -d user=alice -d access=edit https://host/.shares/
# {"id":1,"path":"/home/bob/photos","user":"alice","access":"edit"}
curl -H "Authorization: Bearer $TOKEN" -d action=grant -d path=/home/bob/photos -d user=alice -d access=edit -d expires=7d https://host/.shares/
# {"id":1,"path":"/home/bob/photos","user":"alice","access":"edit","expires_at":"..."}
curl -H "Authorization: Bearer $TOKEN" -d action=ungrant -d id=1 https://host/.shares/
```

`access` is `read` (the default) or `edit`. Sharing needs the `share`
`access` is `read` (the default) or `edit`; `expires` is `1d`, `7d`, `30d`
or `90d`, or left out for no end date (`expires_at` is then `null`). Sharing needs the `share`
action on the folder, and `write` for edit, which everyone has in their
home unless a rule takes it away.

Expand Down
20 changes: 14 additions & 6 deletions internal/auth/authtest/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ func (s *Store) ShareFolder(_ context.Context, fs *auth.FolderShare) error {
n := 0
for _, other := range s.folders {
if other.Path == fs.Path && other.UserID == fs.UserID {
other.Write = fs.Write
other.Write, other.ExpiresAt = fs.Write, fs.ExpiresAt
fs.ID, fs.CreatedAt = other.ID, other.CreatedAt
return nil
}
if other.OwnerID == fs.OwnerID {
if other.OwnerID == fs.OwnerID && live(other) {
n++
}
}
Expand All @@ -286,12 +286,15 @@ func (s *Store) ShareFolder(_ context.Context, fs *auth.FolderShare) error {
return nil
}

// folderShares lists the folder shares keep picks, with the current names
// of their users.
// live reports whether fs has not expired.
func live(fs *auth.FolderShare) bool { return fs.ExpiresAt.IsZero() || time.Now().Before(fs.ExpiresAt) }

// folderShares lists the unexpired folder shares keep picks, with the
// current names of their users.
func (s *Store) folderShares(keep func(*auth.FolderShare) bool) []auth.FolderShare {
var out []auth.FolderShare
for _, fs := range s.folders {
if keep(fs) {
if live(fs) && keep(fs) {
c := *fs
c.Owner, c.User = s.users[fs.OwnerID].Username, s.users[fs.UserID].Username
out = append(out, c)
Expand Down Expand Up @@ -320,7 +323,7 @@ func (s *Store) DeleteFolderShare(_ context.Context, id, userID int64) (*auth.Fo
s.mu.Lock()
defer s.mu.Unlock()
fs, ok := s.folders[id]
if !ok || userID != 0 && fs.OwnerID != userID && fs.UserID != userID {
if !ok || !live(fs) || userID != 0 && fs.OwnerID != userID && fs.UserID != userID {
return nil, auth.ErrNotFound
}
delete(s.folders, id)
Expand Down Expand Up @@ -613,6 +616,11 @@ func (s *Store) DeleteExpiredShares(context.Context) error {
delete(s.shares, k)
}
}
for id, fs := range s.folders {
if !live(fs) {
delete(s.folders, id)
}
}
return nil
}

Expand Down
20 changes: 13 additions & 7 deletions internal/auth/people.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
// what is in it, or with Write to change it too, but for deleting or
// renaming the folder itself. It lets them do only what its owner may do
// there and share, and the rules of administrators for the same paths come
// first. It ends when the folder is deleted or leaves the home, and follows
// it when it moves within the home (see MoveFolderShares).
// first. It ends at ExpiresAt, if set, or when the folder is deleted or
// leaves the home, and follows it when it moves within the home (see
// MoveFolderShares).
type FolderShare struct {
ID int64
OwnerID int64
Expand All @@ -24,16 +25,18 @@ type FolderShare struct {
User string // their username
Write bool
CreatedAt time.Time
ExpiresAt time.Time // zero for never
}

// MaxFolderShares bounds the folder shares of one owner.
const MaxFolderShares = 200

// ShareFolder shares the folder at path, which really is owner's home or in
// it, with the user username (ErrNotFound if there is none), to read or with
// write to change; sharing it with them again changes how. It does not
// check that the folder exists.
func (s *Service) ShareFolder(ctx context.Context, owner *User, path, username string, write bool) (*FolderShare, error) {
// write to change, for ttl or with 0 until it is stopped; sharing it with
// them again changes how and for how long. It does not check that the
// folder exists.
func (s *Service) ShareFolder(ctx context.Context, owner *User, path, username string, write bool, ttl time.Duration) (*FolderShare, error) {
home := s.HomeOf(owner.Username)
if home == "" || !s.within(path, home) {
return nil, fmt.Errorf("%w: only folders in your home can be shared with people", ErrInvalid)
Expand All @@ -58,14 +61,17 @@ func (s *Service) ShareFolder(ctx context.Context, owner *User, path, username s
}
}
fs := &FolderShare{OwnerID: owner.ID, Owner: owner.Username, Path: path, UserID: user.ID, User: user.Username, Write: write}
if ttl > 0 {
fs.ExpiresAt = time.Now().Add(ttl)
}
if err := s.store.ShareFolder(ctx, fs); err != nil {
return nil, err
}
return fs, nil
}

// FolderShares lists the folders owner shares with people, newest first;
// those of everyone with all.
// FolderShares lists the folders owner shares with people that have not
// expired, newest first; those of everyone with all.
func (s *Service) FolderShares(ctx context.Context, owner *User, all bool) ([]FolderShare, error) {
if all {
return s.store.FolderShares(ctx, 0)
Expand Down
100 changes: 86 additions & 14 deletions internal/auth/people_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"fmt"
"strings"
"testing"
"time"

"go.uber.org/zap"

"goftp/internal/auth"
)
Expand All @@ -32,11 +35,11 @@ func TestFolderShares(t *testing.T) {
{"/home/dora/docs", "dora", auth.ErrInvalid}, // dora's already
{"/home/dora/docs", "nobody", auth.ErrNotFound},
} {
if _, err := svc.ShareFolder(ctx, dora, tc.path, tc.user, false); !errors.Is(err, tc.want) {
if _, err := svc.ShareFolder(ctx, dora, tc.path, tc.user, false, 0); !errors.Is(err, tc.want) {
t.Errorf("ShareFolder(%q, %q): %v", tc.path, tc.user, err)
}
}
fs, err := svc.ShareFolder(ctx, dora, "/home/dora/docs", "Erin", false)
fs, err := svc.ShareFolder(ctx, dora, "/home/dora/docs", "Erin", false, 0)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -76,7 +79,7 @@ func TestFolderShares(t *testing.T) {
})

// Sharing it again changes how.
again, err := svc.ShareFolder(ctx, dora, "/home/dora/docs", "erin", true)
again, err := svc.ShareFolder(ctx, dora, "/home/dora/docs", "erin", true, 0)
if err != nil || again.ID != fs.ID || !again.Write {
t.Fatalf("shared again: %+v, %v", again, err)
}
Expand Down Expand Up @@ -136,10 +139,10 @@ func TestFolderShares(t *testing.T) {
t.Errorf("SharedWith(%s) = %v, want %v", u.Username, got, want)
}
}
if _, err := svc.ShareFolder(ctx, dora, "/home/dora/docs/team", "finn", false); err != nil {
if _, err := svc.ShareFolder(ctx, dora, "/home/dora/docs/team", "finn", false, 0); err != nil {
t.Fatal(err)
}
if _, err := svc.ShareFolder(ctx, erin, "/home/erin", "dora", false); err != nil {
if _, err := svc.ShareFolder(ctx, erin, "/home/erin", "dora", false, 0); err != nil {
t.Fatal(err)
}
lists(dora, false, "dora:/home/dora/docs/team>finn", "dora:/home/dora/docs>erin")
Expand Down Expand Up @@ -176,7 +179,7 @@ func TestFolderShares(t *testing.T) {
// Unrelated moves leave them be; shares left where a folder moves to
// are of a folder that is gone.
for _, p := range []string{"/home/dora/p", "/home/dora/q", "/home/dora/qq"} {
if _, err := svc.ShareFolder(ctx, dora, p, "erin", false); err != nil {
if _, err := svc.ShareFolder(ctx, dora, p, "erin", false, 0); err != nil {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -205,7 +208,7 @@ func TestFolderShares(t *testing.T) {
if _, err := svc.UnshareFolder(ctx, dora, q, false); !errors.Is(err, auth.ErrNotFound) {
t.Errorf("ended twice: %v", err)
}
fs, err = svc.ShareFolder(ctx, dora, "/home/dora", "finn", false)
fs, err = svc.ShareFolder(ctx, dora, "/home/dora", "finn", false, 0)
if err != nil {
t.Fatal(err)
}
Expand All @@ -214,7 +217,7 @@ func TestFolderShares(t *testing.T) {
}

// Users' shares go with them.
if _, err := svc.ShareFolder(ctx, dora, "/home/dora/x", "erin", false); err != nil {
if _, err := svc.ShareFolder(ctx, dora, "/home/dora/x", "erin", false, 0); err != nil {
t.Fatal(err)
}
if err := svc.DeleteUser(ctx, "erin"); err != nil {
Expand All @@ -223,7 +226,7 @@ func TestFolderShares(t *testing.T) {
lists(dora, true)

// Homes that moved leave their shares behind.
if _, err := svc.ShareFolder(ctx, dora, "/home/dora/x", "finn", true); err != nil {
if _, err := svc.ShareFolder(ctx, dora, "/home/dora/x", "finn", true, 0); err != nil {
t.Fatal(err)
}
svc.SetHomes("/users")
Expand All @@ -245,15 +248,15 @@ func TestFolderShareLimit(t *testing.T) {
}
dora := userNamed(t, svc, "dora")
for i := range auth.MaxFolderShares {
if _, err := svc.ShareFolder(ctx, dora, fmt.Sprintf("/home/dora/%d", i), "erin", false); err != nil {
if _, err := svc.ShareFolder(ctx, dora, fmt.Sprintf("/home/dora/%d", i), "erin", false, 0); err != nil {
t.Fatal(err)
}
}
if _, err := svc.ShareFolder(ctx, dora, "/home/dora/more", "erin", false); !errors.Is(err, auth.ErrTooMany) {
if _, err := svc.ShareFolder(ctx, dora, "/home/dora/more", "erin", false, 0); !errors.Is(err, auth.ErrTooMany) {
t.Errorf("one too many: %v", err)
}
// Changing how one is shared is not one more.
if _, err := svc.ShareFolder(ctx, dora, "/home/dora/0", "erin", true); err != nil {
if _, err := svc.ShareFolder(ctx, dora, "/home/dora/0", "erin", true, 0); err != nil {
t.Errorf("shared again: %v", err)
}
})
Expand All @@ -272,11 +275,11 @@ func TestFolderSharesFolded(t *testing.T) {
}
}
dora, erin := userNamed(t, svc, "dora"), userNamed(t, svc, "erin")
first, err := svc.ShareFolder(ctx, dora, "/home/dora/Docs", "erin", false)
first, err := svc.ShareFolder(ctx, dora, "/home/dora/Docs", "erin", false, 0)
if err != nil {
t.Fatal(err)
}
again, err := svc.ShareFolder(ctx, dora, "/home/dora/DOCS", "erin", true)
again, err := svc.ShareFolder(ctx, dora, "/home/dora/DOCS", "erin", true, 0)
if err != nil || again.ID != first.ID || again.Path != "/home/dora/Docs" {
t.Errorf("shared again: %+v, %v", again, err)
}
Expand All @@ -296,3 +299,72 @@ func TestFolderSharesFolded(t *testing.T) {
}
})
}

func TestFolderShareExpiry(t *testing.T) {
services(t, func(t *testing.T, svc *auth.Service) {
ctx := context.Background()
svc.SetHomes("/home")
for _, name := range []string{"dora", "erin"} {
if _, err := svc.CreateUser(ctx, name, "user"); err != nil {
t.Fatal(err)
}
}
dora, erin := userNamed(t, svc, "dora"), userNamed(t, svc, "erin")
reads := func(p string) bool {
t.Helper()
rules, err := svc.RulesFor(ctx, erin)
if err != nil {
t.Fatal(err)
}
return rules.Allow(p, auth.ActRead)
}

fs, err := svc.ShareFolder(ctx, dora, "/home/dora/docs", "erin", false, time.Hour)
if err != nil {
t.Fatal(err)
}
if until := time.Until(fs.ExpiresAt); until < 59*time.Minute || until > time.Hour {
t.Errorf("expires in %v", until)
}
shares, err := svc.SharedWith(ctx, erin)
if err != nil || len(shares) != 1 || time.Until(shares[0].ExpiresAt) < 59*time.Minute {
t.Errorf("listed: %+v, %v", shares, err)
}
// Sharing it again changes until when, here to never.
if again, err := svc.ShareFolder(ctx, dora, "/home/dora/docs", "erin", false, 0); err != nil || again.ID != fs.ID || !again.ExpiresAt.IsZero() {
t.Errorf("shared again: %+v, %v", again, err)
}
if shares, err := svc.FolderShares(ctx, dora, false); err != nil || len(shares) != 1 || !shares[0].ExpiresAt.IsZero() {
t.Errorf("no end: %+v, %v", shares, err)
}

// Once it ends, it is gone, though kept until purged.
old, err := svc.ShareFolder(ctx, dora, "/home/dora/old", "erin", true, 50*time.Millisecond)
if err != nil {
t.Fatal(err)
}
if !reads("/home/dora/old/a.txt") {
t.Error("not shared before it ends")
}
time.Sleep(100 * time.Millisecond)
if reads("/home/dora/old/a.txt") {
t.Error("shared after it ended")
}
if shares, err := svc.FolderShares(ctx, dora, true); err != nil || len(shares) != 1 || shares[0].Path != "/home/dora/docs" {
t.Errorf("listed after it ended: %+v, %v", shares, err)
}
if _, err := svc.UnshareFolder(ctx, dora, old.ID, false); !errors.Is(err, auth.ErrNotFound) {
t.Errorf("stopped after it ended: %v", err)
}
purge, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
svc.PurgeExpired(purge, time.Hour, zap.NewNop())
cancel()
renewed, err := svc.ShareFolder(ctx, dora, "/home/dora/old", "erin", false, 0)
if err != nil || renewed.ID == old.ID {
t.Errorf("shared anew after the purge: %+v, %v", renewed, err)
}
if !reads("/home/dora/old/a.txt") || !reads("/home/dora/docs/a.txt") {
t.Error("shared anew")
}
})
}
Loading
Loading