github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/proxy/proxyauth.go (about)

     1  package proxy
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  
     7  	"github.com/docker/distribution/registry/client/auth"
     8  )
     9  
    10  const tokenURL = "https://auth.docker.io/token"
    11  
    12  type userpass struct {
    13  	username string
    14  	password string
    15  }
    16  
    17  type credentials struct {
    18  	creds map[string]userpass
    19  }
    20  
    21  func (c credentials) Basic(u *url.URL) (string, string) {
    22  	up := c.creds[u.String()]
    23  
    24  	return up.username, up.password
    25  }
    26  
    27  // ConfigureAuth authorizes with the upstream registry
    28  func ConfigureAuth(remoteURL, username, password string, cm auth.ChallengeManager) (auth.CredentialStore, error) {
    29  	if err := ping(cm, remoteURL+"/v2/", "Docker-Distribution-Api-Version"); err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	creds := map[string]userpass{
    34  		tokenURL: {
    35  			username: username,
    36  			password: password,
    37  		},
    38  	}
    39  	return credentials{creds: creds}, nil
    40  }
    41  
    42  func ping(manager auth.ChallengeManager, endpoint, versionHeader string) error {
    43  	resp, err := http.Get(endpoint)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	defer resp.Body.Close()
    48  
    49  	if err := manager.AddResponse(resp); err != nil {
    50  		return err
    51  	}
    52  
    53  	return nil
    54  }