Refactor: rename to AuthCodeBrowser (#342)

This commit is contained in:
Hidetake Iwata
2020-07-26 18:49:22 +09:00
committed by GitHub
parent ffaa26cba8
commit 804a245fde
8 changed files with 30 additions and 28 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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