From fdb6acf0b68cbda8af4b1c586736f2197f502807 Mon Sep 17 00:00:00 2001 From: yanosea Date: Tue, 25 Feb 2025 17:46:55 +0900 Subject: [PATCH] wip --- app/application/spotlike/auth_usecase.go | 14 +++++--- .../spotlike/get_auth_url_usecase.go | 27 ++++++++------- .../spotlike/get_authenticator_usecase.go | 34 +++++++++++++++++++ .../cli/spotlike/command/spotlike/auth.go | 25 ++++++++++---- 4 files changed, 76 insertions(+), 24 deletions(-) create mode 100644 app/application/spotlike/get_authenticator_usecase.go diff --git a/app/application/spotlike/auth_usecase.go b/app/application/spotlike/auth_usecase.go index a3e502d..36e8be7 100644 --- a/app/application/spotlike/auth_usecase.go +++ b/app/application/spotlike/auth_usecase.go @@ -1,13 +1,19 @@ package spotlike -import () +import ( + spotifyauth "github.com/zmb3/spotify/v2/auth" +) // authUseCase is a struct that contains the use case of authenticating the Spotify client. -type authUseCase struct{} +type authUseCase struct { + Authenticator *spotifyauth.Authenticator +} // NewAuthUseCase returns a new instance of the AuthUseCase struct. -func NewAuthUseCase() *authUseCase { - return &authUseCase{} +func NewAuthUseCase(authenticator *spotifyauth.Authenticator) *authUseCase { + return &authUseCase{ + Authenticator: authenticator, + } } // Run returns the output of the AuthUseCase. diff --git a/app/application/spotlike/get_auth_url_usecase.go b/app/application/spotlike/get_auth_url_usecase.go index 1ed74d9..67c661b 100644 --- a/app/application/spotlike/get_auth_url_usecase.go +++ b/app/application/spotlike/get_auth_url_usecase.go @@ -1,22 +1,23 @@ package spotlike -import () +import ( + "github.com/thanhpk/randstr" + spotifyauth "github.com/zmb3/spotify/v2/auth" +) // getAuthUrlUseCase is a struct that contains the use case of getting the authentication URL. -type getAuthUrlUseCase struct{} - -// NewGetAuthUrlUseCase returns a new instance of the GetAuthUrlUseCase struct. -func NewGetAuthUrlUseCase() *getAuthUrlUseCase { - return &getAuthUrlUseCase{} +type getAuthUrlUseCase struct { + Authenticator *spotifyauth.Authenticator } -type GetAuthUrlUseCaseOutputDto struct { - AuthUrl string +// NewGetAuthUrlUseCase returns a new instance of the GetAuthUrlUseCase struct. +func NewGetAuthUrlUseCase(authenticator *spotifyauth.Authenticator) *getAuthUrlUseCase { + return &getAuthUrlUseCase{ + Authenticator: authenticator, + } } -// Run returns the output of the GetAuthUrlUseCase. -func (uc *getAuthUrlUseCase) Run() *GetAuthUrlUseCaseOutputDto { - return &GetAuthUrlUseCaseOutputDto{ - AuthUrl: "https://accounts.spotify.com/authorize?client_id", - } +// Run returns the authentication URL. +func (uc *getAuthUrlUseCase) Run() string { + return uc.Authenticator.AuthURL(randstr.Hex(11)) } diff --git a/app/application/spotlike/get_authenticator_usecase.go b/app/application/spotlike/get_authenticator_usecase.go new file mode 100644 index 0000000..dc86b53 --- /dev/null +++ b/app/application/spotlike/get_authenticator_usecase.go @@ -0,0 +1,34 @@ +package spotlike + +import ( + spotifyauth "github.com/zmb3/spotify/v2/auth" + + "github.com/yanosea/spotlike/app/config" +) + +// getAuthenticatorUseCase is a struct that contains the use case of getting the authenticator. +type getAuthenticatorUseCase struct { + SpotlikeConfig *config.SpotlikeConfig +} + +// NewGetAuthenticatorUseCase returns a new instance of the GetAuthenticatorUseCase struct. +func NewGetAuthenticatorUseCase(conf *config.SpotlikeConfig) *getAuthenticatorUseCase { + return &getAuthenticatorUseCase{ + SpotlikeConfig: conf, + } +} + +// Run returns the authenticator. +func (uc *getAuthenticatorUseCase) Run() *spotifyauth.Authenticator { + return spotifyauth.New( + spotifyauth.WithScopes( + spotifyauth.ScopeUserFollowRead, + spotifyauth.ScopeUserFollowModify, + spotifyauth.ScopeUserLibraryRead, + spotifyauth.ScopeUserLibraryModify, + ), + spotifyauth.WithClientID(uc.SpotlikeConfig.SpotifyID), + spotifyauth.WithClientSecret(uc.SpotlikeConfig.SpotifySecret), + spotifyauth.WithRedirectURL(uc.SpotlikeConfig.SpotifyRedirectUri), + ) +} diff --git a/app/presentation/cli/spotlike/command/spotlike/auth.go b/app/presentation/cli/spotlike/command/spotlike/auth.go index a8ec5ae..26377b4 100644 --- a/app/presentation/cli/spotlike/command/spotlike/auth.go +++ b/app/presentation/cli/spotlike/command/spotlike/auth.go @@ -5,6 +5,7 @@ import ( "os" spotlikeApp "github.com/yanosea/spotlike/app/application/spotlike" + baseConfig "github.com/yanosea/spotlike/app/config" "github.com/yanosea/spotlike/app/presentation/cli/spotlike/config" "github.com/yanosea/spotlike/app/presentation/cli/spotlike/formatter" "github.com/yanosea/spotlike/app/presentation/cli/spotlike/presenter" @@ -143,20 +144,30 @@ func runAuth(conf *config.SpotlikeCliConfig, output *string) error { } } + bc := &baseConfig.SpotlikeConfig{ + SpotifyID: conf.SpotifyID, + SpotifySecret: conf.SpotifySecret, + SpotifyRedirectUri: conf.SpotifyRedirectUri, + SpotifyRefreshToken: conf.SpotifyRefreshToken, + } + + gauc := spotlikeApp.NewGetAuthenticatorUseCase(bc) + authenticator := gauc.Run() + if conf.SpotifyRefreshToken == "" { - guc := spotlikeApp.NewGetAuthUrlUseCase(conf) - presenter.Print(os.Stdout, guc.Run()) + guuc := spotlikeApp.NewGetAuthUrlUseCase(authenticator) + presenter.Print(os.Stdout, guuc.Run()) - auc := spotlikeApp.NewAuthUseCase(conf) - if err := auc.Run(conf); err != nil { + auc := spotlikeApp.NewAuthUseCase(authenticator) + if err := auc.Run(); err != nil { o := formatter.Red("❌ Failed to authenticate... Please try again...") *output = o return err } } else { - uc := spotlikeApp.NewRefreshUseCase(conf) - if err := uc.Refresh(); err != nil { - if err := uc.Run(conf); err != nil { + ruc := spotlikeApp.NewRefreshUseCase(conf) + if err := ruc.Refresh(); err != nil { + if err := ruc.Run(); err != nil { o := formatter.Red("❌ Failed to refresh... Please try again...") *output = o return err