Fix tests and lint errors, plus a bit of refactor

This commit is contained in:
Deluan 2023-01-22 12:25:35 -05:00
parent 72a12e344e
commit 94cc2b2ac5
14 changed files with 114 additions and 98 deletions

View file

@ -74,7 +74,7 @@ func (s *shareService) loadMediafiles(ctx context.Context, filter squirrel.Eq, s
func (s *shareService) loadPlaylistTracks(ctx context.Context, id string) (model.MediaFiles, error) { func (s *shareService) loadPlaylistTracks(ctx context.Context, id string) (model.MediaFiles, error) {
// Create a context with a fake admin user, to be able to access playlists // Create a context with a fake admin user, to be able to access playlists
ctx = request.WithUser(context.TODO(), model.User{IsAdmin: true}) ctx = request.WithUser(ctx, model.User{IsAdmin: true})
tracks, err := s.ds.Playlist(ctx).Tracks(id, true).GetAll(model.QueryOptions{Sort: "id"}) tracks, err := s.ds.Playlist(ctx).Tracks(id, true).GetAll(model.QueryOptions{Sort: "id"})
if err != nil { if err != nil {

View file

@ -29,7 +29,7 @@ var _ = Describe("Share", func() {
}) })
Describe("Save", func() { Describe("Save", func() {
It("it adds a random name", func() { It("it sets a random ID", func() {
entity := &model.Share{Description: "test"} entity := &model.Share{Description: "test"}
id, err := repo.Save(entity) id, err := repo.Save(entity)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -44,7 +44,7 @@ var _ = Describe("Share", func() {
err := repo.Update("id", entity) err := repo.Update("id", entity)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(mockedRepo.(*tests.MockShareRepo).Entity).To(Equal("entity")) Expect(mockedRepo.(*tests.MockShareRepo).Entity).To(Equal("entity"))
Expect(mockedRepo.(*tests.MockShareRepo).Cols).To(ConsistOf("description")) Expect(mockedRepo.(*tests.MockShareRepo).Cols).To(ConsistOf("description", "expires_at"))
}) })
}) })
}) })

View file

@ -1,43 +0,0 @@
package public
import (
"context"
"errors"
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/navidrome/navidrome/core/auth"
"github.com/navidrome/navidrome/model"
)
func EncodeArtworkID(artID model.ArtworkID) string {
token, _ := auth.CreatePublicToken(map[string]any{"id": artID.String()})
return token
}
func DecodeArtworkID(tokenString string) (model.ArtworkID, error) {
token, err := auth.TokenAuth.Decode(tokenString)
if err != nil {
return model.ArtworkID{}, err
}
if token == nil {
return model.ArtworkID{}, errors.New("unauthorized")
}
err = jwt.Validate(token, jwt.WithRequiredClaim("id"))
if err != nil {
return model.ArtworkID{}, err
}
claims, err := token.AsMap(context.Background())
if err != nil {
return model.ArtworkID{}, err
}
id, ok := claims["id"].(string)
if !ok {
return model.ArtworkID{}, errors.New("invalid id type")
}
artID, err := model.ParseArtworkID(id)
if err == nil {
return artID, nil
}
// Try to default to mediafile artworkId
return model.ParseArtworkID("mf-" + id)
}

View file

@ -0,0 +1,71 @@
package public
import (
"context"
"errors"
"net/http"
"net/url"
"path/filepath"
"strconv"
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core/auth"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server"
)
func ImageURL(r *http.Request, artID model.ArtworkID, size int) string {
link := encodeArtworkID(artID)
path := filepath.Join(consts.URLPathPublicImages, link)
params := url.Values{}
if size > 0 {
params.Add("size", strconv.Itoa(size))
}
return server.AbsoluteURL(r, path, params)
}
func encodeArtworkID(artID model.ArtworkID) string {
token, _ := auth.CreatePublicToken(map[string]any{"id": artID.String()})
return token
}
func decodeArtworkID(tokenString string) (model.ArtworkID, error) {
token, err := auth.TokenAuth.Decode(tokenString)
if err != nil {
return model.ArtworkID{}, err
}
if token == nil {
return model.ArtworkID{}, errors.New("unauthorized")
}
err = jwt.Validate(token, jwt.WithRequiredClaim("id"))
if err != nil {
return model.ArtworkID{}, err
}
claims, err := token.AsMap(context.Background())
if err != nil {
return model.ArtworkID{}, err
}
id, ok := claims["id"].(string)
if !ok {
return model.ArtworkID{}, errors.New("invalid id type")
}
artID, err := model.ParseArtworkID(id)
if err == nil {
return artID, nil
}
// Try to default to mediafile artworkId (if used with a mediafileShare token)
return model.ParseArtworkID("mf-" + id)
}
func encodeMediafileShare(s model.Share, id string) string {
claims := map[string]any{"id": id}
if s.Format != "" {
claims["f"] = s.Format
}
if s.MaxBitRate != 0 {
claims["b"] = s.MaxBitRate
}
token, _ := auth.CreateExpiringPublicToken(s.ExpiresAt, claims)
return token
}

View file

@ -1,38 +1,38 @@
package public_test package public
import ( import (
"github.com/go-chi/jwtauth/v5" "github.com/go-chi/jwtauth/v5"
"github.com/navidrome/navidrome/core/auth" "github.com/navidrome/navidrome/core/auth"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/public"
. "github.com/onsi/ginkgo/v2" . "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
var _ = Describe("EncodeArtworkID", func() { var _ = Describe("encodeArtworkID", func() {
Context("Public ID Encoding", func() { Context("Public ID Encoding", func() {
BeforeEach(func() { BeforeEach(func() {
auth.TokenAuth = jwtauth.New("HS256", []byte("super secret"), nil) auth.TokenAuth = jwtauth.New("HS256", []byte("super secret"), nil)
}) })
It("returns a reversible string representation", func() { It("returns a reversible string representation", func() {
id := model.NewArtworkID(model.KindArtistArtwork, "1234") id := model.NewArtworkID(model.KindArtistArtwork, "1234")
encoded := public.EncodeArtworkID(id) encoded := encodeArtworkID(id)
decoded, err := public.DecodeArtworkID(encoded) decoded, err := decodeArtworkID(encoded)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(decoded).To(Equal(id)) Expect(decoded).To(Equal(id))
}) })
It("fails to decode an invalid token", func() { It("fails to decode an invalid token", func() {
_, err := public.DecodeArtworkID("xx-123") _, err := decodeArtworkID("xx-123")
Expect(err).To(MatchError("invalid JWT")) Expect(err).To(MatchError("invalid JWT"))
}) })
It("fails to decode an invalid id", func() { It("defaults to kind mediafile", func() {
encoded := public.EncodeArtworkID(model.ArtworkID{}) encoded := encodeArtworkID(model.ArtworkID{})
_, err := public.DecodeArtworkID(encoded) id, err := decodeArtworkID(encoded)
Expect(err).To(MatchError("invalid artwork id")) Expect(err).ToNot(HaveOccurred())
Expect(id.Kind).To(Equal(model.KindMediaFileArtwork))
}) })
It("fails to decode a token without an id", func() { It("fails to decode a token without an id", func() {
token, _ := auth.CreatePublicToken(map[string]any{}) token, _ := auth.CreatePublicToken(map[string]any{})
_, err := public.DecodeArtworkID(token) _, err := decodeArtworkID(token)
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
}) })

View file

@ -21,7 +21,7 @@ func (p *Router) handleImages(w http.ResponseWriter, r *http.Request) {
return return
} }
artId, err := DecodeArtworkID(id) artId, err := decodeArtworkID(id)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return

View file

@ -4,7 +4,6 @@ import (
"errors" "errors"
"net/http" "net/http"
"github.com/navidrome/navidrome/core/auth"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server" "github.com/navidrome/navidrome/server"
@ -39,26 +38,17 @@ func (p *Router) handleShares(w http.ResponseWriter, r *http.Request) {
return return
} }
s = p.mapShareInfo(s) s = p.mapShareInfo(*s)
server.IndexWithShare(p.ds, ui.BuildAssets(), s)(w, r) server.IndexWithShare(p.ds, ui.BuildAssets(), s)(w, r)
} }
func (p *Router) mapShareInfo(s *model.Share) *model.Share { func (p *Router) mapShareInfo(s model.Share) *model.Share {
mapped := &model.Share{ mapped := &model.Share{
Description: s.Description, Description: s.Description,
Tracks: s.Tracks, Tracks: s.Tracks,
} }
for i := range s.Tracks { for i := range s.Tracks {
// TODO Use Encode(Artwork)ID? mapped.Tracks[i].ID = encodeMediafileShare(s, s.Tracks[i].ID)
claims := map[string]any{"id": s.Tracks[i].ID}
if s.Format != "" {
claims["f"] = s.Format
}
if s.MaxBitRate != 0 {
claims["b"] = s.MaxBitRate
}
id, _ := auth.CreateExpiringPublicToken(s.ExpiresAt, claims)
mapped.Tracks[i].ID = id
} }
return mapped return mapped
} }

View file

@ -98,7 +98,7 @@ func decodeStreamInfo(tokenString string) (shareTrackInfo, error) {
} }
resp := shareTrackInfo{} resp := shareTrackInfo{}
resp.id = id resp.id = id
resp.format, ok = claims["f"].(string) resp.format, _ = claims["f"].(string)
resp.bitrate, ok = claims["b"].(int) resp.bitrate, _ = claims["b"].(int)
return resp, nil return resp, nil
} }

View file

@ -10,6 +10,7 @@ import (
"github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/public"
"github.com/navidrome/navidrome/server/subsonic/filter" "github.com/navidrome/navidrome/server/subsonic/filter"
"github.com/navidrome/navidrome/server/subsonic/responses" "github.com/navidrome/navidrome/server/subsonic/responses"
"github.com/navidrome/navidrome/utils" "github.com/navidrome/navidrome/utils"
@ -256,9 +257,9 @@ func (api *Router) GetArtistInfo(r *http.Request) (*responses.Subsonic, error) {
response := newResponse() response := newResponse()
response.ArtistInfo = &responses.ArtistInfo{} response.ArtistInfo = &responses.ArtistInfo{}
response.ArtistInfo.Biography = artist.Biography response.ArtistInfo.Biography = artist.Biography
response.ArtistInfo.SmallImageUrl = publicImageURL(r, artist.CoverArtID(), 160) response.ArtistInfo.SmallImageUrl = public.ImageURL(r, artist.CoverArtID(), 160)
response.ArtistInfo.MediumImageUrl = publicImageURL(r, artist.CoverArtID(), 320) response.ArtistInfo.MediumImageUrl = public.ImageURL(r, artist.CoverArtID(), 320)
response.ArtistInfo.LargeImageUrl = publicImageURL(r, artist.CoverArtID(), 0) response.ArtistInfo.LargeImageUrl = public.ImageURL(r, artist.CoverArtID(), 0)
response.ArtistInfo.LastFmUrl = artist.ExternalUrl response.ArtistInfo.LastFmUrl = artist.ExternalUrl
response.ArtistInfo.MusicBrainzID = artist.MbzArtistID response.ArtistInfo.MusicBrainzID = artist.MbzArtistID
for _, s := range artist.SimilarArtists { for _, s := range artist.SimilarArtists {

View file

@ -5,15 +5,11 @@ import (
"fmt" "fmt"
"mime" "mime"
"net/http" "net/http"
"net/url"
"path/filepath"
"strconv"
"strings" "strings"
"github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/request" "github.com/navidrome/navidrome/model/request"
"github.com/navidrome/navidrome/server"
"github.com/navidrome/navidrome/server/public" "github.com/navidrome/navidrome/server/public"
"github.com/navidrome/navidrome/server/subsonic/responses" "github.com/navidrome/navidrome/server/subsonic/responses"
"github.com/navidrome/navidrome/utils" "github.com/navidrome/navidrome/utils"
@ -92,7 +88,7 @@ func toArtist(r *http.Request, a model.Artist) responses.Artist {
AlbumCount: a.AlbumCount, AlbumCount: a.AlbumCount,
UserRating: a.Rating, UserRating: a.Rating,
CoverArt: a.CoverArtID().String(), CoverArt: a.CoverArtID().String(),
ArtistImageUrl: publicImageURL(r, a.CoverArtID(), 0), ArtistImageUrl: public.ImageURL(r, a.CoverArtID(), 0),
} }
if a.Starred { if a.Starred {
artist.Starred = &a.StarredAt artist.Starred = &a.StarredAt
@ -106,7 +102,7 @@ func toArtistID3(r *http.Request, a model.Artist) responses.ArtistID3 {
Name: a.Name, Name: a.Name,
AlbumCount: a.AlbumCount, AlbumCount: a.AlbumCount,
CoverArt: a.CoverArtID().String(), CoverArt: a.CoverArtID().String(),
ArtistImageUrl: publicImageURL(r, a.CoverArtID(), 0), ArtistImageUrl: public.ImageURL(r, a.CoverArtID(), 0),
UserRating: a.Rating, UserRating: a.Rating,
} }
if a.Starred { if a.Starred {
@ -115,16 +111,6 @@ func toArtistID3(r *http.Request, a model.Artist) responses.ArtistID3 {
return artist return artist
} }
func publicImageURL(r *http.Request, artID model.ArtworkID, size int) string {
link := public.EncodeArtworkID(artID)
path := filepath.Join(consts.URLPathPublicImages, link)
params := url.Values{}
if size > 0 {
params.Add("size", strconv.Itoa(size))
}
return server.AbsoluteURL(r, path, params)
}
func toGenres(genres model.Genres) *responses.Genres { func toGenres(genres model.Genres) *responses.Genres {
response := make([]responses.Genre, len(genres)) response := make([]responses.Genre, len(genres))
for i, g := range genres { for i, g := range genres {

View file

@ -12,6 +12,7 @@ import (
"github.com/deluan/sanitize" "github.com/deluan/sanitize"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/public"
"github.com/navidrome/navidrome/server/subsonic/responses" "github.com/navidrome/navidrome/server/subsonic/responses"
"github.com/navidrome/navidrome/utils" "github.com/navidrome/navidrome/utils"
) )
@ -112,7 +113,7 @@ func (api *Router) Search2(r *http.Request) (*responses.Subsonic, error) {
AlbumCount: artist.AlbumCount, AlbumCount: artist.AlbumCount,
UserRating: artist.Rating, UserRating: artist.Rating,
CoverArt: artist.CoverArtID().String(), CoverArt: artist.CoverArtID().String(),
ArtistImageUrl: publicImageURL(r, artist.CoverArtID(), 0), ArtistImageUrl: public.ImageURL(r, artist.CoverArtID(), 0),
} }
if artist.Starred { if artist.Starred {
searchResult2.Artist[i].Starred = &as[i].StarredAt searchResult2.Artist[i].Starred = &as[i].StarredAt

View file

@ -20,8 +20,12 @@ func (m *MockShareRepo) Save(entity interface{}) (string, error) {
if m.Error != nil { if m.Error != nil {
return "", m.Error return "", m.Error
} }
m.Entity = entity s := entity.(*model.Share)
return "id", nil if s.ID == "" {
s.ID = "id"
}
m.Entity = s
return s.ID, nil
} }
func (m *MockShareRepo) Update(id string, entity interface{}, cols ...string) error { func (m *MockShareRepo) Update(id string, entity interface{}, cols ...string) error {
@ -33,3 +37,10 @@ func (m *MockShareRepo) Update(id string, entity interface{}, cols ...string) er
m.Cols = cols m.Cols = cols
return nil return nil
} }
func (m *MockShareRepo) Exists(id string) (bool, error) {
if m.Error != nil {
return false, m.Error
}
return id == m.ID, nil
}

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,6 @@
import ReactJkMusicPlayer from 'navidrome-music-player' import ReactJkMusicPlayer from 'navidrome-music-player'
import config, { shareInfo } from './config' import { shareInfo } from './config'
import { baseUrl, shareCoverUrl, shareStreamUrl } from './utils' import { shareCoverUrl, shareStreamUrl } from './utils'
const SharePlayer = () => { const SharePlayer = () => {
const list = shareInfo?.tracks.map((s) => { const list = shareInfo?.tracks.map((s) => {