2020-02-06 16:48:35 -05:00
|
|
|
package auth_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2021-04-30 10:00:03 -04:00
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
|
|
|
"github.com/navidrome/navidrome/consts"
|
2020-07-10 12:45:58 -04:00
|
|
|
"github.com/navidrome/navidrome/core/auth"
|
2020-02-06 16:48:35 -05:00
|
|
|
"github.com/navidrome/navidrome/log"
|
2021-05-11 17:21:18 -04:00
|
|
|
"github.com/navidrome/navidrome/model"
|
2024-12-05 21:40:34 -05:00
|
|
|
"github.com/navidrome/navidrome/tests"
|
2022-07-26 16:47:16 -04:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
2020-02-06 16:48:35 -05:00
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestAuth(t *testing.T) {
|
2022-12-21 14:53:36 -05:00
|
|
|
log.SetLevel(log.LevelFatal)
|
2020-02-06 16:48:35 -05:00
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
|
RunSpecs(t, "Auth Test Suite")
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 10:00:03 -04:00
|
|
|
const (
|
2026-05-19 18:23:36 -03:00
|
|
|
oneDay = 24 * time.Hour
|
2021-04-30 10:00:03 -04:00
|
|
|
)
|
2020-02-06 16:48:35 -05:00
|
|
|
|
2022-07-26 16:47:16 -04:00
|
|
|
var _ = BeforeSuite(func() {
|
|
|
|
|
conf.Server.SessionTimeout = 2 * oneDay
|
|
|
|
|
})
|
|
|
|
|
|
2020-02-06 16:48:35 -05:00
|
|
|
var _ = Describe("Auth", func() {
|
2021-04-30 10:00:03 -04:00
|
|
|
|
2020-02-06 16:48:35 -05:00
|
|
|
BeforeEach(func() {
|
2024-12-05 21:40:34 -05:00
|
|
|
ds := &tests.MockDataStore{
|
|
|
|
|
MockedProperty: &tests.MockedPropertyRepo{},
|
|
|
|
|
}
|
|
|
|
|
auth.Init(ds)
|
2020-02-06 16:48:35 -05:00
|
|
|
})
|
2021-04-30 10:00:03 -04:00
|
|
|
|
|
|
|
|
Describe("Validate", func() {
|
2020-02-06 16:48:35 -05:00
|
|
|
It("returns error with an invalid JWT token", func() {
|
|
|
|
|
_, err := auth.Validate("invalid.token")
|
2021-05-11 17:21:18 -04:00
|
|
|
Expect(err).To(HaveOccurred())
|
2020-02-06 16:48:35 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("returns the claims from a valid JWT token", func() {
|
2026-02-08 08:57:30 -06:00
|
|
|
claims := map[string]any{}
|
2020-02-06 16:48:35 -05:00
|
|
|
claims["iss"] = "issuer"
|
2021-05-11 17:21:18 -04:00
|
|
|
claims["iat"] = time.Now().Unix()
|
2020-02-06 16:48:35 -05:00
|
|
|
claims["exp"] = time.Now().Add(1 * time.Minute).Unix()
|
2021-05-11 17:21:18 -04:00
|
|
|
_, tokenStr, err := auth.TokenAuth.Encode(claims)
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2020-02-06 16:48:35 -05:00
|
|
|
|
|
|
|
|
decodedClaims, err := auth.Validate(tokenStr)
|
2021-05-11 17:21:18 -04:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2026-03-02 13:15:31 -05:00
|
|
|
Expect(decodedClaims.Issuer).To(Equal("issuer"))
|
2020-02-06 16:48:35 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("returns ErrExpired if the `exp` field is in the past", func() {
|
2026-02-08 08:57:30 -06:00
|
|
|
claims := map[string]any{}
|
2020-02-06 16:48:35 -05:00
|
|
|
claims["iss"] = "issuer"
|
|
|
|
|
claims["exp"] = time.Now().Add(-1 * time.Minute).Unix()
|
2021-05-11 17:21:18 -04:00
|
|
|
_, tokenStr, err := auth.TokenAuth.Encode(claims)
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2020-02-06 16:48:35 -05:00
|
|
|
|
2021-05-11 17:21:18 -04:00
|
|
|
_, err = auth.Validate(tokenStr)
|
|
|
|
|
Expect(err).To(MatchError("token is expired"))
|
2020-02-06 16:48:35 -05:00
|
|
|
})
|
|
|
|
|
})
|
2021-04-30 10:00:03 -04:00
|
|
|
|
|
|
|
|
Describe("CreateToken", func() {
|
|
|
|
|
It("creates a valid token", func() {
|
|
|
|
|
u := &model.User{
|
|
|
|
|
ID: "123",
|
|
|
|
|
UserName: "johndoe",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
}
|
|
|
|
|
tokenStr, err := auth.CreateToken(u)
|
2021-05-11 17:21:18 -04:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2021-04-30 10:00:03 -04:00
|
|
|
|
|
|
|
|
claims, err := auth.Validate(tokenStr)
|
2021-05-11 17:21:18 -04:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2021-04-30 10:00:03 -04:00
|
|
|
|
2026-03-02 13:15:31 -05:00
|
|
|
Expect(claims.Issuer).To(Equal(consts.JWTIssuer))
|
|
|
|
|
Expect(claims.Subject).To(Equal("johndoe"))
|
|
|
|
|
Expect(claims.UserID).To(Equal("123"))
|
|
|
|
|
Expect(claims.IsAdmin).To(Equal(true))
|
|
|
|
|
Expect(claims.ExpiresAt).To(BeTemporally(">", time.Now()))
|
2021-04-30 10:00:03 -04: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).
2026-08-02 12:58:53 -04:00
|
|
|
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())
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-04-30 10:00:03 -04:00
|
|
|
Describe("TouchToken", func() {
|
|
|
|
|
It("updates the expiration time", func() {
|
|
|
|
|
yesterday := time.Now().Add(-oneDay)
|
2026-02-08 08:57:30 -06:00
|
|
|
claims := map[string]any{}
|
2021-04-30 10:00:03 -04:00
|
|
|
claims["iss"] = "issuer"
|
|
|
|
|
claims["exp"] = yesterday.Unix()
|
2021-05-11 17:21:18 -04:00
|
|
|
token, _, err := auth.TokenAuth.Encode(claims)
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2021-04-30 10:00:03 -04:00
|
|
|
|
|
|
|
|
touched, err := auth.TouchToken(token)
|
2021-05-11 17:21:18 -04:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2021-04-30 10:00:03 -04:00
|
|
|
|
|
|
|
|
decodedClaims, err := auth.Validate(touched)
|
2021-05-11 17:21:18 -04:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2026-03-02 13:15:31 -05:00
|
|
|
Expect(decodedClaims.ExpiresAt.Sub(yesterday)).To(BeNumerically(">=", oneDay))
|
2021-04-30 10:00:03 -04:00
|
|
|
})
|
|
|
|
|
})
|
feat(jellyfin): non-expiring, audience-scoped tokens revocable by password change (#6013)
* feat(auth): add per-user token_epoch column and bump method
* feat(auth): add aud and ep claims, omitted when zero
* feat(auth): add CreateAPIToken for non-expiring, audience-scoped tokens
* feat(auth): add CheckClaims for epoch and audience validation
* feat(jellyfin): issue non-expiring, jellyfin-scoped access tokens
* fix(subsonic): reject API-scoped and revoked tokens on the jwt path
* fix(server): reject API-scoped and revoked tokens on the native API
* fix(server): pin the token-subject guard and stop leaking test config
Adds a regression spec for the DevAutoLogin/ExtAuth guard in
tokenAllowed, switches its comparison to case-insensitive to match
the user lookup's own COLLATE NOCASE semantics, and restores Subsonic
JWT test config after each spec instead of leaking SessionTimeout.
* feat(request): add a token epoch holder for handler-to-middleware signalling
* refactor(server): write the refreshed JWT header after the handler runs
* feat(auth): revoke all tokens for a user when their password changes
* fix(server): restore Unwrap on the JWT refresh writer so SSE write deadlines apply
* test(auth): pin that non-session tokens reject API access tokens
* test(jellyfin): pin token scoping and epoch revocation end to end
Exercises auth.CreateAPIToken and CheckClaims against the real Jellyfin
router and SQLite DB: the minted token has no exp and is aud-scoped to
jellyfin, and bumping token_epoch through the real UserRepository revokes
an already-issued token on the next protected request.
* test(nativeapi): pin the token-epoch handoff through a real password-change request
Drive a self password change through the real Authenticator/JWTRefresher
chain and a real SQLite-backed userRepository, so the epoch handoff between
Put and the refreshed-token writer is verified end to end, not as two
separately-tested halves. Also fix tokenAllowed to read the enriched ctx it
was given instead of r.Context(), so its warning log carries the username.
* refactor(server): drop tokenAllowed's now-unused request parameter
Finding-2 already moved every use to ctx; r was dead weight. Also note
in the new nativeapi test why it must stay the package's only real-DB
spec: db.Db() is a process-wide singleton its cleanup closes for good.
* refactor(auth): remove duplication in claim decoding and token minting
* refactor(auth): group aud with the standard JWT claims
* refactor(auth): read aud with the standard-claim accessor pattern
* fix(log): redact every api_key spelling the Jellyfin API accepts
* fix(auth): bind session tokens to the user id, not just the username
* fix(auth): return the token epoch from the same atomic increment
* fix(auth): bump the token epoch in the same statement as the password write
* chore(auth): trim comments to the why-only budget
2026-08-22 20:36:24 -04:00
|
|
|
|
|
|
|
|
Describe("CreateAPIToken", func() {
|
|
|
|
|
var usr *model.User
|
|
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
|
usr = &model.User{ID: "123", UserName: "johndoe", TokenEpoch: 4}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("does not expire", func() {
|
|
|
|
|
tokenStr, err := auth.CreateAPIToken(usr, auth.AudienceJellyfin)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
|
|
claims, err := auth.Validate(tokenStr)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(claims.ExpiresAt.IsZero()).To(BeTrue())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("carries the audience and the user's epoch", func() {
|
|
|
|
|
tokenStr, err := auth.CreateAPIToken(usr, auth.AudienceJellyfin)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
|
|
claims, err := auth.Validate(tokenStr)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(claims.Audience).To(Equal([]string{"jellyfin"}))
|
|
|
|
|
Expect(claims.Epoch).To(Equal(4))
|
|
|
|
|
Expect(claims.Subject).To(Equal("johndoe"))
|
|
|
|
|
Expect(claims.UserID).To(Equal("123"))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Describe("CreateToken with an epoch", func() {
|
|
|
|
|
It("carries the epoch and still expires", func() {
|
|
|
|
|
usr := &model.User{ID: "123", UserName: "johndoe", TokenEpoch: 9}
|
|
|
|
|
tokenStr, err := auth.CreateToken(usr)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
|
|
claims, err := auth.Validate(tokenStr)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(claims.Epoch).To(Equal(9))
|
|
|
|
|
Expect(claims.Audience).To(BeEmpty())
|
|
|
|
|
Expect(claims.ExpiresAt).To(BeTemporally(">", time.Now()))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Describe("TouchClaims", func() {
|
|
|
|
|
It("preserves custom claims and refreshes the expiry", func() {
|
|
|
|
|
tokenStr, err := auth.TouchClaims(auth.Claims{Subject: "johndoe", UserID: "123", Epoch: 5})
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
|
|
claims, err := auth.Validate(tokenStr)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(claims.Epoch).To(Equal(5))
|
|
|
|
|
Expect(claims.Subject).To(Equal("johndoe"))
|
|
|
|
|
Expect(claims.ExpiresAt).To(BeTemporally(">", time.Now()))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Describe("CheckClaims", func() {
|
|
|
|
|
usr := model.User{ID: "123", UserName: "johndoe", TokenEpoch: 2}
|
|
|
|
|
|
|
|
|
|
It("accepts a matching epoch and audience", func() {
|
|
|
|
|
c := auth.Claims{Epoch: 2, Audience: []string{auth.AudienceJellyfin}}
|
|
|
|
|
Expect(auth.CheckClaims(c, usr, auth.AudienceJellyfin)).To(Succeed())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("accepts a token with no audience on any API", func() {
|
|
|
|
|
c := auth.Claims{Epoch: 2}
|
|
|
|
|
Expect(auth.CheckClaims(c, usr, auth.AudienceNative)).To(Succeed())
|
|
|
|
|
Expect(auth.CheckClaims(c, usr, auth.AudienceJellyfin)).To(Succeed())
|
|
|
|
|
Expect(auth.CheckClaims(c, usr, auth.AudienceSubsonic)).To(Succeed())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("rejects a stale epoch", func() {
|
|
|
|
|
c := auth.Claims{Epoch: 1, Audience: []string{auth.AudienceJellyfin}}
|
|
|
|
|
Expect(auth.CheckClaims(c, usr, auth.AudienceJellyfin)).To(MatchError(auth.ErrTokenRevoked))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("rejects a token minted for another API", func() {
|
|
|
|
|
c := auth.Claims{Epoch: 2, Audience: []string{auth.AudienceJellyfin}}
|
|
|
|
|
Expect(auth.CheckClaims(c, usr, auth.AudienceNative)).To(MatchError(auth.ErrWrongAudience))
|
|
|
|
|
Expect(auth.CheckClaims(c, usr, auth.AudienceSubsonic)).To(MatchError(auth.ErrWrongAudience))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("accepts a multi-audience token that includes this API", func() {
|
|
|
|
|
c := auth.Claims{Epoch: 2, Audience: []string{"other", auth.AudienceNative}}
|
|
|
|
|
Expect(auth.CheckClaims(c, usr, auth.AudienceNative)).To(Succeed())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("accepts a pre-upgrade token against a never-bumped user", func() {
|
|
|
|
|
fresh := model.User{ID: "456", UserName: "newbie"}
|
|
|
|
|
Expect(auth.CheckClaims(auth.Claims{}, fresh, auth.AudienceNative)).To(Succeed())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("accepts a token whose user id matches", func() {
|
|
|
|
|
c := auth.Claims{UserID: "123", Epoch: 2}
|
|
|
|
|
Expect(auth.CheckClaims(c, usr, auth.AudienceNative)).To(Succeed())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("rejects a token for a deleted user recreated under the same name", func() {
|
|
|
|
|
recreated := model.User{ID: "new-random-id", UserName: "johndoe"}
|
|
|
|
|
c := auth.Claims{UserID: "123", Audience: []string{auth.AudienceJellyfin}}
|
|
|
|
|
Expect(auth.CheckClaims(c, recreated, auth.AudienceJellyfin)).To(MatchError(auth.ErrWrongUser))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("accepts a token that carries no user id", func() {
|
|
|
|
|
fresh := model.User{ID: "456", UserName: "newbie"}
|
|
|
|
|
Expect(auth.CheckClaims(auth.Claims{}, fresh, auth.AudienceNative)).To(Succeed())
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-02-06 16:48:35 -05:00
|
|
|
})
|