diff --git a/backends/auth.go b/backends/auth.go index 01a93d7..386e935 100644 --- a/backends/auth.go +++ b/backends/auth.go @@ -43,7 +43,7 @@ func Authenticate(backend models.Backend, token *oauth2.Token, config *viper.Vip if err != nil { return auth, err } - authenticator.OAuth2Setup(redirectURL.String(), token) + authenticator.OAuth2Setup(redirectURL, token) } return auth, nil } diff --git a/backends/spotify/models.go b/backends/spotify/models.go index 1c1fa4b..2dbd0ef 100644 --- a/backends/spotify/models.go +++ b/backends/spotify/models.go @@ -22,6 +22,15 @@ THE SOFTWARE. package spotify +type TracksResult struct { + Href string `json:"href"` + Limit int `json:"limit"` + Next string `json:"next"` + Previous string `json:"previous"` + Offset int `json:"offset"` + Total int `json:"total"` +} + type RecentlyPlayedResult struct { Href string `json:"href"` Limit int `json:"limit"` diff --git a/backends/spotify/spotify.go b/backends/spotify/spotify.go index 582648f..5ff8611 100644 --- a/backends/spotify/spotify.go +++ b/backends/spotify/spotify.go @@ -18,6 +18,7 @@ Scotty. If not, see . package spotify import ( + "net/url" "sort" "strconv" "time" @@ -42,17 +43,22 @@ func (b *SpotifyApiBackend) FromConfig(config *viper.Viper) models.Backend { return b } -func (b *SpotifyApiBackend) OAuth2Config(redirectUrl string) oauth2.Config { +func (b *SpotifyApiBackend) OAuth2Config(redirectUrl *url.URL) oauth2.Config { return oauth2.Config{ ClientID: b.clientId, ClientSecret: b.clientSecret, - Scopes: []string{"user-read-recently-played"}, - RedirectURL: redirectUrl, - Endpoint: spotify.Endpoint, + Scopes: []string{ + "user-read-currently-playing", + "user-read-recently-played", + "user-library-read", + "user-library-modify", + }, + RedirectURL: redirectUrl.String(), + Endpoint: spotify.Endpoint, } } -func (b *SpotifyApiBackend) OAuth2Setup(redirectUrl string, token *oauth2.Token) error { +func (b *SpotifyApiBackend) OAuth2Setup(redirectUrl *url.URL, token *oauth2.Token) error { config := b.OAuth2Config(redirectUrl) b.client = NewClient(config, token) return nil diff --git a/cmd/auth.go b/cmd/auth.go index 6c1c1c4..0bff457 100644 --- a/cmd/auth.go +++ b/cmd/auth.go @@ -25,9 +25,9 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" "go.uploadedlobster.com/scotty/backends" + "go.uploadedlobster.com/scotty/models" "go.uploadedlobster.com/scotty/storage" "golang.org/x/oauth2" - "golang.org/x/oauth2/spotify" ) // authCmd represents the auth command @@ -37,19 +37,14 @@ var authCmd = &cobra.Command{ Long: `For backends requiring authentication this command can be used to authenticate.`, Run: func(cmd *cobra.Command, args []string) { serviceName, serviceConfig := getConfigFromFlag(cmd, "service") - backend := serviceConfig.GetString("backend") + backend, err := backends.ResolveBackend[models.OAuth2Authenticator](serviceConfig) + cobra.CheckErr(err) - redirectURL, err := backends.BuildRedirectURL(viper.GetViper(), backend) + redirectURL, err := backends.BuildRedirectURL(viper.GetViper(), backend.Name()) cobra.CheckErr(err) ctx := context.Background() - conf := &oauth2.Config{ - ClientID: serviceConfig.GetString("client-id"), - ClientSecret: serviceConfig.GetString("client-secret"), - Scopes: []string{"user-read-recently-played"}, - RedirectURL: redirectURL.String(), - Endpoint: spotify.Endpoint, - } + conf := backend.OAuth2Config(redirectURL) responseChan := make(chan string) diff --git a/models/interfaces.go b/models/interfaces.go index f2d7374..b676ba6 100644 --- a/models/interfaces.go +++ b/models/interfaces.go @@ -17,6 +17,7 @@ Scotty. If not, see . package models import ( + "net/url" "time" "github.com/spf13/viper" @@ -83,9 +84,11 @@ type LovesImport interface { // Must be implemented by backends requiring OAuth2 authentication type OAuth2Authenticator interface { + Backend + // Returns OAuth2 config suitable for this backend - OAuth2Config(redirectUrl string) oauth2.Config + OAuth2Config(redirectUrl *url.URL) oauth2.Config // Setup the OAuth2 client - OAuth2Setup(redirectUrl string, token *oauth2.Token) error + OAuth2Setup(redirectUrl *url.URL, token *oauth2.Token) error }