All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.gesellix.docker.authentication.FileStore Maven / Gradle / Ivy

package de.gesellix.docker.authentication;

import java.util.Base64;
import java.util.Map;
import java.util.stream.Collectors;

public class FileStore implements CredsStore {

  private final Map config;
  private transient Map allAuthConfigs;

  public FileStore(Map config) {
    this.config = config.containsKey("auths") ? (Map) config.get("auths") : config;
  }

  @Override
  public AuthConfig getAuthConfig(String registry) {
    final AuthConfig authConfig = getAuthConfigs().get(registry);
    return authConfig != null ? authConfig : AuthConfig.EMPTY_AUTH_CONFIG;
  }

  @Override
  public Map getAuthConfigs() {
    if (allAuthConfigs == null) {
      allAuthConfigs = config.entrySet().stream()
          .filter((e) -> e.getValue() != null && e.getValue().get("auth") != null)
          .collect(Collectors.toMap(
              Map.Entry::getKey,
              e -> {
                String registry = e.getKey();
                Map value = e.getValue();
                String[] login = new String(Base64.getDecoder().decode((String) value.get("auth"))).split(":");
                String username = login[0];
                String password = login[1];

                AuthConfig authConfig = new AuthConfig();
                authConfig.setServeraddress(registry);
                // TODO?
                // if (username == TOKEN_USERNAME) { authConfig.setIdentitytoken(value.get("Secret")) }
                authConfig.setUsername(username);
                authConfig.setPassword(password);
                authConfig.setEmail((String) value.get("email"));
                return authConfig;
              }
          ));
    }
    return allAuthConfigs;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy