github.com/xiaobinqt/libcompose@v1.1.0/docker/auth/auth.go (about)

     1  package auth
     2  
     3  import (
     4  	"github.com/docker/cli/cli/config/configfile"
     5  	clitypes "github.com/docker/cli/cli/config/types"
     6  	"github.com/docker/docker/api/types"
     7  	"github.com/docker/docker/registry"
     8  )
     9  
    10  // Lookup defines a method for looking up authentication information
    11  type Lookup interface {
    12  	All() map[string]types.AuthConfig
    13  	Lookup(repoInfo *registry.RepositoryInfo) types.AuthConfig
    14  }
    15  
    16  // ConfigLookup implements AuthLookup by reading a Docker config file
    17  type ConfigLookup struct {
    18  	*configfile.ConfigFile
    19  }
    20  
    21  // NewConfigLookup creates a new ConfigLookup for a given context
    22  func NewConfigLookup(configfile *configfile.ConfigFile) *ConfigLookup {
    23  	return &ConfigLookup{
    24  		ConfigFile: configfile,
    25  	}
    26  }
    27  
    28  // Lookup uses a Docker config file to lookup authentication information
    29  func (c *ConfigLookup) Lookup(repoInfo *registry.RepositoryInfo) types.AuthConfig {
    30  	if c.ConfigFile == nil || repoInfo == nil || repoInfo.Index == nil {
    31  		return types.AuthConfig{}
    32  	}
    33  	return registry.ResolveAuthConfig(convert(c.ConfigFile.AuthConfigs), repoInfo.Index)
    34  }
    35  
    36  // All uses a Docker config file to get all authentication information
    37  func (c *ConfigLookup) All() map[string]types.AuthConfig {
    38  	if c.ConfigFile == nil {
    39  		return map[string]types.AuthConfig{}
    40  	}
    41  	return convert(c.ConfigFile.AuthConfigs)
    42  }
    43  
    44  func convert(acs map[string]clitypes.AuthConfig) map[string]types.AuthConfig {
    45  	if acs == nil {
    46  		return nil
    47  	}
    48  
    49  	result := map[string]types.AuthConfig{}
    50  	for k, v := range acs {
    51  		result[k] = types.AuthConfig{
    52  			Username:      v.Username,
    53  			Password:      v.Password,
    54  			Auth:          v.Auth,
    55  			Email:         v.Email,
    56  			ServerAddress: v.ServerAddress,
    57  			IdentityToken: v.IdentityToken,
    58  			RegistryToken: v.RegistryToken,
    59  		}
    60  	}
    61  	return result
    62  }