Skip to content

Commit

Permalink
Merge pull request #175 from weters/authz-basic-fix
Browse files Browse the repository at this point in the history
URL decode the client_id and client_secret in Authz header
  • Loading branch information
RangelReale authored Feb 22, 2018
2 parents 2dc1b43 + fab9fa0 commit a05b753
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
16 changes: 15 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"errors"
"net/http"
"net/url"
"strings"
)

Expand Down Expand Up @@ -51,7 +52,20 @@ func CheckBasicAuth(r *http.Request) (*BasicAuth, error) {
return nil, errors.New("Invalid authorization message")
}

return &BasicAuth{Username: pair[0], Password: pair[1]}, nil
// Decode the client_id and client_secret pairs as per
// https://tools.ietf.org/html/rfc6749#section-2.3.1

username, err := url.QueryUnescape(pair[0])
if err != nil {
return nil, err
}

password, err := url.QueryUnescape(pair[1])
if err != nil {
return nil, err
}

return &BasicAuth{Username: username, Password: password}, nil
}

// Return "Bearer" token from request. The header has precedence over query string.
Expand Down
26 changes: 22 additions & 4 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
)

const (
badAuthValue = "Digest XHHHHHHH"
goodAuthValue = "Basic dGVzdDp0ZXN0"
goodBearerAuthValue = "Bearer BGFVTDUJDp0ZXN0"
badAuthValue = "Digest XHHHHHHH"
badUsernameInAuthValue = "Basic dSUyc2VybmFtZTpwYXNzd29yZA==" // u%2sername:password
badPasswordInAuthValue = "Basic dXNlcm5hbWU6cGElMnN3b3Jk" // username:pa%2sword
goodAuthValue = "Basic Y2xpZW50K25hbWU6Y2xpZW50KyUyNGVjcmV0"
goodBearerAuthValue = "Bearer BGFVTDUJDp0ZXN0"
)

func TestBasicAuth(t *testing.T) {
Expand All @@ -28,6 +30,22 @@ func TestBasicAuth(t *testing.T) {
return
}

// with invalid username
r.Header.Set("Authorization", badUsernameInAuthValue)
b, err = CheckBasicAuth(r)
if b != nil || err == nil {
t.Errorf("Validated invalid auth with bad username")
return
}

// with invalid username
r.Header.Set("Authorization", badPasswordInAuthValue)
b, err = CheckBasicAuth(r)
if b != nil || err == nil {
t.Errorf("Validated invalid auth with bad password")
return
}

// with valid header
r.Header.Set("Authorization", goodAuthValue)
b, err = CheckBasicAuth(r)
Expand All @@ -37,7 +55,7 @@ func TestBasicAuth(t *testing.T) {
}

// check extracted auth data
if b.Username != "test" || b.Password != "test" {
if b.Username != "client name" || b.Password != "client $ecret" {
t.Errorf("Error decoding basic auth")
}
}
Expand Down

0 comments on commit a05b753

Please sign in to comment.