Skip to content

Commit

Permalink
Added a nice error message when hitting the auth failure rate limit t…
Browse files Browse the repository at this point in the history
…o hoverctl
  • Loading branch information
benjih authored and mogronalol committed Apr 27, 2017
1 parent 3e31113 commit 7ed578b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
18 changes: 16 additions & 2 deletions functional-tests/hoverctl/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,21 @@ var _ = Describe("hoverctl login", func() {
It("should not log you with incorrect credentials", func() {
output := functional_tests.Run(hoverctlBinary, "login", "--username", "incorrect", "--password", "incorrect")

Expect(output).To(ContainSubstring("Failed to login to Hoverfly"))
Expect(output).To(ContainSubstring("Incorrect username or password"))
})

It("should error after too many failed attempts", func() {
output := functional_tests.Run(hoverctlBinary, "login", "--username", "incorrect", "--password", "incorrect")
Expect(output).To(ContainSubstring("Incorrect username or password"))

output = functional_tests.Run(hoverctlBinary, "login", "--username", "incorrect", "--password", "incorrect")
Expect(output).To(ContainSubstring("Incorrect username or password"))

output = functional_tests.Run(hoverctlBinary, "login", "--username", "incorrect", "--password", "incorrect")
Expect(output).To(ContainSubstring("Incorrect username or password"))

output = functional_tests.Run(hoverctlBinary, "login", "--username", "incorrect", "--password", "incorrect")
Expect(output).To(ContainSubstring("Too many failed login attempts, please wait 10 minutes"))
})

It("should error nicely if username is missing", func() {
Expand All @@ -59,7 +73,7 @@ var _ = Describe("hoverctl login", func() {
It("should error nicely if it cannot connect", func() {
output := functional_tests.Run(hoverctlBinary, "login", "--username", username, "--password", password)

Expect(output).To(ContainSubstring("Failed to login to Hoverfly"))
Expect(output).To(ContainSubstring("There was an error when logging in"))
})
})

Expand Down
7 changes: 1 addition & 6 deletions hoverctl/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"errors"
"fmt"
"os"

"github.com/SpectoLabs/hoverfly/hoverctl/wrapper"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -55,11 +54,7 @@ target in the hoverctl configuration file.

token, err := wrapper.Login(*target, username, password)
if err != nil {
if verbose {
fmt.Fprintln(os.Stderr, err.Error())
}

handleIfError(errors.New("Failed to login to Hoverfly"))
handleIfError(err)
}

target.AuthToken = token
Expand Down
18 changes: 13 additions & 5 deletions hoverctl/wrapper/hoverfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,12 @@ func Login(target Target, username, password string) (string, error) {

jsonCredentials, err := json.Marshal(credentials)
if err != nil {
return "", err
return "", fmt.Errorf("There was an error when preparing to login")
}

request, err := http.NewRequest("POST", BuildURL(target, "/api/token-auth"), strings.NewReader(string(jsonCredentials)))
if err != nil {
return "", err
return "", fmt.Errorf("There was an error when preparing to login")
}

client := &http.Client{
Expand All @@ -308,18 +308,26 @@ func Login(target Target, username, password string) (string, error) {

response, err := client.Do(request)
if err != nil {
return "", err
return "", fmt.Errorf("There was an error when logging in")
}

if response.StatusCode == http.StatusTooManyRequests {
return "", fmt.Errorf("Too many failed login attempts, please wait 10 minutes")
}

if response.StatusCode == http.StatusUnauthorized {
return "", fmt.Errorf("Incorrect username or password")
}

body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
return "", fmt.Errorf("There was an error when logging in")
}

var authToken HoverflyAuthTokenSchema
err = json.Unmarshal(body, &authToken)
if err != nil {
return "", err
return "", fmt.Errorf("There was an error when logging in")
}

return authToken.Token, nil
Expand Down

0 comments on commit 7ed578b

Please sign in to comment.