github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/api/client/lib/login.go (about)

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