gitgithubcookies.netrc

GitHub cookiefile


Since Git uses cURL for HTTP, I am able to have a file ~/.netrc [1] like this:

machine github.com
login 89z
password [Personal access token]

However it seems that another option is available, using cookies [2]. It looks like the syntax would be like this:

[http]
   saveCookies = true
   cookiefile = C:/cookie.txt

My question is, can this be used to push to GitHub? If so, how would I create the cookie file?

  1. https://curl.se/libcurl/c/CURLOPT_NETRC.html
  2. https://git-scm.com/docs/git-config#Documentation/git-config.txt-httpcookieFile

Solution

  • The reason I wanted to move away from Netrc, was because not all languages have built-in support to parse. At any rate, I found that the spec [1] and one popular implementation [2] just assume the tokens are separated by whitespace. So if you have control over the Netrc file, then you can just put each entry on its own line, then it's very easy to parse. Here is example with Go:

    package main
    
    import (
       "fmt"
       "net/http"
       "os"
    )
    
    func netrc(addr string) (*http.Request, error) {
       home, err := os.UserHomeDir()
       if err != nil { return nil, err }
       file, err := os.Open(home + "/_netrc")
       if err != nil { return nil, err }
       defer file.Close()
       var login, pass string
       fmt.Fscanf(file, "default login %v password %v", &login, &pass)
       req, err := http.NewRequest("GET", addr, nil)
       if err != nil { return nil, err }
       req.SetBasicAuth(login, pass)
       return req, nil
    }
    
    func main() {
       req, err := netrc("https://api.github.com/rate_limit")
       if err != nil {
          panic(err)
       }
       res, err := new(http.Client).Do(req)
       if err != nil {
          panic(err)
       }
       fmt.Println(res)
    }
    
    1. https://gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html
    2. https://github.com/curl/curl/blob/33ba0ecf/lib/netrc.c#L87