|
@@ -1,45 +1,11 @@
|
|
package requestdecorator
|
|
package requestdecorator
|
|
|
|
|
|
import (
|
|
import (
|
|
- "encoding/base64"
|
|
|
|
"net/http"
|
|
"net/http"
|
|
"strings"
|
|
"strings"
|
|
"testing"
|
|
"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) {
|
|
func TestUAVersionInfo(t *testing.T) {
|
|
uavi := NewUAVersionInfo("foo", "bar")
|
|
uavi := NewUAVersionInfo("foo", "bar")
|
|
if !uavi.isValid() {
|
|
if !uavi.isValid() {
|
|
@@ -147,7 +113,7 @@ func TestAuthDecorator(t *testing.T) {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
|
|
- username, password, ok := basicAuth(reqDecorated)
|
|
|
|
|
|
+ username, password, ok := reqDecorated.BasicAuth()
|
|
if !ok {
|
|
if !ok {
|
|
t.Fatalf("Cannot retrieve basic auth info from request")
|
|
t.Fatalf("Cannot retrieve basic auth info from request")
|
|
}
|
|
}
|
|
@@ -189,7 +155,7 @@ func TestRequestFactory(t *testing.T) {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
|
|
- username, password, ok := basicAuth(req)
|
|
|
|
|
|
+ username, password, ok := req.BasicAuth()
|
|
if !ok {
|
|
if !ok {
|
|
t.Fatalf("Cannot retrieve basic auth info from request")
|
|
t.Fatalf("Cannot retrieve basic auth info from request")
|
|
}
|
|
}
|
|
@@ -220,7 +186,7 @@ func TestRequestFactoryNewRequestWithDecorators(t *testing.T) {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
|
|
- username, password, ok := basicAuth(req)
|
|
|
|
|
|
+ username, password, ok := req.BasicAuth()
|
|
if !ok {
|
|
if !ok {
|
|
t.Fatalf("Cannot retrieve basic auth info from request")
|
|
t.Fatalf("Cannot retrieve basic auth info from request")
|
|
}
|
|
}
|