quintodrome/core/auth/auth_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
2.8 KiB
Go
Raw Normal View History

2020-02-06 12:48:35 -09:00
package auth_test
import (
"testing"
"time"
2021-04-30 06:00:03 -08:00
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core/auth"
2020-02-06 12:48:35 -09:00
"github.com/navidrome/navidrome/log"
2021-05-11 13:21:18 -08:00
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/tests"
2022-07-26 12:47:16 -08:00
. "github.com/onsi/ginkgo/v2"
2020-02-06 12:48:35 -09:00
. "github.com/onsi/gomega"
)
func TestAuth(t *testing.T) {
log.SetLevel(log.LevelFatal)
2020-02-06 12:48:35 -09:00
RegisterFailHandler(Fail)
RunSpecs(t, "Auth Test Suite")
}
2021-04-30 06:00:03 -08:00
const (
testJWTSecret = "not so secret"
oneDay = 24 * time.Hour
)
2020-02-06 12:48:35 -09:00
2022-07-26 12:47:16 -08:00
var _ = BeforeSuite(func() {
conf.Server.SessionTimeout = 2 * oneDay
})
2020-02-06 12:48:35 -09:00
var _ = Describe("Auth", func() {
2021-04-30 06:00:03 -08:00
2020-02-06 12:48:35 -09:00
BeforeEach(func() {
ds := &tests.MockDataStore{
MockedProperty: &tests.MockedPropertyRepo{},
}
auth.Init(ds)
2020-02-06 12:48:35 -09:00
})
2021-04-30 06:00:03 -08:00
Describe("Validate", func() {
2020-02-06 12:48:35 -09:00
It("returns error with an invalid JWT token", func() {
_, err := auth.Validate("invalid.token")
2021-05-11 13:21:18 -08:00
Expect(err).To(HaveOccurred())
2020-02-06 12:48:35 -09:00
})
It("returns the claims from a valid JWT token", func() {
2026-02-08 05:57:30 -09:00
claims := map[string]any{}
2020-02-06 12:48:35 -09:00
claims["iss"] = "issuer"
2021-05-11 13:21:18 -08:00
claims["iat"] = time.Now().Unix()
2020-02-06 12:48:35 -09:00
claims["exp"] = time.Now().Add(1 * time.Minute).Unix()
2021-05-11 13:21:18 -08:00
_, tokenStr, err := auth.TokenAuth.Encode(claims)
Expect(err).NotTo(HaveOccurred())
2020-02-06 12:48:35 -09:00
decodedClaims, err := auth.Validate(tokenStr)
2021-05-11 13:21:18 -08:00
Expect(err).NotTo(HaveOccurred())
Expect(decodedClaims.Issuer).To(Equal("issuer"))
2020-02-06 12:48:35 -09:00
})
It("returns ErrExpired if the `exp` field is in the past", func() {
2026-02-08 05:57:30 -09:00
claims := map[string]any{}
2020-02-06 12:48:35 -09:00
claims["iss"] = "issuer"
claims["exp"] = time.Now().Add(-1 * time.Minute).Unix()
2021-05-11 13:21:18 -08:00
_, tokenStr, err := auth.TokenAuth.Encode(claims)
Expect(err).NotTo(HaveOccurred())
2020-02-06 12:48:35 -09:00
2021-05-11 13:21:18 -08:00
_, err = auth.Validate(tokenStr)
Expect(err).To(MatchError("token is expired"))
2020-02-06 12:48:35 -09:00
})
})
2021-04-30 06:00:03 -08: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 13:21:18 -08:00
Expect(err).NotTo(HaveOccurred())
2021-04-30 06:00:03 -08:00
claims, err := auth.Validate(tokenStr)
2021-05-11 13:21:18 -08:00
Expect(err).NotTo(HaveOccurred())
2021-04-30 06:00:03 -08: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 06:00:03 -08:00
})
})
Describe("TouchToken", func() {
It("updates the expiration time", func() {
yesterday := time.Now().Add(-oneDay)
2026-02-08 05:57:30 -09:00
claims := map[string]any{}
2021-04-30 06:00:03 -08:00
claims["iss"] = "issuer"
claims["exp"] = yesterday.Unix()
2021-05-11 13:21:18 -08:00
token, _, err := auth.TokenAuth.Encode(claims)
Expect(err).NotTo(HaveOccurred())
2021-04-30 06:00:03 -08:00
touched, err := auth.TouchToken(token)
2021-05-11 13:21:18 -08:00
Expect(err).NotTo(HaveOccurred())
2021-04-30 06:00:03 -08:00
decodedClaims, err := auth.Validate(touched)
2021-05-11 13:21:18 -08:00
Expect(err).NotTo(HaveOccurred())
Expect(decodedClaims.ExpiresAt.Sub(yesterday)).To(BeNumerically(">=", oneDay))
2021-04-30 06:00:03 -08:00
})
})
2020-02-06 12:48:35 -09:00
})