|
@@ -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")
|
|
|
}
|