mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
api: add registry.EncodeAuthConfig
Based on EncodeAuthToBase64 in docker/cli;
1f4111d2bf/cli/command/registry.go (L30-L37)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
7819811835
commit
7ca66e3cfc
2 changed files with 19 additions and 0 deletions
|
@ -33,6 +33,19 @@ type AuthConfig struct {
|
|||
RegistryToken string `json:"registrytoken,omitempty"`
|
||||
}
|
||||
|
||||
// EncodeAuthConfig serializes the auth configuration as a base64url encoded
|
||||
// RFC4648, section 5) JSON string for sending through the X-Registry-Auth header.
|
||||
//
|
||||
// For details on base64url encoding, see:
|
||||
// - RFC4648, section 5: https://tools.ietf.org/html/rfc4648#section-5
|
||||
func EncodeAuthConfig(authConfig AuthConfig) (string, error) {
|
||||
buf, err := json.Marshal(authConfig)
|
||||
if err != nil {
|
||||
return "", errInvalidParameter{err}
|
||||
}
|
||||
return base64.URLEncoding.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
// DecodeAuthConfig decodes base64url encoded (RFC4648, section 5) JSON
|
||||
// authentication information as sent through the X-Registry-Auth header.
|
||||
//
|
||||
|
|
|
@ -51,3 +51,9 @@ func TestDecodeAuthConfigBody(t *testing.T) {
|
|||
assert.NilError(t, err)
|
||||
assert.Equal(t, *token, expected)
|
||||
}
|
||||
|
||||
func TestEncodeAuthConfig(t *testing.T) {
|
||||
token, err := EncodeAuthConfig(expected)
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, token, encoded)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue