зеркало из https://github.com/docker/hub-tool.git
Fix re-login when token expired
* if it's a sudo re-login the user * if not and if the toekn expired, re-login Signed-off-by: Djordje Lukic <djordje.lukic@docker.com>
This commit is contained in:
Родитель
06a23b6d7b
Коммит
acb01a290e
|
@ -75,32 +75,22 @@ func NewRootCmd(streams command.Streams, hubClient *hub.Client, store credential
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Annotations["sudo"] == "true" && ac.Username != "" {
|
|
||||||
return requireTwoFactorCode(cmd.Context(), streams, hubClient, store)
|
|
||||||
}
|
|
||||||
|
|
||||||
if ac.Username == "" {
|
if ac.Username == "" {
|
||||||
log.Fatal(ansi.Error(`You need to be logged in to Docker Hub to use this tool.
|
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.`))
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
token, refreshToken, err := hubClient.Login(ac.Username, ac.Password, func() (string, error) {
|
if ac.TokenExpired() {
|
||||||
return "", nil
|
return tryLogin(cmd.Context(), streams, hubClient, ac, store)
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
return store.Store(credentials.Auth{
|
|
||||||
Username: ac.Username,
|
|
||||||
Password: ac.Password,
|
|
||||||
Token: token,
|
|
||||||
RefreshToken: refreshToken,
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if flags.showVersion {
|
if flags.showVersion {
|
||||||
|
@ -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 {
|
func tryLogin(ctx context.Context, streams command.Streams, hubClient *hub.Client, ac *credentials.Auth, store credentials.Store) error {
|
||||||
ac, err := store.GetAuth()
|
token, refreshToken, err := login.Login(ctx, streams, hubClient, ac.Username, ac.Password)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !ac.TokenExpired() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
token, refreshToken, err := login.VerifyTwoFactorCode(ctx, streams, hubClient, ac.Username, ac.Password)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Clien
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
token, refreshToken, err := VerifyTwoFactorCode(ctx, streams, hubClient, username, password)
|
token, refreshToken, err := Login(ctx, streams, hubClient, username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -66,8 +66,8 @@ func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Clien
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerifyTwoFactorCode run 2FA login
|
// Login runs login and optionnaly the 2FA
|
||||||
func VerifyTwoFactorCode(ctx context.Context, streams command.Streams, hubClient *hub.Client, username string, password string) (string, string, error) {
|
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 hubClient.Login(username, password, func() (string, error) {
|
||||||
return readClearText(ctx, streams, "2FA required, please provide the 6 digit code: ")
|
return readClearText(ctx, streams, "2FA required, please provide the 6 digit code: ")
|
||||||
})
|
})
|
||||||
|
|
Загрузка…
Ссылка в новой задаче