github.com/lusis/distribution@v2.0.1+incompatible/registry/handlers/basicauth_prego14.go (about)

     1  // +build !go1.4
     2  
     3  package handlers
     4  
     5  import (
     6  	"encoding/base64"
     7  	"net/http"
     8  	"strings"
     9  )
    10  
    11  // NOTE(stevvooe): This is basic auth support from go1.4 present to ensure we
    12  // can compile on go1.3 and earlier.
    13  
    14  // BasicAuth returns the username and password provided in the request's
    15  // Authorization header, if the request uses HTTP Basic Authentication.
    16  // See RFC 2617, Section 2.
    17  func basicAuth(r *http.Request) (username, password string, ok bool) {
    18  	auth := r.Header.Get("Authorization")
    19  	if auth == "" {
    20  		return
    21  	}
    22  	return parseBasicAuth(auth)
    23  }
    24  
    25  // parseBasicAuth parses an HTTP Basic Authentication string.
    26  // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
    27  func parseBasicAuth(auth string) (username, password string, ok bool) {
    28  	if !strings.HasPrefix(auth, "Basic ") {
    29  		return
    30  	}
    31  	c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(auth, "Basic "))
    32  	if err != nil {
    33  		return
    34  	}
    35  	cs := string(c)
    36  	s := strings.IndexByte(cs, ':')
    37  	if s < 0 {
    38  		return
    39  	}
    40  	return cs[:s], cs[s+1:], true
    41  }