From 804a245fdeb2f5fe33683ec6f63eee9045216f27 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sun, 26 Jul 2020 18:49:22 +0900 Subject: [PATCH] Refactor: rename to AuthCodeBrowser (#342) --- pkg/adaptors/cmd/authentication.go | 2 +- pkg/adaptors/cmd/cmd_test.go | 12 ++++++------ pkg/di/wire_gen.go | 6 ++++-- pkg/usecases/authentication/authcode_browser.go | 8 ++++---- pkg/usecases/authentication/authcode_browser_test.go | 8 ++++---- pkg/usecases/authentication/authentication.go | 12 ++++++------ pkg/usecases/authentication/authentication_test.go | 6 +++--- pkg/usecases/setup/stage2.go | 4 ++-- 8 files changed, 30 insertions(+), 28 deletions(-) diff --git a/pkg/adaptors/cmd/authentication.go b/pkg/adaptors/cmd/authentication.go index c9c800d..ee7a3bf 100644 --- a/pkg/adaptors/cmd/authentication.go +++ b/pkg/adaptors/cmd/authentication.go @@ -60,7 +60,7 @@ func (o *authenticationOptions) addFlags(f *pflag.FlagSet) { func (o *authenticationOptions) grantOptionSet() (s authentication.GrantOptionSet, err error) { switch { case o.GrantType == "authcode" || (o.GrantType == "auto" && o.Username == ""): - s.AuthCodeOption = &authentication.AuthCodeOption{ + s.AuthCodeBrowserOption = &authentication.AuthCodeBrowserOption{ BindAddress: o.determineListenAddress(), SkipOpenBrowser: o.SkipOpenBrowser, RedirectURLHostname: o.RedirectURLHostname, diff --git a/pkg/adaptors/cmd/cmd_test.go b/pkg/adaptors/cmd/cmd_test.go index 912fa62..246e853 100644 --- a/pkg/adaptors/cmd/cmd_test.go +++ b/pkg/adaptors/cmd/cmd_test.go @@ -26,7 +26,7 @@ func TestCmd_Run(t *testing.T) { args: []string{executable}, in: standalone.Input{ GrantOptionSet: authentication.GrantOptionSet{ - AuthCodeOption: &authentication.AuthCodeOption{ + AuthCodeBrowserOption: &authentication.AuthCodeBrowserOption{ BindAddress: defaultListenAddress, RedirectURLHostname: "localhost", }, @@ -41,7 +41,7 @@ func TestCmd_Run(t *testing.T) { }, in: standalone.Input{ GrantOptionSet: authentication.GrantOptionSet{ - AuthCodeOption: &authentication.AuthCodeOption{ + AuthCodeBrowserOption: &authentication.AuthCodeBrowserOption{ BindAddress: []string{"127.0.0.1:10080", "127.0.0.1:20080"}, RedirectURLHostname: "localhost", }, @@ -58,7 +58,7 @@ func TestCmd_Run(t *testing.T) { }, in: standalone.Input{ GrantOptionSet: authentication.GrantOptionSet{ - AuthCodeOption: &authentication.AuthCodeOption{ + AuthCodeBrowserOption: &authentication.AuthCodeBrowserOption{ BindAddress: []string{"127.0.0.1:10080", "127.0.0.1:20080"}, RedirectURLHostname: "localhost", }, @@ -89,7 +89,7 @@ func TestCmd_Run(t *testing.T) { CACertData: "BASE64ENCODED", SkipTLSVerify: true, GrantOptionSet: authentication.GrantOptionSet{ - AuthCodeOption: &authentication.AuthCodeOption{ + AuthCodeBrowserOption: &authentication.AuthCodeBrowserOption{ BindAddress: []string{"127.0.0.1:10080", "127.0.0.1:20080"}, SkipOpenBrowser: true, RedirectURLHostname: "localhost", @@ -196,7 +196,7 @@ func TestCmd_Run(t *testing.T) { IssuerURL: "https://issuer.example.com", ClientID: "YOUR_CLIENT_ID", GrantOptionSet: authentication.GrantOptionSet{ - AuthCodeOption: &authentication.AuthCodeOption{ + AuthCodeBrowserOption: &authentication.AuthCodeBrowserOption{ BindAddress: []string{"127.0.0.1:8000", "127.0.0.1:18000"}, RedirectURLHostname: "localhost", }, @@ -234,7 +234,7 @@ func TestCmd_Run(t *testing.T) { CACertData: "BASE64ENCODED", SkipTLSVerify: true, GrantOptionSet: authentication.GrantOptionSet{ - AuthCodeOption: &authentication.AuthCodeOption{ + AuthCodeBrowserOption: &authentication.AuthCodeBrowserOption{ BindAddress: []string{"127.0.0.1:10080", "127.0.0.1:20080"}, SkipOpenBrowser: true, RedirectURLHostname: "localhost", diff --git a/pkg/di/wire_gen.go b/pkg/di/wire_gen.go index 3fe2b15..19436fa 100644 --- a/pkg/di/wire_gen.go +++ b/pkg/di/wire_gen.go @@ -26,6 +26,7 @@ import ( // Injectors from di.go: +// NewCmd returns an instance of adaptors.Cmd. func NewCmd() cmd.Interface { clockReal := &clock.Real{} stdin := _wireFileValue @@ -41,12 +42,13 @@ var ( _wireOsFileValue = os.Stdout ) +// NewCmdForHeadless returns an instance of adaptors.Cmd for headless testing. func NewCmdForHeadless(clockInterface clock.Interface, stdin stdio.Stdin, stdout stdio.Stdout, loggerInterface logger.Interface, browserInterface browser.Interface) cmd.Interface { factory := &oidcclient.Factory{ Clock: clockInterface, Logger: loggerInterface, } - authCode := &authentication.AuthCode{ + authCodeBrowser := &authentication.AuthCodeBrowser{ Browser: browserInterface, Logger: loggerInterface, } @@ -65,7 +67,7 @@ func NewCmdForHeadless(clockInterface clock.Interface, stdin stdio.Stdin, stdout OIDCClient: factory, Logger: loggerInterface, Clock: clockInterface, - AuthCode: authCode, + AuthCodeBrowser: authCodeBrowser, AuthCodeKeyboard: authCodeKeyboard, ROPC: ropc, } diff --git a/pkg/usecases/authentication/authcode_browser.go b/pkg/usecases/authentication/authcode_browser.go index e1a5495..865329d 100644 --- a/pkg/usecases/authentication/authcode_browser.go +++ b/pkg/usecases/authentication/authcode_browser.go @@ -13,14 +13,14 @@ import ( "golang.org/x/xerrors" ) -// AuthCode provides the authentication code flow. -type AuthCode struct { +// AuthCodeBrowser provides the authentication code flow using the browser. +type AuthCodeBrowser struct { Browser browser.Interface Logger logger.Interface } -func (u *AuthCode) Do(ctx context.Context, o *AuthCodeOption, client oidcclient.Interface) (*Output, error) { - u.Logger.V(1).Infof("starting the authentication code flow via the browser") +func (u *AuthCodeBrowser) Do(ctx context.Context, o *AuthCodeBrowserOption, client oidcclient.Interface) (*Output, error) { + u.Logger.V(1).Infof("starting the authentication code flow using the browser") state, err := oidc.NewState() if err != nil { return nil, xerrors.Errorf("could not generate a state: %w", err) diff --git a/pkg/usecases/authentication/authcode_browser_test.go b/pkg/usecases/authentication/authcode_browser_test.go index 1e8f0ea..ab81524 100644 --- a/pkg/usecases/authentication/authcode_browser_test.go +++ b/pkg/usecases/authentication/authcode_browser_test.go @@ -27,7 +27,7 @@ func TestAuthCode_Do(t *testing.T) { defer ctrl.Finish() ctx, cancel := context.WithTimeout(context.TODO(), timeout) defer cancel() - o := &AuthCodeOption{ + o := &AuthCodeBrowserOption{ BindAddress: []string{"127.0.0.1:8000"}, SkipOpenBrowser: true, RedirectURLHostname: "localhost", @@ -54,7 +54,7 @@ func TestAuthCode_Do(t *testing.T) { RefreshToken: "YOUR_REFRESH_TOKEN", IDTokenClaims: dummyTokenClaims, }, nil) - u := AuthCode{ + u := AuthCodeBrowser{ Logger: logger.New(t), } got, err := u.Do(ctx, o, mockOIDCClient) @@ -76,7 +76,7 @@ func TestAuthCode_Do(t *testing.T) { defer ctrl.Finish() ctx, cancel := context.WithTimeout(context.TODO(), timeout) defer cancel() - o := &AuthCodeOption{ + o := &AuthCodeBrowserOption{ BindAddress: []string{"127.0.0.1:8000"}, } mockOIDCClient := mock_oidcclient.NewMockInterface(ctrl) @@ -94,7 +94,7 @@ func TestAuthCode_Do(t *testing.T) { mockBrowser := mock_browser.NewMockInterface(ctrl) mockBrowser.EXPECT(). Open("LOCAL_SERVER_URL") - u := AuthCode{ + u := AuthCodeBrowser{ Logger: logger.New(t), Browser: mockBrowser, } diff --git a/pkg/usecases/authentication/authentication.go b/pkg/usecases/authentication/authentication.go index 6c0cc29..b3adc89 100644 --- a/pkg/usecases/authentication/authentication.go +++ b/pkg/usecases/authentication/authentication.go @@ -18,7 +18,7 @@ import ( var Set = wire.NewSet( wire.Struct(new(Authentication), "*"), wire.Bind(new(Interface), new(*Authentication)), - wire.Struct(new(AuthCode), "*"), + wire.Struct(new(AuthCodeBrowser), "*"), wire.Struct(new(AuthCodeKeyboard), "*"), wire.Struct(new(ROPC), "*"), ) @@ -41,12 +41,12 @@ type Input struct { } type GrantOptionSet struct { - AuthCodeOption *AuthCodeOption + AuthCodeBrowserOption *AuthCodeBrowserOption AuthCodeKeyboardOption *AuthCodeKeyboardOption ROPCOption *ROPCOption } -type AuthCodeOption struct { +type AuthCodeBrowserOption struct { SkipOpenBrowser bool BindAddress []string RedirectURLHostname string @@ -90,7 +90,7 @@ type Authentication struct { OIDCClient oidcclient.FactoryInterface Logger logger.Interface Clock clock.Interface - AuthCode *AuthCode + AuthCodeBrowser *AuthCodeBrowser AuthCodeKeyboard *AuthCodeKeyboard ROPC *ROPC } @@ -143,8 +143,8 @@ func (u *Authentication) Do(ctx context.Context, in Input) (*Output, error) { u.Logger.V(1).Infof("could not refresh the token: %s", err) } - if in.GrantOptionSet.AuthCodeOption != nil { - return u.AuthCode.Do(ctx, in.GrantOptionSet.AuthCodeOption, client) + if in.GrantOptionSet.AuthCodeBrowserOption != nil { + return u.AuthCodeBrowser.Do(ctx, in.GrantOptionSet.AuthCodeBrowserOption, client) } if in.GrantOptionSet.AuthCodeKeyboardOption != nil { return u.AuthCodeKeyboard.Do(ctx, in.GrantOptionSet.AuthCodeKeyboardOption, client) diff --git a/pkg/usecases/authentication/authentication_test.go b/pkg/usecases/authentication/authentication_test.go index bf8a54f..0e59ae4 100644 --- a/pkg/usecases/authentication/authentication_test.go +++ b/pkg/usecases/authentication/authentication_test.go @@ -114,14 +114,14 @@ func TestAuthentication_Do(t *testing.T) { } }) - t.Run("HasExpiredRefreshToken/AuthCode", func(t *testing.T) { + t.Run("HasExpiredRefreshToken/AuthCodeBrowser", func(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() ctx, cancel := context.WithTimeout(context.TODO(), timeout) defer cancel() in := Input{ GrantOptionSet: GrantOptionSet{ - AuthCodeOption: &AuthCodeOption{ + AuthCodeBrowserOption: &AuthCodeBrowserOption{ BindAddress: []string{"127.0.0.1:8000"}, SkipOpenBrowser: true, }, @@ -159,7 +159,7 @@ func TestAuthentication_Do(t *testing.T) { }, Logger: testingLogger.New(t), Clock: clock.Fake(expiryTime.Add(+time.Hour)), - AuthCode: &AuthCode{ + AuthCodeBrowser: &AuthCodeBrowser{ Logger: testingLogger.New(t), }, } diff --git a/pkg/usecases/setup/stage2.go b/pkg/usecases/setup/stage2.go index 3a3e8a1..8eb55d5 100644 --- a/pkg/usecases/setup/stage2.go +++ b/pkg/usecases/setup/stage2.go @@ -137,8 +137,8 @@ func makeCredentialPluginArgs(in Stage2Input) []string { args = append(args, "--insecure-skip-tls-verify") } - if in.GrantOptionSet.AuthCodeOption != nil { - if in.GrantOptionSet.AuthCodeOption.SkipOpenBrowser { + if in.GrantOptionSet.AuthCodeBrowserOption != nil { + if in.GrantOptionSet.AuthCodeBrowserOption.SkipOpenBrowser { args = append(args, "--skip-open-browser") } }