github.com/infraboard/keyauth@v0.8.1/common/tools/auth.go (about) 1 package tools 2 3 import ( 4 "encoding/base64" 5 "strings" 6 ) 7 8 // ParseBasicAuth parses an HTTP Basic Authentication string. 9 // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true). 10 func ParseBasicAuth(auth string) (username, password string, ok bool) { 11 const prefix = "Basic " 12 // Case insensitive prefix match. See Issue 22736. 13 if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) { 14 return 15 } 16 c, err := base64.StdEncoding.DecodeString(auth[len(prefix):]) 17 if err != nil { 18 return 19 } 20 cs := string(c) 21 s := strings.IndexByte(cs, ':') 22 if s < 0 { 23 return 24 } 25 return cs[:s], cs[s+1:], true 26 }