Skip to content

Commit c3fe7bc

Browse files
committed
fallback to regular login if oauth login fails to start
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
1 parent 5eb3275 commit c3fe7bc

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

cli/command/registry/login.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,16 @@ func loginWithStoredCredentials(ctx context.Context, dockerCli command.Cli, auth
144144
func loginUser(ctx context.Context, dockerCli command.Cli, opts loginOptions, defaultUsername, serverAddress string) (*registrytypes.AuthenticateOKBody, error) {
145145
// If we're logging into the index server and the user didn't provide a username or password, use the device flow
146146
if serverAddress == registry.IndexServer && opts.user == "" && opts.password == "" {
147-
return loginWithDeviceCodeFlow(ctx, dockerCli)
148-
} else {
149-
return loginWithUsernameAndPassword(ctx, dockerCli, opts, defaultUsername, serverAddress)
147+
response, err := loginWithDeviceCodeFlow(ctx, dockerCli)
148+
// if the error represents a failure to initiate the device-code flow,
149+
// then we fallback to regular cli credentials login
150+
if !errors.Is(err, manager.ErrDeviceLoginStartFail) {
151+
return response, err
152+
}
153+
fmt.Fprint(dockerCli.Err(), "Failed to start web-based login - falling back to command line login...\n\n")
150154
}
155+
156+
return loginWithUsernameAndPassword(ctx, dockerCli, opts, defaultUsername, serverAddress)
151157
}
152158

153159
func loginWithUsernameAndPassword(ctx context.Context, dockerCli command.Cli, opts loginOptions, defaultUsername, serverAddress string) (*registrytypes.AuthenticateOKBody, error) {

cli/internal/oauth/manager/manager.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/docker/cli/cli/internal/oauth/api"
1616
"github.com/docker/docker/registry"
1717
"github.com/morikuni/aec"
18+
"github.com/sirupsen/logrus"
1819

1920
"github.com/pkg/browser"
2021
)
@@ -70,6 +71,8 @@ func New(options OAuthManagerOptions) *OAuthManager {
7071
}
7172
}
7273

74+
var ErrDeviceLoginStartFail = errors.New("failed to start device code flow login")
75+
7376
// LoginDevice launches the device authentication flow with the tenant,
7477
// printing instructions to the provided writer and attempting to open the
7578
// browser for the user to authenticate.
@@ -80,11 +83,13 @@ func New(options OAuthManagerOptions) *OAuthManager {
8083
func (m *OAuthManager) LoginDevice(ctx context.Context, w io.Writer) (*types.AuthConfig, error) {
8184
state, err := m.api.GetDeviceCode(ctx, m.audience)
8285
if err != nil {
83-
return nil, fmt.Errorf("failed to get device code: %w", err)
86+
logrus.Debugf("failed to start device code login: %v", err)
87+
return nil, ErrDeviceLoginStartFail
8488
}
8589

8690
if state.UserCode == "" {
87-
return nil, errors.New("no user code returned")
91+
logrus.Debugf("failed to start device code login: missing user code")
92+
return nil, ErrDeviceLoginStartFail
8893
}
8994

9095
_, _ = fmt.Fprintln(w, aec.Bold.Apply("\nUSING WEB BASED LOGIN"))

0 commit comments

Comments
 (0)