1 // Copyright 2015 go-dockerclient authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
20 // ErrCannotParseDockercfg is the error returned by NewAuthConfigurations when the dockercfg cannot be parsed.
21 var ErrCannotParseDockercfg = errors.New("Failed to read authentication from dockercfg")
23 // AuthConfiguration represents authentication options to use in the PushImage
24 // method. It represents the authentication in the Docker index server.
25 type AuthConfiguration struct {
26 Username string `json:"username,omitempty"`
27 Password string `json:"password,omitempty"`
28 Email string `json:"email,omitempty"`
29 ServerAddress string `json:"serveraddress,omitempty"`
32 // AuthConfigurations represents authentication options to use for the
33 // PushImage method accommodating the new X-Registry-Config header
34 type AuthConfigurations struct {
35 Configs map[string]AuthConfiguration `json:"configs"`
38 // AuthConfigurations119 is used to serialize a set of AuthConfigurations
39 // for Docker API >= 1.19.
40 type AuthConfigurations119 map[string]AuthConfiguration
42 // dockerConfig represents a registry authentation configuration from the
44 type dockerConfig struct {
45 Auth string `json:"auth"`
46 Email string `json:"email"`
49 // NewAuthConfigurationsFromDockerCfg returns AuthConfigurations from the
51 func NewAuthConfigurationsFromDockerCfg() (*AuthConfigurations, error) {
54 p := path.Join(os.Getenv("HOME"), ".docker", "config.json")
57 p := path.Join(os.Getenv("HOME"), ".dockercfg")
63 return NewAuthConfigurations(r)
66 // NewAuthConfigurations returns AuthConfigurations from a JSON encoded string in the
67 // same format as the .dockercfg file.
68 func NewAuthConfigurations(r io.Reader) (*AuthConfigurations, error) {
69 var auth *AuthConfigurations
70 confs, err := parseDockerConfig(r)
74 auth, err = authConfigs(confs)
81 func parseDockerConfig(r io.Reader) (map[string]dockerConfig, error) {
82 buf := new(bytes.Buffer)
84 byteData := buf.Bytes()
86 confsWrapper := struct {
87 Auths map[string]dockerConfig `json:"auths"`
89 if err := json.Unmarshal(byteData, &confsWrapper); err == nil {
90 if len(confsWrapper.Auths) > 0 {
91 return confsWrapper.Auths, nil
95 var confs map[string]dockerConfig
96 if err := json.Unmarshal(byteData, &confs); err != nil {
102 // authConfigs converts a dockerConfigs map to a AuthConfigurations object.
103 func authConfigs(confs map[string]dockerConfig) (*AuthConfigurations, error) {
104 c := &AuthConfigurations{
105 Configs: make(map[string]AuthConfiguration),
107 for reg, conf := range confs {
108 data, err := base64.StdEncoding.DecodeString(conf.Auth)
112 userpass := strings.SplitN(string(data), ":", 2)
113 if len(userpass) != 2 {
114 return nil, ErrCannotParseDockercfg
116 c.Configs[reg] = AuthConfiguration{
118 Username: userpass[0],
119 Password: userpass[1],
126 // AuthStatus returns the authentication status for Docker API versions >= 1.23.
127 type AuthStatus struct {
128 Status string `json:"Status,omitempty" yaml:"Status,omitempty"`
129 IdentityToken string `json:"IdentityToken,omitempty" yaml:"IdentityToken,omitempty"`
132 // AuthCheck validates the given credentials. It returns nil if successful.
134 // For Docker API versions >= 1.23, the AuthStatus struct will be populated, otherwise it will be empty.`
136 // See https://goo.gl/6nsZkH for more details.
137 func (c *Client) AuthCheck(conf *AuthConfiguration) (AuthStatus, error) {
138 var authStatus AuthStatus
140 return authStatus, fmt.Errorf("conf is nil")
142 resp, err := c.do("POST", "/auth", doOptions{data: conf})
144 return authStatus, err
146 defer resp.Body.Close()
147 data, err := ioutil.ReadAll(resp.Body)
149 return authStatus, err
152 return authStatus, nil
154 if err := json.Unmarshal(data, &authStatus); err != nil {
155 return authStatus, err
157 return authStatus, nil