2015-02-07 00:19:19 +00:00
|
|
|
// +build !go1.4
|
|
|
|
|
2015-02-11 01:25:40 +00:00
|
|
|
package handlers
|
2015-02-07 00:19:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NOTE(stevvooe): This is basic auth support from go1.4 present to ensure we
|
|
|
|
// can compile on go1.3 and earlier.
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
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
|
|
|
|
}
|