MCPcopy
hub / github.com/perkeep/perkeep / BasicAuth

Function BasicAuth

internal/httputil/auth.go:83–107  ·  view source on GitHub ↗

BasicAuth parses the Authorization header on req If absent or invalid, an error is returned.

(req *http.Request)

Source from the content-addressed store, hash-verified

81// BasicAuth parses the Authorization header on req
82// If absent or invalid, an error is returned.
83func BasicAuth(req *http.Request) (username, password string, err error) {
84 auth := req.Header.Get("Authorization")
85 if auth == "" {
86 err = fmt.Errorf("Missing \"Authorization\" in header")
87 return
88 }
89 matches := basicAuthPattern.FindStringSubmatch(auth)
90 if len(matches) != 2 {
91 err = fmt.Errorf("Bogus Authorization header")
92 return
93 }
94 encoded := matches[1]
95 enc := base64.StdEncoding
96 decBuf := make([]byte, enc.DecodedLen(len(encoded)))
97 n, err := enc.Decode(decBuf, []byte(encoded))
98 if err != nil {
99 return
100 }
101 pieces := strings.SplitN(string(decBuf[0:n]), ":", 2)
102 if len(pieces) != 2 {
103 err = fmt.Errorf("didn't get two pieces")
104 return
105 }
106 return pieces[0], pieces[1], nil
107}

Callers 3

AllowedAccessMethod · 0.92
AllowedAccessMethod · 0.92
TestBasicAuthFunction · 0.85

Calls 1

GetMethod · 0.65

Tested by 1

TestBasicAuthFunction · 0.68