moved OAuth2Authenticator to auth package

This commit is contained in:
Philipp Wolfer 2023-12-10 14:17:49 +01:00
parent dd501df5c5
commit a59a542967
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
6 changed files with 41 additions and 21 deletions

View file

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

View file

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

34
internal/auth/auth.go Normal file
View file

@ -0,0 +1,34 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
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 <https://www.gnu.org/licenses/>.
*/
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
}

View file

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

View file

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

View file

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