Skip to content

Commit

Permalink
Fix BASIC challenge (#191)
Browse files Browse the repository at this point in the history
* Fix BASIC challenge

Java people like to yell, which `strings.Title` doesn't handle well.

* Add Canonical() to challenge
* Make canonical form lowercase
  • Loading branch information
jonjohnsonjr authored Jun 2, 2018
1 parent e033bc7 commit 34a6841
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions v1/remote/transport/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
type challenge string

const (
anonymous challenge = "Anonymous"
basic challenge = "Basic"
bearer challenge = "Bearer"
anonymous challenge = "anonymous"
basic challenge = "basic"
bearer challenge = "bearer"
)

type pingResp struct {
Expand All @@ -38,6 +38,10 @@ type pingResp struct {
parameters map[string]string
}

func (c challenge) Canonical() challenge {
return challenge(strings.ToLower(string(c)))
}

func parseChallenge(suffix string) map[string]string {
kv := make(map[string]string)
for _, token := range strings.Split(suffix, ",") {
Expand Down Expand Up @@ -75,13 +79,13 @@ func ping(reg name.Registry, t http.RoundTripper) (*pingResp, error) {
if parts := strings.SplitN(wac, " ", 2); len(parts) == 2 {
// If there are two parts, then parse the challenge parameters.
return &pingResp{
challenge: challenge(strings.Title(parts[0])),
challenge: challenge(parts[0]).Canonical(),
parameters: parseChallenge(parts[1]),
}, nil
}
// Otherwise, just return the challenge without parameters.
return &pingResp{
challenge: challenge(strings.Title(wac)),
challenge: challenge(wac).Canonical(),
}, nil
default:
return nil, fmt.Errorf("unrecognized HTTP status: %v", resp.Status)
Expand Down
2 changes: 1 addition & 1 deletion v1/remote/transport/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestPingNoChallenge(t *testing.T) {
func TestPingBasicChallengeNoParams(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic`)
w.Header().Set("WWW-Authenticate", `BASIC`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
}))
defer server.Close()
Expand Down
2 changes: 1 addition & 1 deletion v1/remote/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func New(reg name.Registry, auth authn.Authenticator, t http.RoundTripper, scope
return nil, err
}

switch pr.challenge {
switch pr.challenge.Canonical() {
case anonymous:
return t, nil
case basic:
Expand Down

0 comments on commit 34a6841

Please sign in to comment.