2016-09-06 14:46:37 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2016-10-18 20:52:46 -04:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2016-09-06 14:46:37 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegistryLogin authenticates the docker server with a given docker registry.
|
|
|
|
// It returns UnauthorizerError when the authentication fails.
|
2016-10-18 20:52:46 -04:00
|
|
|
func (cli *Client) RegistryLogin(ctx context.Context, auth types.AuthConfig) (registry.AuthenticateOKBody, error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
resp, err := cli.post(ctx, "/auth", url.Values{}, auth, nil)
|
|
|
|
|
|
|
|
if resp.statusCode == http.StatusUnauthorized {
|
2016-10-18 20:52:46 -04:00
|
|
|
return registry.AuthenticateOKBody{}, unauthorizedError{err}
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
2016-10-18 20:52:46 -04:00
|
|
|
return registry.AuthenticateOKBody{}, err
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 20:52:46 -04:00
|
|
|
var response registry.AuthenticateOKBody
|
2016-09-06 14:46:37 -04:00
|
|
|
err = json.NewDecoder(resp.body).Decode(&response)
|
|
|
|
ensureReaderClosed(resp)
|
|
|
|
return response, err
|
|
|
|
}
|