github.com/nikkelma/oras-project_oras-go@v1.1.1-0.20220201001104-a75f6a419090/pkg/auth/docker/login.go (about)

     1  /*
     2  Copyright The ORAS Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package docker
    17  
    18  import (
    19  	"context"
    20  
    21  	ctypes "github.com/docker/cli/cli/config/types"
    22  	"github.com/docker/docker/api/types"
    23  	"github.com/docker/docker/registry"
    24  
    25  	iface "oras.land/oras-go/pkg/auth"
    26  )
    27  
    28  // Login logs in to a docker registry identified by the hostname.
    29  // Deprecated: use LoginWithOpts
    30  func (c *Client) Login(ctx context.Context, hostname, username, secret string, insecure bool) error {
    31  	settings := &iface.LoginSettings{
    32  		Context:  ctx,
    33  		Hostname: hostname,
    34  		Username: username,
    35  		Secret:   secret,
    36  		Insecure: insecure,
    37  	}
    38  	return c.login(settings)
    39  }
    40  
    41  // LoginWithOpts logs in to a docker registry identified by the hostname with custom options.
    42  func (c *Client) LoginWithOpts(options ...iface.LoginOption) error {
    43  	settings := &iface.LoginSettings{}
    44  	for _, option := range options {
    45  		option(settings)
    46  	}
    47  	return c.login(settings)
    48  }
    49  
    50  func (c *Client) login(settings *iface.LoginSettings) error {
    51  	hostname := resolveHostname(settings.Hostname)
    52  	cred := types.AuthConfig{
    53  		Username:      settings.Username,
    54  		ServerAddress: hostname,
    55  	}
    56  	if settings.Username == "" {
    57  		cred.IdentityToken = settings.Secret
    58  	} else {
    59  		cred.Password = settings.Secret
    60  	}
    61  
    62  	opts := registry.ServiceOptions{}
    63  
    64  	if settings.Insecure {
    65  		opts.InsecureRegistries = []string{hostname}
    66  	}
    67  
    68  	// Login to ensure valid credential
    69  	remote, err := registry.NewService(opts)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	ctx := settings.Context
    74  	if ctx == nil {
    75  		ctx = context.Background()
    76  	}
    77  	userAgent := settings.UserAgent
    78  	if userAgent == "" {
    79  		userAgent = "oras"
    80  	}
    81  	if _, token, err := remote.Auth(ctx, &cred, userAgent); err != nil {
    82  		return err
    83  	} else if token != "" {
    84  		cred.Username = ""
    85  		cred.Password = ""
    86  		cred.IdentityToken = token
    87  	}
    88  
    89  	// Store credential
    90  	return c.primaryCredentialsStore(hostname).Store(ctypes.AuthConfig(cred))
    91  }