public void TestRegistryAuthenticationFailedException_message()
        {
            RegistryAuthenticationFailedException exception =
                new RegistryAuthenticationFailedException("serverUrl", "imageName", "message");

            Assert.AreEqual("serverUrl", exception.GetRegistry());
            Assert.AreEqual("imageName", exception.GetImageName());
            Assert.AreEqual(
                "Failed to authenticate with registry serverUrl/imageName because: message",
                exception.Message);
        }
Example #2
0
        /**
         * Sends the authentication request and retrieves the Bearer authorization token.
         *
         * @param credential the credential used to authenticate
         * @param scope the scope of permissions to authenticate for
         * @return the {@link Authorization} response
         * @throws RegistryAuthenticationFailedException if authentication fails
         * @see <a
         *     href="https://docs.docker.com/registry/spec/auth/token/#how-to-authenticate">https://docs.docker.com/registry/spec/auth/token/#how-to-authenticate</a>
         */
        private async Task <Authorization> AuthenticateAsync(Credential credential, string scope, IEventHandlers eventHandlers)
        {
            try
            {
                using (Connection connection =
                           Connection.GetConnectionFactory(eventHandlers)(GetAuthenticationUrl(credential, scope)))
                    using (var request = new HttpRequestMessage())
                    {
                        foreach (var value in userAgent)
                        {
                            request.Headers.UserAgent.Add(value);
                        }

                        if (IsOAuth2Auth(credential))
                        {
                            string parameters = GetAuthRequestParameters(credential, scope);
                            request.Content = new BlobHttpContent(Blobs.From(parameters), MediaType.FormData);
                        }
                        else if (credential != null)
                        {
                            Authorization authorization = Authorization.FromBasicCredentials(credential.GetUsername(), credential.GetPassword());
                            request.Headers.Authorization = new AuthenticationHeaderValue(authorization.GetScheme(), authorization.GetToken());
                        }
                        if (IsOAuth2Auth(credential))
                        {
                            request.Method = HttpMethod.Post;
                        }
                        else
                        {
                            request.Method = HttpMethod.Get;
                        }

                        string responseString;
                        using (HttpResponseMessage response = await connection.SendAsync(request).ConfigureAwait(false))
                            using (StreamReader reader = new StreamReader(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), Encoding.UTF8))
                            {
                                responseString = await reader.ReadToEndAsync().ConfigureAwait(false);
                            }

                        AuthenticationResponseTemplate responseJson =
                            JsonTemplateMapper.ReadJson <AuthenticationResponseTemplate>(responseString);

                        if (responseJson.GetTokenOrAccessToken() == null)
                        {
                            var err = new RegistryAuthenticationFailedException(
                                registryEndpointRequestProperties.GetRegistry(),
                                registryEndpointRequestProperties.GetImageName(),
                                "Did not get token in authentication response from "
                                + GetAuthenticationUrl(credential, scope)
                                + "; parameters: "
                                + GetAuthRequestParameters(credential, scope));
                            eventHandlers?.Dispatch(LogEvent.Error(err.Message));
                            throw err;
                        }
                        return(Authorization.FromBearerToken(responseJson.GetTokenOrAccessToken()));
                    }
            }
            catch (Exception ex) when(ex is IOException || ex is JsonException)
            {
                var eee = new RegistryAuthenticationFailedException(
                    registryEndpointRequestProperties.GetRegistry(),
                    registryEndpointRequestProperties.GetImageName(),
                    ex);

                eventHandlers?.Dispatch(LogEvent.Error(eee.Message));
                throw eee;
            }
        }