Add more useful error for bad credentials. (#51)

* 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.
This commit is contained in:
Chris Smith 2017-12-11 15:42:19 +00:00 committed by Jess Frazelle
parent 3bc47ed926
commit 0ff43808ca
2 changed files with 6 additions and 1 deletions

View file

@ -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*$`)
@ -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 {

View file

@ -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 {