func tryFindNetrcFileCreds()

in httputil/httputil.go [142:169]


func tryFindNetrcFileCreds(host string) (string, error) {
	dir, err := homedir.Dir()
	if err != nil {
		return "", err
	}

	var file = filepath.Join(dir, ".netrc")
	n, err := netrc.Parse(file)
	if err != nil {
		// netrc does not exist or we can't read it
		return "", err
	}

	m := n.Machine(host)
	if m == nil {
		// if host is not found, we should proceed without providing any Authorization header,
		// because remote host may not have auth at all.
		log.Printf("Skipping basic authentication for %s because no credentials found in %s", host, file)
		return "", fmt.Errorf("could not find creds for %s in netrc %s", host, file)
	}

	log.Printf("Using basic authentication credentials for host %s from %s", host, file)

	login := m.Get("login")
	pwd := m.Get("password")
	token := b64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", login, pwd)))
	return fmt.Sprintf("Basic %s", token), nil
}