authconfig.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package registry // import "github.com/docker/docker/api/types/registry"
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "io"
  6. "strings"
  7. "github.com/pkg/errors"
  8. )
  9. // AuthHeader is the name of the header used to send encoded registry
  10. // authorization credentials for registry operations (push/pull).
  11. const AuthHeader = "X-Registry-Auth"
  12. // AuthConfig contains authorization information for connecting to a Registry.
  13. type AuthConfig struct {
  14. Username string `json:"username,omitempty"`
  15. Password string `json:"password,omitempty"`
  16. Auth string `json:"auth,omitempty"`
  17. // Email is an optional value associated with the username.
  18. // This field is deprecated and will be removed in a later
  19. // version of docker.
  20. Email string `json:"email,omitempty"`
  21. ServerAddress string `json:"serveraddress,omitempty"`
  22. // IdentityToken is used to authenticate the user and get
  23. // an access token for the registry.
  24. IdentityToken string `json:"identitytoken,omitempty"`
  25. // RegistryToken is a bearer token to be sent to a registry
  26. RegistryToken string `json:"registrytoken,omitempty"`
  27. }
  28. // EncodeAuthConfig serializes the auth configuration as a base64url encoded
  29. // RFC4648, section 5) JSON string for sending through the X-Registry-Auth header.
  30. //
  31. // For details on base64url encoding, see:
  32. // - RFC4648, section 5: https://tools.ietf.org/html/rfc4648#section-5
  33. func EncodeAuthConfig(authConfig AuthConfig) (string, error) {
  34. buf, err := json.Marshal(authConfig)
  35. if err != nil {
  36. return "", errInvalidParameter{err}
  37. }
  38. return base64.URLEncoding.EncodeToString(buf), nil
  39. }
  40. // DecodeAuthConfig decodes base64url encoded (RFC4648, section 5) JSON
  41. // authentication information as sent through the X-Registry-Auth header.
  42. //
  43. // This function always returns an AuthConfig, even if an error occurs. It is up
  44. // to the caller to decide if authentication is required, and if the error can
  45. // be ignored.
  46. //
  47. // For details on base64url encoding, see:
  48. // - RFC4648, section 5: https://tools.ietf.org/html/rfc4648#section-5
  49. func DecodeAuthConfig(authEncoded string) (*AuthConfig, error) {
  50. if authEncoded == "" {
  51. return &AuthConfig{}, nil
  52. }
  53. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  54. return decodeAuthConfigFromReader(authJSON)
  55. }
  56. // DecodeAuthConfigBody decodes authentication information as sent as JSON in the
  57. // body of a request. This function is to provide backward compatibility with old
  58. // clients and API versions. Current clients and API versions expect authentication
  59. // to be provided through the X-Registry-Auth header.
  60. //
  61. // Like DecodeAuthConfig, this function always returns an AuthConfig, even if an
  62. // error occurs. It is up to the caller to decide if authentication is required,
  63. // and if the error can be ignored.
  64. func DecodeAuthConfigBody(rdr io.ReadCloser) (*AuthConfig, error) {
  65. return decodeAuthConfigFromReader(rdr)
  66. }
  67. func decodeAuthConfigFromReader(rdr io.Reader) (*AuthConfig, error) {
  68. authConfig := &AuthConfig{}
  69. if err := json.NewDecoder(rdr).Decode(authConfig); err != nil {
  70. // always return an (empty) AuthConfig to increase compatibility with
  71. // the existing API.
  72. return &AuthConfig{}, invalid(err)
  73. }
  74. return authConfig, nil
  75. }
  76. func invalid(err error) error {
  77. return errInvalidParameter{errors.Wrap(err, "invalid X-Registry-Auth header")}
  78. }
  79. type errInvalidParameter struct{ error }
  80. func (errInvalidParameter) InvalidParameter() {}
  81. func (e errInvalidParameter) Cause() error { return e.error }
  82. func (e errInvalidParameter) Unwrap() error { return e.error }