diff --git a/cmd/service_add.go b/cmd/service_add.go index 0bf671d..3360faa 100644 --- a/cmd/service_add.go +++ b/cmd/service_add.go @@ -27,11 +27,11 @@ import ( "github.com/manifoldco/promptui" "github.com/spf13/cobra" + "go.uploadedlobster.com/scotty/internal/auth" "go.uploadedlobster.com/scotty/internal/backends" "go.uploadedlobster.com/scotty/internal/cli" "go.uploadedlobster.com/scotty/internal/config" "go.uploadedlobster.com/scotty/internal/i18n" - "go.uploadedlobster.com/scotty/internal/models" ) var serviceAddCmd = &cobra.Command{ @@ -95,7 +95,7 @@ func init() { } func promptForAuth(service config.ServiceConfig) error { - backend, err := backends.ResolveBackend[models.OAuth2Authenticator](service) + backend, err := backends.ResolveBackend[auth.OAuth2Authenticator](service) if err != nil { // No authentication required, return return nil diff --git a/cmd/service_auth.go b/cmd/service_auth.go index ddab35d..0a075c5 100644 --- a/cmd/service_auth.go +++ b/cmd/service_auth.go @@ -18,9 +18,9 @@ package cmd import ( "github.com/spf13/cobra" + "go.uploadedlobster.com/scotty/internal/auth" "go.uploadedlobster.com/scotty/internal/backends" "go.uploadedlobster.com/scotty/internal/cli" - "go.uploadedlobster.com/scotty/internal/models" ) var serviceAuthCmd = &cobra.Command{ @@ -33,7 +33,7 @@ multiple services using the same backend but different authentication.`, Run: func(cmd *cobra.Command, args []string) { serviceConfig, err := cli.SelectService(cmd) cobra.CheckErr(err) - backend, err := backends.ResolveBackend[models.OAuth2Authenticator](serviceConfig) + backend, err := backends.ResolveBackend[auth.OAuth2Authenticator](serviceConfig) cobra.CheckErr(err) cli.AuthenticationFlow(serviceConfig, backend) }, diff --git a/internal/auth/auth.go b/internal/auth/auth.go new file mode 100644 index 0000000..5ba05af --- /dev/null +++ b/internal/auth/auth.go @@ -0,0 +1,34 @@ +/* +Copyright © 2023 Philipp Wolfer + +Scotty is free software: you can redistribute it and/or modify it under the +terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later version. + +Scotty is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +Scotty. If not, see . +*/ + +package auth + +import ( + "net/url" + + "go.uploadedlobster.com/scotty/internal/models" + "golang.org/x/oauth2" +) + +// Must be implemented by backends requiring OAuth2 authentication +type OAuth2Authenticator interface { + models.Backend + + // Returns OAuth2 config suitable for this backend + OAuth2Strategy(redirectUrl *url.URL) OAuth2Strategy + + // Setup the OAuth2 client + OAuth2Setup(token oauth2.TokenSource) error +} diff --git a/internal/backends/auth.go b/internal/backends/auth.go index d27efd6..c17e9ba 100644 --- a/internal/backends/auth.go +++ b/internal/backends/auth.go @@ -37,7 +37,7 @@ func BuildRedirectURL(config *viper.Viper, backend string) (*url.URL, error) { } func Authenticate(service string, backend models.Backend, db storage.Database, config *viper.Viper) (bool, error) { - authenticator, needAuth := backend.(models.OAuth2Authenticator) + authenticator, needAuth := backend.(auth.OAuth2Authenticator) if needAuth { redirectURL, err := BuildRedirectURL(config, backend.Name()) if err != nil { diff --git a/internal/cli/auth.go b/internal/cli/auth.go index fc5c889..828651a 100644 --- a/internal/cli/auth.go +++ b/internal/cli/auth.go @@ -26,12 +26,11 @@ import ( "go.uploadedlobster.com/scotty/internal/backends" "go.uploadedlobster.com/scotty/internal/config" "go.uploadedlobster.com/scotty/internal/i18n" - "go.uploadedlobster.com/scotty/internal/models" "go.uploadedlobster.com/scotty/internal/storage" "golang.org/x/oauth2" ) -func AuthenticationFlow(service config.ServiceConfig, backend models.OAuth2Authenticator) { +func AuthenticationFlow(service config.ServiceConfig, backend auth.OAuth2Authenticator) { redirectURL, err := backends.BuildRedirectURL(viper.GetViper(), backend.Name()) cobra.CheckErr(err) diff --git a/internal/models/interfaces.go b/internal/models/interfaces.go index cc52ead..cc19d8d 100644 --- a/internal/models/interfaces.go +++ b/internal/models/interfaces.go @@ -17,12 +17,10 @@ Scotty. If not, see . package models import ( - "net/url" "time" - "go.uploadedlobster.com/scotty/internal/auth" + // "go.uploadedlobster.com/scotty/internal/auth" "go.uploadedlobster.com/scotty/internal/config" - "golang.org/x/oauth2" ) // A listen service backend. @@ -85,14 +83,3 @@ type LovesImport interface { // Imports the given list of loves. ImportLoves(export LovesResult, importResult ImportResult, progress chan Progress) (ImportResult, error) } - -// Must be implemented by backends requiring OAuth2 authentication -type OAuth2Authenticator interface { - Backend - - // Returns OAuth2 config suitable for this backend - OAuth2Strategy(redirectUrl *url.URL) auth.OAuth2Strategy - - // Setup the OAuth2 client - OAuth2Setup(token oauth2.TokenSource) error -}