@@ -3,8 +3,6 @@ package command
33import (
44 "bufio"
55 "context"
6- "encoding/base64"
7- "encoding/json"
86 "fmt"
97 "io"
108 "os"
@@ -21,13 +19,9 @@ import (
2119 "github.com/pkg/errors"
2220)
2321
24- // EncodeAuthToBase64 serializes the auth configuration as JSON base64 payload
22+ // EncodeAuthToBase64 serializes the auth configuration as JSON base64 payload.
2523func EncodeAuthToBase64 (authConfig registrytypes.AuthConfig ) (string , error ) {
26- buf , err := json .Marshal (authConfig )
27- if err != nil {
28- return "" , err
29- }
30- return base64 .URLEncoding .EncodeToString (buf ), nil
24+ return registrytypes .EncodeAuthConfig (authConfig )
3125}
3226
3327// RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
@@ -45,7 +39,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
4539 if err != nil {
4640 return "" , err
4741 }
48- return EncodeAuthToBase64 (authConfig )
42+ return registrytypes . EncodeAuthConfig (authConfig )
4943 }
5044}
5145
@@ -177,14 +171,19 @@ func promptWithDefault(out io.Writer, prompt string, configDefault string) {
177171 }
178172}
179173
180- // RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete image
174+ // RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete
175+ // image. The auth configuration is serialized as a base64url encoded RFC4648,
176+ // section 5) JSON string for sending through the X-Registry-Auth header.
177+ //
178+ // For details on base64url encoding, see:
179+ // - RFC4648, section 5: https://tools.ietf.org/html/rfc4648#section-5
181180func RetrieveAuthTokenFromImage (ctx context.Context , cli Cli , image string ) (string , error ) {
182181 // Retrieve encoded auth token from the image reference
183182 authConfig , err := resolveAuthConfigFromImage (ctx , cli , image )
184183 if err != nil {
185184 return "" , err
186185 }
187- encodedAuth , err := EncodeAuthToBase64 (authConfig )
186+ encodedAuth , err := registrytypes . EncodeAuthConfig (authConfig )
188187 if err != nil {
189188 return "" , err
190189 }
0 commit comments