mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-26 20:09:47 +01:00
- GrantRepository keeps three deletes: DeleteForUser, DeleteStaleEpochs
(replaces DeleteOtherEpochs and DeleteIfEpoch) and DeleteIdle. Delete(id)
is gone; the idle path in ResolveGrant now calls DeleteIdle, so a grant
renewed by another node between the read and the delete survives.
settleEpoch deletes the user's grants below the snapshot's epoch, which
is safe outside the transaction because epochs only move forward.
- The "dead grants on an older epoch are only deleted when presented"
policy note moves from the repository to core ListGrants.
- SQL trace logging no longer prints the args of property writes (signing
keys) or user password writes, both encrypted with a key that may be the
public default. The SQL statement is still logged.
- The spec gate rejects JSON body keys that differ from a declared property
only in case. kin-openapi validates exact names while encoding/json
decodes case-insensitively, so {"scopes":[],"Scopes":null} minted a
token with every scope and a "Client" key skipped maxLength.
It also rejects data after the first JSON value, which the handlers'
decoder ignores and which let a body skip the alias check.
- The liveness cache trims its eviction log on evict, not only on put, so
evict-only traffic stays bounded; the floor still drops stale fills.
- createAccessToken, login and setupFirstAdmin declare Cache-Control:
no-store on their success responses.
206 lines
7.4 KiB
Go
206 lines
7.4 KiB
Go
package persistence
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("GrantRepository", func() {
|
|
var ctx context.Context
|
|
var repo model.GrantRepository
|
|
var now time.Time
|
|
|
|
newGrant := func(userID, hash string) *model.Grant {
|
|
return &model.Grant{UserID: userID, Name: "TV", Client: "TestApp", Scopes: model.Scopes{"all"},
|
|
Provider: "password", SecretHash: hash, CreatedAt: now}
|
|
}
|
|
|
|
BeforeEach(func() {
|
|
ctx = log.NewContext(GinkgoT().Context())
|
|
repo = NewGrantRepository(GetDBXBuilder())
|
|
now = time.Now().UTC().Truncate(time.Second)
|
|
DeferCleanup(func() {
|
|
_, _ = GetDBXBuilder().NewQuery("delete from api_grant").Execute()
|
|
})
|
|
})
|
|
|
|
It("stores a grant and finds it by id and by secret hash", func() {
|
|
g := newGrant(adminUser.ID, "hash-1")
|
|
g.Scopes = model.Scopes{"read", "password"}
|
|
Expect(repo.Put(ctx, g)).To(Succeed())
|
|
Expect(g.ID).ToNot(BeEmpty())
|
|
|
|
byID, err := repo.Get(ctx, g.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(byID.Scopes).To(Equal(model.Scopes{"read", "password"}))
|
|
Expect(byID.LastUsedAt).To(BeNil())
|
|
Expect(byID.LastUsedIP).To(BeEmpty())
|
|
|
|
byHash, err := repo.FindBySecretHash(ctx, "hash-1")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(byHash.ID).To(Equal(g.ID))
|
|
})
|
|
|
|
It("returns ErrNotFound for unknown ids and hashes", func() {
|
|
_, err := repo.Get(ctx, "nope")
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
_, err = repo.FindBySecretHash(ctx, "nope")
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
})
|
|
|
|
It("lists and counts only the user's non-idle grants on the given epoch by lastUsedAt, never-used ones last", func() {
|
|
old := newGrant(adminUser.ID, "h-old")
|
|
old.CreatedAt = now.Add(-100 * 24 * time.Hour)
|
|
usedEarly := newGrant(adminUser.ID, "h-used-early")
|
|
usedEarly.CreatedAt = now.Add(-10 * time.Hour)
|
|
earlyUse := now.Add(-5 * time.Hour)
|
|
usedEarly.LastUsedAt = &earlyUse
|
|
usedLate := newGrant(adminUser.ID, "h-used-late")
|
|
usedLate.CreatedAt = now.Add(-10 * time.Hour)
|
|
lateUse := now.Add(-time.Hour)
|
|
usedLate.LastUsedAt = &lateUse
|
|
freshNeverUsed := newGrant(adminUser.ID, "h-fresh") // newer than both uses, but never used
|
|
other := newGrant(regularUser.ID, "h-other")
|
|
staleEpoch := newGrant(adminUser.ID, "h-stale-epoch")
|
|
staleEpoch.UserEpoch = 1
|
|
for _, g := range []*model.Grant{old, usedEarly, usedLate, freshNeverUsed, other, staleEpoch} {
|
|
Expect(repo.Put(ctx, g)).To(Succeed())
|
|
}
|
|
idleSince := now.Add(-90 * 24 * time.Hour)
|
|
|
|
list, err := repo.GetAllForUser(ctx, adminUser.ID, 0, idleSince, 0, 10)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(list).To(HaveLen(3))
|
|
Expect([]string{list[0].ID, list[1].ID, list[2].ID}).To(Equal([]string{usedLate.ID, usedEarly.ID, freshNeverUsed.ID}))
|
|
|
|
Expect(repo.CountForUser(ctx, adminUser.ID, 0, idleSince)).To(Equal(int64(3)))
|
|
|
|
page, err := repo.GetAllForUser(ctx, adminUser.ID, 0, idleSince, 1, 1)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(page).To(HaveLen(1))
|
|
Expect(page[0].ID).To(Equal(usedEarly.ID))
|
|
})
|
|
|
|
It("deletes a grant only for its owner", func() {
|
|
g := newGrant(adminUser.ID, "h-own")
|
|
Expect(repo.Put(ctx, g)).To(Succeed())
|
|
Expect(repo.DeleteForUser(ctx, regularUser.ID, g.ID)).To(MatchError(model.ErrNotFound))
|
|
Expect(repo.DeleteForUser(ctx, adminUser.ID, g.ID)).To(Succeed())
|
|
_, err := repo.Get(ctx, g.ID)
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
})
|
|
|
|
It("moves epochs forward", func() {
|
|
keep := newGrant(adminUser.ID, "h-keep")
|
|
stay := newGrant(adminUser.ID, "h-stay")
|
|
Expect(repo.Put(ctx, keep)).To(Succeed())
|
|
Expect(repo.Put(ctx, stay)).To(Succeed())
|
|
|
|
Expect(repo.SetEpoch(ctx, adminUser.ID, 0, 3, keep.ID)).To(Succeed())
|
|
kept, err := repo.Get(ctx, keep.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(kept.UserEpoch).To(Equal(3))
|
|
stayed, _ := repo.Get(ctx, stay.ID)
|
|
Expect(stayed.UserEpoch).To(Equal(0))
|
|
|
|
Expect(repo.SetEpoch(ctx, adminUser.ID, 3, 4, "")).To(Succeed())
|
|
kept, _ = repo.Get(ctx, keep.ID)
|
|
Expect(kept.UserEpoch).To(Equal(4))
|
|
})
|
|
|
|
It("never moves a grant that is not on fromEpoch", func() {
|
|
stale := newGrant(adminUser.ID, "h-stale") // left behind by an earlier password change
|
|
stale.UserEpoch = 1
|
|
current := newGrant(adminUser.ID, "h-current")
|
|
current.UserEpoch = 2
|
|
Expect(repo.Put(ctx, stale)).To(Succeed())
|
|
Expect(repo.Put(ctx, current)).To(Succeed())
|
|
|
|
Expect(repo.SetEpoch(ctx, adminUser.ID, 2, 3, "")).To(Succeed())
|
|
got, _ := repo.Get(ctx, stale.ID)
|
|
Expect(got.UserEpoch).To(Equal(1))
|
|
got, _ = repo.Get(ctx, current.ID)
|
|
Expect(got.UserEpoch).To(Equal(3))
|
|
})
|
|
|
|
It("deletes only the user's grants on an epoch before the current one", func() {
|
|
older := newGrant(adminUser.ID, "h-older")
|
|
older.UserEpoch = 1
|
|
previous := newGrant(adminUser.ID, "h-previous")
|
|
previous.UserEpoch = 4
|
|
current := newGrant(adminUser.ID, "h-current")
|
|
current.UserEpoch = 5
|
|
otherUser := newGrant(regularUser.ID, "h-other-user")
|
|
otherUser.UserEpoch = 1
|
|
for _, g := range []*model.Grant{older, previous, current, otherUser} {
|
|
Expect(repo.Put(ctx, g)).To(Succeed())
|
|
}
|
|
|
|
Expect(repo.DeleteStaleEpochs(ctx, adminUser.ID, 5)).To(Succeed())
|
|
|
|
for _, g := range []*model.Grant{older, previous} {
|
|
_, err := repo.Get(ctx, g.ID)
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
}
|
|
for _, g := range []*model.Grant{current, otherUser} {
|
|
_, err := repo.Get(ctx, g.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
}
|
|
})
|
|
|
|
It("touches a never-used grant, then throttles until notSince passes", func() {
|
|
g := newGrant(adminUser.ID, "h-touch")
|
|
Expect(repo.Put(ctx, g)).To(Succeed())
|
|
|
|
Expect(repo.Touch(ctx, g.ID, "10.0.0.1", now, now.Add(-5*time.Minute))).To(Succeed())
|
|
got, _ := repo.Get(ctx, g.ID)
|
|
Expect(got.LastUsedAt).ToNot(BeNil())
|
|
Expect(got.LastUsedAt.UTC()).To(BeTemporally("==", now))
|
|
Expect(got.LastUsedIP).To(Equal("10.0.0.1"))
|
|
|
|
later := now.Add(time.Minute)
|
|
Expect(repo.Touch(ctx, g.ID, "10.0.0.2", later, later.Add(-5*time.Minute))).To(Succeed())
|
|
got, _ = repo.Get(ctx, g.ID)
|
|
Expect(got.LastUsedIP).To(Equal("10.0.0.1"))
|
|
|
|
muchLater := now.Add(6 * time.Minute)
|
|
Expect(repo.Touch(ctx, g.ID, "10.0.0.3", muchLater, muchLater.Add(-5*time.Minute))).To(Succeed())
|
|
got, _ = repo.Get(ctx, g.ID)
|
|
Expect(got.LastUsedIP).To(Equal("10.0.0.3"))
|
|
})
|
|
|
|
It("deletes idle grants, using created_at for never-used ones", func() {
|
|
idle := newGrant(adminUser.ID, "h-idle")
|
|
idle.CreatedAt = now.Add(-100 * 24 * time.Hour)
|
|
usedRecently := newGrant(adminUser.ID, "h-used-recently")
|
|
usedRecently.CreatedAt = now.Add(-100 * 24 * time.Hour)
|
|
recentUse := now.Add(-time.Hour)
|
|
usedRecently.LastUsedAt = &recentUse
|
|
Expect(repo.Put(ctx, idle)).To(Succeed())
|
|
Expect(repo.Put(ctx, usedRecently)).To(Succeed())
|
|
|
|
n, err := repo.DeleteIdle(ctx, now.Add(-90*24*time.Hour))
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(n).To(Equal(int64(1)))
|
|
_, err = repo.Get(ctx, usedRecently.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
It("deletes a user's grants when the user is deleted", func() {
|
|
users := NewUserRepository(GetDBXBuilder())
|
|
u := model.User{ID: "grant-owner", UserName: "grant-owner", NewPassword: "pw"}
|
|
Expect(users.Put(ctx, &u)).To(Succeed())
|
|
g := newGrant(u.ID, "h-cascade")
|
|
Expect(repo.Put(ctx, g)).To(Succeed())
|
|
|
|
Expect(users.Delete(request.WithUser(ctx, adminUser), u.ID)).To(Succeed())
|
|
_, err := repo.Get(ctx, g.ID)
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
})
|
|
})
|