Source file src/cmd/go/internal/auth/auth.go

     1  // Copyright 2019 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.
     4  
     5  // Package auth provides access to user-provided authentication credentials.
     6  package auth
     7  
     8  import "net/http"
     9  
    10  // AddCredentials fills in the user's credentials for req, if any.
    11  // The return value reports whether any matching credentials were found.
    12  func AddCredentials(req *http.Request) (added bool) {
    13  	host := req.Host
    14  	if host == "" {
    15  		host = req.URL.Hostname()
    16  	}
    17  
    18  	// TODO(golang.org/issue/26232): Support arbitrary user-provided credentials.
    19  	netrcOnce.Do(readNetrc)
    20  	for _, l := range netrc {
    21  		if l.machine == host {
    22  			req.SetBasicAuth(l.login, l.password)
    23  			return true
    24  		}
    25  	}
    26  
    27  	return false
    28  }
    29  

View as plain text