github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/client/login.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"golang.org/x/net/context"
    10  )
    11  
    12  // RegistryLogin authenticates the docker server with a given docker registry.
    13  // It returns UnauthorizerError when the authentication fails.
    14  func (cli *Client) RegistryLogin(ctx context.Context, auth types.AuthConfig) (types.AuthResponse, error) {
    15  	resp, err := cli.post(ctx, "/auth", url.Values{}, auth, nil)
    16  
    17  	if resp.statusCode == http.StatusUnauthorized {
    18  		return types.AuthResponse{}, unauthorizedError{err}
    19  	}
    20  	if err != nil {
    21  		return types.AuthResponse{}, err
    22  	}
    23  
    24  	var response types.AuthResponse
    25  	err = json.NewDecoder(resp.body).Decode(&response)
    26  	ensureReaderClosed(resp)
    27  	return response, err
    28  }