Skip to content

Commit

Permalink
Add more useful error for bad credentials. (#51)
Browse files Browse the repository at this point in the history
* Add more useful error for bad credentials.

The www-authenticate: basic response currently gets caught by
the token transport, which fails to parse it and spits out
a rather oblique "malformed auth challenge header" error.

Make the token transport ignore basic auth types, and make
the error transport handle a 401 response.

* Format authchallenge.go correctly.
  • Loading branch information
csmith authored and jessfraz committed Dec 11, 2017
1 parent 3bc47ed commit 0ff4380
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions registry/authchallenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
var (
authChallengeRegex = regexp.MustCompile(
`^\s*Bearer\s+realm="([^"]+)",service="([^"]+)"\s*$`)
basicRegex = regexp.MustCompile(`^\s*Basic\s+.*$`)
challengeRegex = regexp.MustCompile(
`^\s*Bearer\s+realm="([^"]+)",service="([^"]+)",scope="([^"]+)"\s*$`)

Expand All @@ -26,6 +27,10 @@ func parseAuthHeader(header http.Header) (*authService, error) {
}

func parseChallenge(challengeHeader string) (*authService, error) {
if basicRegex.MatchString(challengeHeader) {
return nil, nil
}

match := challengeRegex.FindAllStringSubmatch(challengeHeader, -1)

if len(match) != 1 {
Expand Down
2 changes: 1 addition & 1 deletion registry/errortransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (t *ErrorTransport) RoundTrip(request *http.Request) (*http.Response, error
return resp, err
}

if resp.StatusCode >= 500 {
if resp.StatusCode >= 500 || resp.StatusCode == http.StatusUnauthorized {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down

0 comments on commit 0ff4380

Please sign in to comment.