login.go 708 B

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