openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[REQ] csharp generators - Enable override of SSL error handling

Open coloboxp opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? Please describe.

If your dev servers have locally generated certificates, SSL validation would fail. It is possible to fix by modifying the ApiClient.cs and overriding its handling.

Describe the solution you'd like

Add the possibility to override the handling for the SSL errors

Describe alternatives you've considered

Using a public virtual method to do so; I've edited the mustache file to do it on my end, as the client is generated often and editing it each time is painful.

Additional context

This is what I ended up doing for now, better alternatives are welcome :)

 var clientOptions = new RestClientOptions(baseUrl)
            {
                ClientCertificates = configuration.ClientCertificates,
                CookieContainer = cookies,
                MaxTimeout = configuration.Timeout,
                Proxy = configuration.Proxy,
                UserAgent = configuration.UserAgent,
                RemoteCertificateValidationCallback = RemoteCertificateValidationCallback
            };
        /// <summary>
        /// Verifies the remote Secure Sockets Layer (SSL) certificate used for authentication.
        /// </summary>
        /// <param name="sender">An object that contains state information for this validation.</param>
        /// <param name="certificate">The certificate used to authenticate the remote party.</param>
        /// <param name="chain">The chain of certificate authorities associated with the remote certificate.</param>
        /// <param name="sslpolicyerrors">One or more errors associated with the remote certificate.</param>
        /// <returns>A Boolean value that determines whether the specified certificate is accepted for authentication.</returns>
        public virtual bool RemoteCertificateValidationCallback(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslpolicyerrors)
        {
            return sslpolicyerrors == SslPolicyErrors.None;
        }

Then on my inherited ApiClient, I can do :

    public class LocalApiClient : ApiClient
    {
        public override bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslpolicyerrors)
        {
#if DEBUG
            return ignoreSslErrors || sslpolicyerrors == SslPolicyErrors.None;
#else
            return sslpolicyerrors == SslPolicyErrors.None;
#endif
        }
    }

Or any other more advanced SSL/certificate validation if needed.

coloboxp avatar Feb 03 '23 09:02 coloboxp

I think this is a duplicate of https://github.com/OpenAPITools/openapi-generator/issues/14608 It would be great to see way of setting the RemoteCertificateValidationCallback through IReadableConfiguration. I'll try to submit a pull request..

dlange-hima avatar Jul 03 '23 10:07 dlange-hima

I use openapi generator of 7.0.1 version, RemoteCertificateValidationCallback work only for synchronous method and synchronous httpClient, but not work for asynchronous httpClient, because RestClientOptions for asynchronous client not set RemoteCertificateValidationCallback in private async Task<ApiResponse<T>> ExecAsync<T>(...) method of ApiClient.cs

var clientOptions = new RestClientOptions(baseUrl)
            {
                ClientCertificates = configuration.ClientCertificates,
                MaxTimeout = configuration.Timeout,
                Proxy = configuration.Proxy,
                UserAgent = configuration.UserAgent,
                UseDefaultCredentials = configuration.UseDefaultCredentials
            };

kertak90 avatar Oct 18 '23 10:10 kertak90