1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // netrcauth uses a .netrc file (or _netrc file on Windows) to implement the
6 // GOAUTH protocol described in https://golang.org/issue/26232.
7 // It expects the location of the file as the first command-line argument.
9 // Example GOAUTH usage:
10 // export GOAUTH="netrcauth $HOME/.netrc"
12 // See https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html
13 // or run 'man 5 netrc' for a description of the .netrc file format.
28 fmt.Fprintf(os.Stderr, "usage: %s NETRCFILE [URL]", os.Args[0])
32 log.SetPrefix("netrcauth: ")
34 if len(os.Args) != 2 {
35 // An explicit URL was passed on the command line, but netrcauth does not
36 // have any URL-specific output: it dumps the entire .netrc file at the
43 data, err := ioutil.ReadFile(path)
45 if os.IsNotExist(err) {
48 log.Fatalf("failed to read %s: %v\n", path, err)
51 u := &url.URL{Scheme: "https"}
52 lines := parseNetrc(string(data))
53 for _, l := range lines {
55 fmt.Printf("%s\n\n", u)
57 req := &http.Request{Header: make(http.Header)}
58 req.SetBasicAuth(l.login, l.password)
59 req.Header.Write(os.Stdout)
64 // The following functions were extracted from src/cmd/go/internal/web2/web.go
65 // as of https://golang.org/cl/161698.
67 type netrcLine struct {
73 func parseNetrc(data string) []netrcLine {
74 // See https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html
75 // for documentation on the .netrc format.
79 for _, line := range strings.Split(data, "\n") {
87 f := strings.Fields(line)
89 for ; i < len(f)-1; i += 2 {
90 // Reset at each "machine" token.
91 // “The auto-login process searches the .netrc file for a machine token
92 // that matches […]. Once a match is made, the subsequent .netrc tokens
93 // are processed, stopping when the end of file is reached or another
94 // machine or a default token is encountered.”
97 l = netrcLine{machine: f[i+1]}
105 // “A macro is defined with the specified name; its contents begin with
106 // the next .netrc line and continue until a null line (consecutive
107 // new-line characters) is encountered.”
110 if l.machine != "" && l.login != "" && l.password != "" {
116 if i < len(f) && f[i] == "default" {
117 // “There can be only one default token, and it must be after all machine tokens.”