Auth contacts the public registry with the provided credentials, and returns OK if authentication was successful. It can be used to verify the validity of a client's credentials.
(ctx context.Context, authConfig *registry.AuthConfig, userAgent string)
| 33 | // and returns OK if authentication was successful. |
| 34 | // It can be used to verify the validity of a client's credentials. |
| 35 | func (s *Service) Auth(ctx context.Context, authConfig *registry.AuthConfig, userAgent string) (token string, _ error) { |
| 36 | registryHostName := IndexHostname |
| 37 | |
| 38 | if authConfig.ServerAddress != "" { |
| 39 | serverAddress := authConfig.ServerAddress |
| 40 | if !strings.HasPrefix(serverAddress, "https://") && !strings.HasPrefix(serverAddress, "http://") { |
| 41 | serverAddress = "https://" + serverAddress |
| 42 | } |
| 43 | u, err := url.Parse(serverAddress) |
| 44 | if err != nil { |
| 45 | return "", invalidParam(fmt.Errorf("unable to parse server address: %w", err)) |
| 46 | } |
| 47 | registryHostName = u.Host |
| 48 | } |
| 49 | |
| 50 | // Lookup endpoints for authentication. |
| 51 | endpoints, err := s.Endpoints(ctx, registryHostName) |
| 52 | if err != nil { |
| 53 | if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { |
| 54 | return "", err |
| 55 | } |
| 56 | return "", invalidParam(err) |
| 57 | } |
| 58 | |
| 59 | var lastErr error |
| 60 | for _, endpoint := range endpoints { |
| 61 | authToken, err := loginV2(ctx, authConfig, endpoint, userAgent) |
| 62 | if err != nil { |
| 63 | if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || errdefs.IsUnauthorized(err) { |
| 64 | // Failed to authenticate; don't continue with (non-TLS) endpoints. |
| 65 | return "", err |
| 66 | } |
| 67 | // Try next endpoint |
| 68 | log.G(ctx).WithFields(log.Fields{ |
| 69 | "error": err, |
| 70 | "endpoint": endpoint, |
| 71 | }).Infof("Error logging in to endpoint, trying next endpoint") |
| 72 | lastErr = err |
| 73 | continue |
| 74 | } |
| 75 | |
| 76 | return authToken, nil |
| 77 | } |
| 78 | |
| 79 | return "", lastErr |
| 80 | } |
| 81 | |
| 82 | // APIEndpoint represents a remote API endpoint |
| 83 | type APIEndpoint struct { |
no test coverage detected