From dce6f8ba767eaa6d7edcae279cecab6609579699 Mon Sep 17 00:00:00 2001 From: Jessica Frazelle Date: Mon, 30 Mar 2015 15:27:46 -0700 Subject: [PATCH] fix basicAuth function not in go1.3.3 Docker-DCO-1.1-Signed-off-by: Jessie Frazelle (github: jfrazelle) Docker-DCO-1.1-Signed-off-by: Jessie Frazelle (github: jfrazelle) --- requestdecorator/requestdecorator_test.go | 40 +++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/requestdecorator/requestdecorator_test.go b/requestdecorator/requestdecorator_test.go index 5f1c256..12e2194 100644 --- a/requestdecorator/requestdecorator_test.go +++ b/requestdecorator/requestdecorator_test.go @@ -1,11 +1,45 @@ package requestdecorator import ( + "encoding/base64" "net/http" "strings" "testing" ) +// The following 2 functions are here for 1.3.3 support +// After we drop 1.3.3 support we can use the functions supported +// in go v1.4.0 + +// BasicAuth returns the username and password provided in the request's +// Authorization header, if the request uses HTTP Basic Authentication. +// See RFC 2617, Section 2. +func basicAuth(r *http.Request) (username, password string, ok bool) { + auth := r.Header.Get("Authorization") + if auth == "" { + return + } + return parseBasicAuth(auth) +} + +// parseBasicAuth parses an HTTP Basic Authentication string. +// "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true). +func parseBasicAuth(auth string) (username, password string, ok bool) { + const prefix = "Basic " + if !strings.HasPrefix(auth, prefix) { + return + } + c, err := base64.StdEncoding.DecodeString(auth[len(prefix):]) + if err != nil { + return + } + cs := string(c) + s := strings.IndexByte(cs, ':') + if s < 0 { + return + } + return cs[:s], cs[s+1:], true +} + func TestUAVersionInfo(t *testing.T) { uavi := NewUAVersionInfo("foo", "bar") if !uavi.isValid() { @@ -113,7 +147,7 @@ func TestAuthDecorator(t *testing.T) { t.Fatal(err) } - username, password, ok := reqDecorated.BasicAuth() + username, password, ok := basicAuth(reqDecorated) if !ok { t.Fatalf("Cannot retrieve basic auth info from request") } @@ -155,7 +189,7 @@ func TestRequestFactory(t *testing.T) { t.Fatal(err) } - username, password, ok := req.BasicAuth() + username, password, ok := basicAuth(req) if !ok { t.Fatalf("Cannot retrieve basic auth info from request") } @@ -186,7 +220,7 @@ func TestRequestFactoryNewRequestWithDecorators(t *testing.T) { t.Fatal(err) } - username, password, ok := req.BasicAuth() + username, password, ok := basicAuth(req) if !ok { t.Fatalf("Cannot retrieve basic auth info from request") }