From db149b1ca8ced9f37565a0659a75397e8b3cd3bb Mon Sep 17 00:00:00 2001 From: Jules Michael Date: Mon, 22 Mar 2021 13:09:09 +0400 Subject: [PATCH] refactor: protect spoty pkg deps --- server/route.go | 17 +++-------------- spoty/spoty.go | 33 +++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/server/route.go b/server/route.go index 028d11d..2065fa2 100644 --- a/server/route.go +++ b/server/route.go @@ -22,7 +22,7 @@ type Error struct { // @Description checks if server is running // @Tags core // @Produce json -// @Success 200 {object} Success +// @Success 200 {object} server.Success // @Router /api [get] func (s *Server) handleHealthCheck() gin.HandlerFunc { return func(c *gin.Context) { @@ -92,7 +92,7 @@ func (s *Server) handleCurrentTrackImages(c *gin.Context) { // @Failure 403 {object} server.Error "already authenticated" // @Router /api/authenticate [get] func (s *Server) handleAuthenticate(c *gin.Context) { - c.Redirect(http.StatusFound, s.spoty.Auth.AuthURL(s.spoty.State)) + c.Redirect(http.StatusFound, s.spoty.AuthURL()) } // handleCallback godoc @@ -108,21 +108,10 @@ func (s *Server) handleAuthenticate(c *gin.Context) { // @Failure 404 {object} server.Error "could not retrieve current user" // @Router /api/callback [get] func (s *Server) handleCallback(c *gin.Context) { - tok, err := s.spoty.Auth.Token(s.spoty.State, c.Request) - if err != nil { + if err := s.spoty.SetupNewClient(c.Request); err != nil { c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "could not retrieve token"}) return } - client := s.spoty.Auth.NewClient(tok) - if _, err := client.CurrentUser(); err != nil { - c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": "could not retrieve current user"}) - return - } - - client.AutoRetry = true - - s.spoty.Client = &client - c.JSON(http.StatusOK, gin.H{"success": "welcome, you are now authenticated!"}) } diff --git a/spoty/spoty.go b/spoty/spoty.go index 0293f6b..0bbd92d 100644 --- a/spoty/spoty.go +++ b/spoty/spoty.go @@ -28,10 +28,10 @@ type Image struct { } type Spoty struct { - Client *spotify.Client + client *spotify.Client - Auth spotify.Authenticator - State string + auth spotify.Authenticator + state string cache *cache.Cache } @@ -59,18 +59,18 @@ func New(clientID, clientSecret, host string, port int, cache *cache.Cache) (*Sp } return &Spoty{ - Auth: auth, - State: state.String(), + auth: auth, + state: state.String(), cache: cache, }, nil } func (s *Spoty) IsAuth() bool { - return s.Client != nil + return s.client != nil } func (s *Spoty) IsPlaying() bool { - state, err := s.Client.PlayerState() + state, err := s.client.PlayerState() if err != nil { return false } @@ -78,6 +78,23 @@ func (s *Spoty) IsPlaying() bool { return state.Playing } +func (s *Spoty) AuthURL() string { + return s.auth.AuthURL(s.state) +} + +func (s *Spoty) SetupNewClient(r *http.Request) error { + tok, err := s.auth.Token(s.state, r) + if err != nil { + return err + } + + client := s.auth.NewClient(tok) + client.AutoRetry = true + s.client = &client + + return nil +} + func (s *Spoty) TrackCurrentlyPlaying() (*spotify.FullTrack, error) { const cacheCurrentTrackKey = "current_track" @@ -90,7 +107,7 @@ func (s *Spoty) TrackCurrentlyPlaying() (*spotify.FullTrack, error) { return nil, errors.New("no track currently playing") } - playing, err := s.Client.PlayerCurrentlyPlaying() + playing, err := s.client.PlayerCurrentlyPlaying() if err != nil { return nil, err }