Use config from OAuth2Authenticator for auth command

This commit is contained in:
Philipp Wolfer 2023-11-22 08:41:18 +01:00
parent cf3747bde2
commit d0739aad0f
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
5 changed files with 31 additions and 18 deletions

View file

@ -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
}

View file

@ -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"`

View file

@ -18,6 +18,7 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
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

View file

@ -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)

View file

@ -17,6 +17,7 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
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
}