From e16f81838b2cfff221ed767a705188797b237cd3 Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Mon, 28 Sep 2015 14:02:55 -0700 Subject: [PATCH] etcdhttp/auth: BasicAuth method in standard pkg I created a new PR from https://github.com/coreos/etcd/pull/3598. This is for `TODO: use the standard lib BasicAuth method when we move to Go 1.4.` [1]. `BasicAuth` method got into Go standard package a year ago. [2] --- 1. https://github.com/coreos/etcd/blob/master/pkg/netutil/netutil.go#L126-L138 2. https://codereview.appspot.com/76540043/ --- etcdserver/etcdhttp/client_auth.go | 5 ++--- pkg/netutil/netutil.go | 36 ------------------------------ 2 files changed, 2 insertions(+), 39 deletions(-) diff --git a/etcdserver/etcdhttp/client_auth.go b/etcdserver/etcdhttp/client_auth.go index aa9def0d2..e4477b382 100644 --- a/etcdserver/etcdhttp/client_auth.go +++ b/etcdserver/etcdhttp/client_auth.go @@ -23,7 +23,6 @@ import ( "github.com/coreos/etcd/etcdserver" "github.com/coreos/etcd/etcdserver/auth" "github.com/coreos/etcd/etcdserver/etcdhttp/httptypes" - "github.com/coreos/etcd/pkg/netutil" ) type authHandler struct { @@ -46,7 +45,7 @@ func hasRootAccess(sec auth.Store, r *http.Request) bool { if !sec.AuthEnabled() { return true } - username, password, ok := netutil.BasicAuth(r) + username, password, ok := r.BasicAuth() if !ok { return false } @@ -80,7 +79,7 @@ func hasKeyPrefixAccess(sec auth.Store, r *http.Request, key string, recursive b plog.Warningf("auth: no authorization provided, checking guest access") return hasGuestAccess(sec, r, key) } - username, password, ok := netutil.BasicAuth(r) + username, password, ok := r.BasicAuth() if !ok { plog.Warningf("auth: malformed basic auth encoding") return false diff --git a/pkg/netutil/netutil.go b/pkg/netutil/netutil.go index 6c1dc96c8..62aacf2c3 100644 --- a/pkg/netutil/netutil.go +++ b/pkg/netutil/netutil.go @@ -15,13 +15,10 @@ package netutil import ( - "encoding/base64" "net" - "net/http" "net/url" "reflect" "sort" - "strings" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog" "github.com/coreos/etcd/pkg/types" @@ -118,36 +115,3 @@ func URLStringsEqual(a []string, b []string) bool { return urlsEqual(urlsA, urlsB) } - -// 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. -// Based on the BasicAuth method from the Golang standard lib. -// TODO: use the standard lib BasicAuth method when we move to Go 1.4. -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). -// Taken from the Golang standard lib. -// TODO: use the standard lib BasicAuth method when we move to Go 1.4. -func parseBasicAuth(auth string) (username, password string, ok bool) { - if !strings.HasPrefix(auth, "Basic ") { - return - } - c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(auth, "Basic ")) - if err != nil { - return - } - cs := string(c) - s := strings.IndexByte(cs, ':') - if s < 0 { - return - } - return cs[:s], cs[s+1:], true -}