client/login.go
7c36a1af
 package client
 
 import (
 	"encoding/json"
 	"net/http"
 	"net/url"
 
 	"github.com/docker/docker/api/types"
2732b8a9
 	"github.com/docker/docker/api/types/registry"
7c36a1af
 	"golang.org/x/net/context"
 )
 
 // RegistryLogin authenticates the docker server with a given docker registry.
16233eb0
 // It returns unauthorizedError when the authentication fails.
2732b8a9
 func (cli *Client) RegistryLogin(ctx context.Context, auth types.AuthConfig) (registry.AuthenticateOKBody, error) {
7c36a1af
 	resp, err := cli.post(ctx, "/auth", url.Values{}, auth, nil)
 
 	if resp.statusCode == http.StatusUnauthorized {
2732b8a9
 		return registry.AuthenticateOKBody{}, unauthorizedError{err}
7c36a1af
 	}
 	if err != nil {
2732b8a9
 		return registry.AuthenticateOKBody{}, err
7c36a1af
 	}
 
2732b8a9
 	var response registry.AuthenticateOKBody
7c36a1af
 	err = json.NewDecoder(resp.body).Decode(&response)
 	ensureReaderClosed(resp)
 	return response, err
 }