Skip to content

🎧 Your Personal Streaming Service

Expose Last.fm's ApiKey to UI

Deluan1f997357a95a
diff --git a/conf/configuration.go b/conf/configuration.go
index 0e0af4a43..b3ab11c1b 100644
--- a/conf/configuration.go
+++ b/conf/configuration.go
@@ -214,8 +214,8 @@ func init() {
viper.SetDefault("agents", "lastfm,spotify")
viper.SetDefault("lastfm.enabled", true)
viper.SetDefault("lastfm.language", "en")
- viper.SetDefault("lastfm.apikey", "")
- viper.SetDefault("lastfm.secret", "")
+ viper.SetDefault("lastfm.apikey", consts.LastFMAPIKey)
+ viper.SetDefault("lastfm.secret", consts.LastFMAPISecret)
viper.SetDefault("spotify.id", "")
viper.SetDefault("spotify.secret", "")
diff --git a/consts/consts.go b/consts/consts.go
index 4d83e7b21..52477b744 100644
--- a/consts/consts.go
+++ b/consts/consts.go
@@ -61,6 +61,12 @@ const (
DefaultCacheCleanUpInterval = 10 * time.Minute
)
+// Shared secrets (only add here "secrets" that can be public)
+const (
+ LastFMAPIKey = "9b94a5515ea66b2da3ec03c12300327e"
+ LastFMAPISecret = "74cb6557cec7171d921af5d7d887c587" // nolint:gosec
+)
+
var (
DefaultTranscodings = []map[string]interface{}{
{
diff --git a/core/agents/lastfm/lastfm.go b/core/agents/lastfm/agent.go
similarity index 93%
rename from core/agents/lastfm/lastfm.go
rename to core/agents/lastfm/agent.go
index c9c036a36..ddc77e6d5 100644
--- a/core/agents/lastfm/lastfm.go
+++ b/core/agents/lastfm/agent.go
@@ -13,8 +13,6 @@ import (
const (
lastFMAgentName = "lastfm"
- lastFMAPIKey = "9b94a5515ea66b2da3ec03c12300327e"
- lastFMAPISecret = "74cb6557cec7171d921af5d7d887c587" // nolint:gosec
)
type lastfmAgent struct {
@@ -29,12 +27,8 @@ func lastFMConstructor(ctx context.Context) agents.Interface {
l := &lastfmAgent{
ctx: ctx,
lang: conf.Server.LastFM.Language,
- apiKey: lastFMAPIKey,
- secret: lastFMAPISecret,
- }
- if conf.Server.LastFM.ApiKey != "" {
- l.apiKey = conf.Server.LastFM.ApiKey
- l.secret = conf.Server.LastFM.Secret
+ apiKey: conf.Server.LastFM.ApiKey,
+ secret: conf.Server.LastFM.Secret,
}
hc := utils.NewCachedHTTPClient(http.DefaultClient, consts.DefaultCachedHttpClientTTL)
l.client = NewClient(l.apiKey, l.secret, l.lang, hc)
diff --git a/core/agents/lastfm/lastfm_test.go b/core/agents/lastfm/agent_test.go
similarity index 97%
rename from core/agents/lastfm/lastfm_test.go
rename to core/agents/lastfm/agent_test.go
index f4b771e88..f5156368c 100644
--- a/core/agents/lastfm/lastfm_test.go
+++ b/core/agents/lastfm/agent_test.go
@@ -22,18 +22,13 @@ const (
var _ = Describe("lastfmAgent", func() {
Describe("lastFMConstructor", func() {
- It("uses default api key and language if not configured", func() {
- conf.Server.LastFM.ApiKey = ""
- agent := lastFMConstructor(context.Background())
- Expect(agent.(*lastfmAgent).apiKey).To(Equal(lastFMAPIKey))
- Expect(agent.(*lastfmAgent).lang).To(Equal("en"))
- })
-
It("uses configured api key and language", func() {
conf.Server.LastFM.ApiKey = "123"
+ conf.Server.LastFM.Secret = "secret"
conf.Server.LastFM.Language = "pt"
agent := lastFMConstructor(context.Background())
Expect(agent.(*lastfmAgent).apiKey).To(Equal("123"))
+ Expect(agent.(*lastfmAgent).secret).To(Equal("secret"))
Expect(agent.(*lastfmAgent).lang).To(Equal("pt"))
})
})
diff --git a/core/agents/lastfm/auth_router.go b/core/agents/lastfm/auth_router.go
index 1dee44e57..4cd4f41a1 100644
--- a/core/agents/lastfm/auth_router.go
+++ b/core/agents/lastfm/auth_router.go
@@ -30,13 +30,13 @@ type Router struct {
}
func NewRouter(ds model.DataStore) *Router {
- r := &Router{ds: ds, apiKey: lastFMAPIKey, secret: lastFMAPISecret}
+ r := &Router{
+ ds: ds,
+ apiKey: conf.Server.LastFM.ApiKey,
+ secret: conf.Server.LastFM.Secret,
+ }
r.sessionKeys = &sessionKeys{ds: ds}
r.Handler = r.routes()
- if conf.Server.LastFM.ApiKey != "" {
- r.apiKey = conf.Server.LastFM.ApiKey
- r.secret = conf.Server.LastFM.Secret
- }
r.client = NewClient(r.apiKey, r.secret, "en", http.DefaultClient)
return r
}
diff --git a/server/serve_index.go b/server/serve_index.go
index ab1d79a92..d639cd108 100644
--- a/server/serve_index.go
+++ b/server/serve_index.go
@@ -45,6 +45,7 @@ func serveIndex(ds model.DataStore, fs fs.FS) http.HandlerFunc {
"enableUserEditing": conf.Server.EnableUserEditing,
"devEnableShare": conf.Server.DevEnableShare,
"devEnableScrobble": conf.Server.DevEnableScrobble,
+ "lastFMApiKey": conf.Server.LastFM.ApiKey,
}
auth := handleLoginFromHeaders(ds, r)
if auth != nil {
diff --git a/server/serve_index_test.go b/server/serve_index_test.go
index 2e54d34ca..f524801b3 100644
--- a/server/serve_index_test.go
+++ b/server/serve_index_test.go
@@ -209,6 +209,17 @@ var _ = Describe("serveIndex", func() {
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("devEnableScrobble", false))
})
+
+ It("sets the lastFMApiKey", func() {
+ conf.Server.LastFM.ApiKey = "APIKEY-123"
+ r := httptest.NewRequest("GET", "/index.html", nil)
+ w := httptest.NewRecorder()
+
+ serveIndex(ds, fs)(w, r)
+
+ config := extractAppConfig(w.Body.String())
+ Expect(config).To(HaveKeyWithValue("lastFMApiKey", "APIKEY-123"))
+ })
})
var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__="([^"]*)`)
diff --git a/ui/src/config.js b/ui/src/config.js
index 0d4e86e84..102ee3843 100644
--- a/ui/src/config.js
+++ b/ui/src/config.js
@@ -20,6 +20,7 @@ const defaultConfig = {
enableUserEditing: true,
devEnableShare: true,
devEnableScrobble: true,
+ lastFMApiKey: '9b94a5515ea66b2da3ec03c12300327e',
}
let config