Skip to content

Commit 0edf43a

Browse files
Merge pull request #139 from docker/fix-relog
Fix re-login when token expired
2 parents 94bd9cb + acb01a2 commit 0edf43a

File tree

2 files changed

+12
-30
lines changed

2 files changed

+12
-30
lines changed

Diff for: internal/commands/root.go

+9-27
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,22 @@ func NewRootCmd(streams command.Streams, hubClient *hub.Client, store credential
7575
return err
7676
}
7777

78-
if cmd.Annotations["sudo"] == "true" && ac.Username != "" {
79-
return requireTwoFactorCode(cmd.Context(), streams, hubClient, store)
80-
}
81-
8278
if ac.Username == "" {
8379
log.Fatal(ansi.Error(`You need to be logged in to Docker Hub to use this tool.
8480
Please login to Docker Hub using the "hub-tool login" command.`))
8581
}
8682

87-
if !ac.TokenExpired() {
83+
if cmd.Annotations["sudo"] == "true" {
84+
if err := tryLogin(cmd.Context(), streams, hubClient, ac, store); err != nil {
85+
return err
86+
}
8887
return nil
8988
}
9089

91-
token, refreshToken, err := hubClient.Login(ac.Username, ac.Password, func() (string, error) {
92-
return "", nil
93-
})
94-
if err != nil {
95-
return err
90+
if ac.TokenExpired() {
91+
return tryLogin(cmd.Context(), streams, hubClient, ac, store)
9692
}
97-
98-
return store.Store(credentials.Auth{
99-
Username: ac.Username,
100-
Password: ac.Password,
101-
Token: token,
102-
RefreshToken: refreshToken,
103-
})
93+
return nil
10494
},
10595
RunE: func(cmd *cobra.Command, args []string) error {
10696
if flags.showVersion {
@@ -149,16 +139,8 @@ func newVersionCmd(streams command.Streams) *cobra.Command {
149139
}
150140
}
151141

152-
func requireTwoFactorCode(ctx context.Context, streams command.Streams, hubClient *hub.Client, store credentials.Store) error {
153-
ac, err := store.GetAuth()
154-
if err != nil {
155-
return err
156-
}
157-
if !ac.TokenExpired() {
158-
return nil
159-
}
160-
161-
token, refreshToken, err := login.VerifyTwoFactorCode(ctx, streams, hubClient, ac.Username, ac.Password)
142+
func tryLogin(ctx context.Context, streams command.Streams, hubClient *hub.Client, ac *credentials.Auth, store credentials.Store) error {
143+
token, refreshToken, err := login.Login(ctx, streams, hubClient, ac.Username, ac.Password)
162144
if err != nil {
163145
return err
164146
}

Diff for: internal/login/login.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Clien
4949
return err
5050
}
5151

52-
token, refreshToken, err := VerifyTwoFactorCode(ctx, streams, hubClient, username, password)
52+
token, refreshToken, err := Login(ctx, streams, hubClient, username, password)
5353
if err != nil {
5454
return err
5555
}
@@ -66,8 +66,8 @@ func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Clien
6666
})
6767
}
6868

69-
// VerifyTwoFactorCode run 2FA login
70-
func VerifyTwoFactorCode(ctx context.Context, streams command.Streams, hubClient *hub.Client, username string, password string) (string, string, error) {
69+
// Login runs login and optionnaly the 2FA
70+
func Login(ctx context.Context, streams command.Streams, hubClient *hub.Client, username string, password string) (string, string, error) {
7171
return hubClient.Login(username, password, func() (string, error) {
7272
return readClearText(ctx, streams, "2FA required, please provide the 6 digit code: ")
7373
})

0 commit comments

Comments
 (0)