mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-08 02:17:25 +02:00
refactor(db): migrate all ids to a uniform canonical 128-bit base62 encoding (#5824)
* refactor(model): extract canonical 128-bit base62 id codec * feat(model): generate random ids as canonical 128-bit base62 values * feat(scanner): emit legacy PIDs in canonical base62 encoding * feat(db): add id canonicalization transform for the uniform-ids migration * feat(db): migrate all ids to canonical 128-bit base62 encoding * fix(db): canonicalize ids in junction tables and JSON columns * chore(jellyfin): update id-family notes for uniform canonical ids * test(ids): harden codec input contract and migration edge coverage * refactor(model): use log.Fatal for Encode128 contract guard per project convention * fix(db): force full rescan after id migration for legacy PID configs * test(db): guard id-column inventory against schema drift * refactor(ids): compile-time Encode128 contract and unified column rewrite helper * refactor(db): apply review feedback to id migration Filter empty strings in collectColumn's SQL, reuse a prepared statement for rewriteColumn updates, and clarify the legacy ID functions' comment now that they emit the canonical encoding. * feat(auth): split session and public-link JWT secrets, rotating sessions on id migration * test(subsonic): initialize public token secret in helpers suite The suite sets auth.TokenAuth directly instead of calling auth.Init, so the new PublicTokenAuth was nil whenever Ginkgo's spec order ran a helpers spec before any spec that calls auth.Init, panicking in publicurl.ImageURL. * refactor(db): inline canonicalID into its only consumer, the uniform-ids migration * refactor(model): rename Encode128/Decode128 to Encode/Decode With every id now exactly 128 bits, the width suffix is redundant; the package-qualified id.Encode/id.Decode carries the same information. * test(db): make the id-columns guard classify JSON columns too The guard only inspected columns named id/pid/*_id, so it could not see ids embedded in JSON. Widen it to *_ids and to every JSON column, and drive the "covered" set from a new embeddedIDColumns list instead of the inline calls in the migration. Every JSON column the schema has now carries a verdict. The four denormalized caches -- media_file/album.participants, media_file/album.tags, album.folder_ids and artist.similar_artists -- hold only artist, tag and folder ids. Those all come from id.NewHash, whose 22-char base62 encoding of a 128-bit MD5 is already in canonical range, so canonicalID is the identity on them and the migration correctly leaves them alone. A new codec test pins that invariant, since the exemptions depend on it. Verified on a copy of a 727MB/96k-track production database: canonicalizing those four columns changed zero rows, and artist, tag and folder ids were themselves unchanged by the migration (only media_file ids moved, 95108 of 96666).
This commit is contained in:
parent
b40b41584a
commit
f853ca604a
25 changed files with 957 additions and 59 deletions
|
|
@ -19,35 +19,41 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
once sync.Once
|
||||
// TokenAuth signs UI/API session tokens. Rotated by the id migration so stale sessions die.
|
||||
TokenAuth *jwtauth.JWTAuth
|
||||
// PublicTokenAuth signs public-link tokens (artwork, share streams), on a separate secret that survives the rotation.
|
||||
PublicTokenAuth *jwtauth.JWTAuth
|
||||
)
|
||||
|
||||
// Init creates a JWTAuth object from the secret stored in the DB.
|
||||
// If the secret is not found, it will create a new one and store it in the DB.
|
||||
// Init creates the JWTAuth objects from the secrets stored in the DB.
|
||||
// Missing or undecryptable secrets are regenerated and stored.
|
||||
func Init(ds model.DataStore) {
|
||||
once.Do(func() {
|
||||
ctx := context.TODO()
|
||||
log.Info("Setting Session Timeout", "value", conf.Server.SessionTimeout)
|
||||
|
||||
secret, err := ds.Property(ctx).Get(consts.JWTSecretKey)
|
||||
if err != nil || secret == "" {
|
||||
log.Info(ctx, "Creating new JWT secret, used for encrypting UI sessions")
|
||||
secret = createNewSecret(ctx, ds)
|
||||
} else {
|
||||
if secret, err = utils.Decrypt(ctx, getEncKey(), secret); err != nil {
|
||||
log.Error(ctx, "Could not decrypt JWT secret, creating a new one", err)
|
||||
secret = createNewSecret(ctx, ds)
|
||||
}
|
||||
}
|
||||
|
||||
TokenAuth = jwtauth.New("HS256", []byte(secret), nil)
|
||||
TokenAuth = jwtauth.New("HS256", []byte(loadOrCreateSecret(ctx, ds, consts.JWTSecretKey)), nil)
|
||||
PublicTokenAuth = jwtauth.New("HS256", []byte(loadOrCreateSecret(ctx, ds, consts.JWTPublicSecretKey)), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func loadOrCreateSecret(ctx context.Context, ds model.DataStore, key string) string {
|
||||
secret, err := ds.Property(ctx).Get(key)
|
||||
if err != nil || secret == "" {
|
||||
log.Info(ctx, "Creating new JWT secret", "key", key)
|
||||
return createNewSecret(ctx, ds, key)
|
||||
}
|
||||
if secret, err = utils.Decrypt(ctx, getEncKey(), secret); err != nil {
|
||||
log.Error(ctx, "Could not decrypt JWT secret, creating a new one", "key", key, err)
|
||||
return createNewSecret(ctx, ds, key)
|
||||
}
|
||||
return secret
|
||||
}
|
||||
|
||||
func CreatePublicToken(claims Claims) (string, error) {
|
||||
claims.Issuer = consts.JWTIssuer
|
||||
_, token, err := TokenAuth.Encode(claims.ToMap())
|
||||
_, token, err := PublicTokenAuth.Encode(claims.ToMap())
|
||||
return token, err
|
||||
}
|
||||
|
||||
|
|
@ -56,7 +62,7 @@ func CreateExpiringPublicToken(exp time.Time, claims Claims) (string, error) {
|
|||
if !exp.IsZero() {
|
||||
claims.ExpiresAt = exp
|
||||
}
|
||||
_, token, err := TokenAuth.Encode(claims.ToMap())
|
||||
_, token, err := PublicTokenAuth.Encode(claims.ToMap())
|
||||
return token, err
|
||||
}
|
||||
|
||||
|
|
@ -91,6 +97,15 @@ func Validate(tokenStr string) (Claims, error) {
|
|||
return ClaimsFromToken(token), nil
|
||||
}
|
||||
|
||||
// ValidatePublic verifies a public-link token against the public secret.
|
||||
func ValidatePublic(tokenStr string) (Claims, error) {
|
||||
token, err := jwtauth.VerifyToken(PublicTokenAuth, tokenStr)
|
||||
if err != nil {
|
||||
return Claims{}, err
|
||||
}
|
||||
return ClaimsFromToken(token), nil
|
||||
}
|
||||
|
||||
func WithAdminUser(ctx context.Context, ds model.DataStore) context.Context {
|
||||
u, err := ds.User(ctx).FindFirstAdmin()
|
||||
if err != nil {
|
||||
|
|
@ -107,14 +122,14 @@ func WithAdminUser(ctx context.Context, ds model.DataStore) context.Context {
|
|||
return request.WithUser(ctx, *u)
|
||||
}
|
||||
|
||||
func createNewSecret(ctx context.Context, ds model.DataStore) string {
|
||||
func createNewSecret(ctx context.Context, ds model.DataStore, key string) string {
|
||||
secret := id.NewRandom()
|
||||
encSecret, err := utils.Encrypt(ctx, getEncKey(), secret)
|
||||
if err != nil {
|
||||
log.Error(ctx, "Could not encrypt JWT secret", err)
|
||||
return secret
|
||||
}
|
||||
if err := ds.Property(ctx).Put(consts.JWTSecretKey, encSecret); err != nil {
|
||||
if err := ds.Property(ctx).Put(key, encSecret); err != nil {
|
||||
log.Error(ctx, "Could not save JWT secret in DB", err)
|
||||
}
|
||||
return secret
|
||||
|
|
|
|||
|
|
@ -89,6 +89,51 @@ var _ = Describe("Auth", func() {
|
|||
})
|
||||
})
|
||||
|
||||
Describe("Session/Public secret split", func() {
|
||||
claims := func() map[string]any {
|
||||
return map[string]any{"iss": "issuer", "exp": time.Now().Add(1 * time.Minute).Unix()}
|
||||
}
|
||||
|
||||
It("verifies a session token via Validate but not ValidatePublic", func() {
|
||||
_, tokenStr, err := auth.TokenAuth.Encode(claims())
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
_, err = auth.Validate(tokenStr)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
_, err = auth.ValidatePublic(tokenStr)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("verifies a public token via ValidatePublic but not Validate", func() {
|
||||
_, tokenStr, err := auth.PublicTokenAuth.Encode(claims())
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
_, err = auth.ValidatePublic(tokenStr)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
_, err = auth.Validate(tokenStr)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("decodes public tokens minted by CreatePublicToken via PublicTokenAuth", func() {
|
||||
tokenStr, err := auth.CreatePublicToken(auth.Claims{ID: "art-1"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
claims, err := auth.ValidatePublic(tokenStr)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(claims.ID).To(Equal("art-1"))
|
||||
_, err = auth.Validate(tokenStr)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("decodes expiring public tokens minted by CreateExpiringPublicToken via PublicTokenAuth", func() {
|
||||
exp := time.Now().Add(1 * time.Hour)
|
||||
tokenStr, err := auth.CreateExpiringPublicToken(exp, auth.Claims{ID: "art-2"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
claims, err := auth.ValidatePublic(tokenStr)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(claims.ID).To(Equal("art-2"))
|
||||
_, err = auth.Validate(tokenStr)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("TouchToken", func() {
|
||||
It("updates the expiration time", func() {
|
||||
yesterday := time.Now().Add(-oneDay)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue