From 9cc72ff1a9632736adc7dd12f096e3233896fced Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Fri, 14 Jun 2013 09:53:48 +0000 Subject: [PATCH 1/3] fix auth in case you change your password on index.io --- commands.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/commands.go b/commands.go index 63c787c40a..918a781198 100644 --- a/commands.go +++ b/commands.go @@ -296,20 +296,16 @@ func (cli *DockerCli) CmdLogin(args ...string) error { if username == "" { username = cli.authConfig.Username } - if username != cli.authConfig.Username { - fmt.Print("Password: ") - password = readString(os.Stdin, os.Stdout) + fmt.Print("Password: ") + password = readString(os.Stdin, os.Stdout) - if password == "" { - return fmt.Errorf("Error : Password Required") - } + if password == "" { + return fmt.Errorf("Error : Password Required") + } - fmt.Print("Email (", cli.authConfig.Email, "): ") - email = readAndEchoString(os.Stdin, os.Stdout) - if email == "" { - email = cli.authConfig.Email - } - } else { + fmt.Print("Email (", cli.authConfig.Email, "): ") + email = readAndEchoString(os.Stdin, os.Stdout) + if email == "" { email = cli.authConfig.Email } term.RestoreTerminal(oldState) From 90f6bdd6e41ec352009ffbd073c1f45983abb74b Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Fri, 14 Jun 2013 13:38:51 +0000 Subject: [PATCH 2/3] update docs, remove config file on 401 --- api.go | 4 +++ auth/auth.go | 5 ++-- commands.go | 32 ++++++++++++++------- docs/sources/api/docker_remote_api_v1.2.rst | 7 +++++ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/api.go b/api.go index 5e1a6d7011..a69f6a3c2d 100644 --- a/api.go +++ b/api.go @@ -49,6 +49,10 @@ func httpError(w http.ResponseWriter, err error) { http.Error(w, err.Error(), http.StatusConflict) } else if strings.HasPrefix(err.Error(), "Impossible") { http.Error(w, err.Error(), http.StatusNotAcceptable) + } else if strings.HasPrefix(err.Error(), "Wrong login/password") { + http.Error(w, err.Error(), http.StatusUnauthorized) + } else if strings.Contains(err.Error(), "hasn't been activated") { + http.Error(w, err.Error(), http.StatusForbidden) } else { http.Error(w, err.Error(), http.StatusInternalServerError) } diff --git a/auth/auth.go b/auth/auth.go index b7ad73b9ed..5f521ba3db 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -146,7 +146,7 @@ func Login(authConfig *AuthConfig, store bool) (string, error) { if reqStatusCode == 201 { status = "Account created. Please use the confirmation link we sent" + - " to your e-mail to activate it.\n" + " to your e-mail to activate it." storeConfig = true } else if reqStatusCode == 403 { return "", fmt.Errorf("Login: Your account hasn't been activated. " + @@ -165,10 +165,11 @@ func Login(authConfig *AuthConfig, store bool) (string, error) { return "", err } if resp.StatusCode == 200 { - status = "Login Succeeded\n" + status = "Login Succeeded" storeConfig = true } else if resp.StatusCode == 401 { if store { + authConfig.Email = "" if err := SaveConfig(authConfig); err != nil { return "", err } diff --git a/commands.go b/commands.go index 918a781198..d09eb962ce 100644 --- a/commands.go +++ b/commands.go @@ -296,16 +296,21 @@ func (cli *DockerCli) CmdLogin(args ...string) error { if username == "" { username = cli.authConfig.Username } - fmt.Print("Password: ") - password = readString(os.Stdin, os.Stdout) + if username != cli.authConfig.Username { + fmt.Print("Password: ") + password = readString(os.Stdin, os.Stdout) - if password == "" { - return fmt.Errorf("Error : Password Required") - } + if password == "" { + return fmt.Errorf("Error : Password Required") + } - fmt.Print("Email (", cli.authConfig.Email, "): ") - email = readAndEchoString(os.Stdin, os.Stdout) - if email == "" { + fmt.Print("Email (", cli.authConfig.Email, "): ") + email = readAndEchoString(os.Stdin, os.Stdout) + if email == "" { + email = cli.authConfig.Email + } + } else { + password = cli.authConfig.Password email = cli.authConfig.Email } term.RestoreTerminal(oldState) @@ -314,7 +319,14 @@ func (cli *DockerCli) CmdLogin(args ...string) error { cli.authConfig.Password = password cli.authConfig.Email = email - body, _, err := cli.call("POST", "/auth", cli.authConfig) + body, statusCode, err := cli.call("POST", "/auth", cli.authConfig) + if statusCode == 401 { + cli.authConfig.Username = "" + cli.authConfig.Password = "" + cli.authConfig.Email = "" + auth.SaveConfig(cli.authConfig) + return err + } if err != nil { return err } @@ -327,7 +339,7 @@ func (cli *DockerCli) CmdLogin(args ...string) error { } auth.SaveConfig(cli.authConfig) if out2.Status != "" { - fmt.Print(out2.Status) + fmt.Println(out2.Status) } return nil } diff --git a/docs/sources/api/docker_remote_api_v1.2.rst b/docs/sources/api/docker_remote_api_v1.2.rst index 8354760e2f..3231692102 100644 --- a/docs/sources/api/docker_remote_api_v1.2.rst +++ b/docs/sources/api/docker_remote_api_v1.2.rst @@ -877,9 +877,16 @@ Check auth configuration .. sourcecode:: http HTTP/1.1 200 OK + Content-Type: application/json + + { + "Status": "Login Succeeded" + } :statuscode 200: no error :statuscode 204: no error + :statuscode 401: unauthorized + :statuscode 403: forbidden :statuscode 500: server error From 639833aaf53d741176e0b4f817cbbf5a5da24037 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Fri, 21 Jun 2013 09:20:57 +0000 Subject: [PATCH 3/3] fix tests --- auth/auth_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/auth/auth_test.go b/auth/auth_test.go index ead69e8913..8e7adaef8d 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -34,7 +34,7 @@ func TestLogin(t *testing.T) { if err != nil { t.Fatal(err) } - if status != "Login Succeeded\n" { + if status != "Login Succeeded" { t.Fatalf("Expected status \"Login Succeeded\", found \"%s\" instead", status) } } @@ -55,7 +55,7 @@ func TestCreateAccount(t *testing.T) { t.Fatal(err) } expectedStatus := "Account created. Please use the confirmation link we sent" + - " to your e-mail to activate it.\n" + " to your e-mail to activate it." if status != expectedStatus { t.Fatalf("Expected status: \"%s\", found \"%s\" instead.", expectedStatus, status) }