From 9d657908fd75b18070a44ebb333e287a00da5fc1 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Tue, 23 May 2017 13:23:46 -0400 Subject: [PATCH] RegistryAuth: add method for building from auth token --- .../docker/client/messages/RegistryAuth.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/spotify/docker/client/messages/RegistryAuth.java b/src/main/java/com/spotify/docker/client/messages/RegistryAuth.java index 5fbabc014..c9102d680 100644 --- a/src/main/java/com/spotify/docker/client/messages/RegistryAuth.java +++ b/src/main/java/com/spotify/docker/client/messages/RegistryAuth.java @@ -144,15 +144,13 @@ public static RegistryAuth create(@JsonProperty("username") String username, @JsonProperty("identityToken") final String identityToken, @JsonProperty("auth") final String auth) { + final Builder builder; if (auth != null) { - final String[] authParams = Base64.decodeAsString(auth).split(":"); - - if (authParams.length == 2) { - username = authParams[0].trim(); - password = authParams[1].trim(); - } + builder = forAuthToken(auth); + } else { + builder = builder(); } - return builder() + return builder .username(username) .password(password) .email(email) @@ -161,6 +159,21 @@ public static RegistryAuth create(@JsonProperty("username") String username, .build(); } + /** Construct a Builder based upon the "auth" field of the docker client config file. */ + public static Builder forAuthToken(String auth) { + final String[] authParams = Base64.decodeAsString(auth).split(":"); + + if (authParams.length != 2) { + throw new IllegalArgumentException( + "auth token does not appear to be properly formatted after base64 decoding, " + + "did not find two values separated by a colon: " + + auth); + } + return builder() + .username(authParams[0].trim()) + .password(authParams[1].trim()); + } + public static Builder builder() { return new AutoValue_RegistryAuth.Builder(); }