Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix re-login when token expired #139

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,22 @@ func NewRootCmd(streams command.Streams, hubClient *hub.Client, store credential
return err
}

if cmd.Annotations["sudo"] == "true" && ac.Username != "" {
return requireTwoFactorCode(cmd.Context(), streams, hubClient, store)
}

if ac.Username == "" {
log.Fatal(ansi.Error(`You need to be logged in to Docker Hub to use this tool.
Please login to Docker Hub using the "hub-tool login" command.`))
}

if !ac.TokenExpired() {
if cmd.Annotations["sudo"] == "true" {
if err := tryLogin(cmd.Context(), streams, hubClient, ac, store); err != nil {
return err
}
return nil
}

token, refreshToken, err := hubClient.Login(ac.Username, ac.Password, func() (string, error) {
return "", nil
})
if err != nil {
return err
if ac.TokenExpired() {
return tryLogin(cmd.Context(), streams, hubClient, ac, store)
}

return store.Store(credentials.Auth{
Username: ac.Username,
Password: ac.Password,
Token: token,
RefreshToken: refreshToken,
})
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if flags.showVersion {
Expand Down Expand Up @@ -149,16 +139,8 @@ func newVersionCmd(streams command.Streams) *cobra.Command {
}
}

func requireTwoFactorCode(ctx context.Context, streams command.Streams, hubClient *hub.Client, store credentials.Store) error {
ac, err := store.GetAuth()
if err != nil {
return err
}
if !ac.TokenExpired() {
return nil
}

token, refreshToken, err := login.VerifyTwoFactorCode(ctx, streams, hubClient, ac.Username, ac.Password)
func tryLogin(ctx context.Context, streams command.Streams, hubClient *hub.Client, ac *credentials.Auth, store credentials.Store) error {
token, refreshToken, err := login.Login(ctx, streams, hubClient, ac.Username, ac.Password)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Clien
return err
}

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

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