dmariogatto / certificate-transparency

Certificate Transparency ported to C# for .NET
MIT License
6 stars 2 forks source link

Support custom HttpClient (for proxy auth) #11

Closed amather closed 1 year ago

amather commented 1 year ago

Hello!

The library fails to work in an environment where internet access is only available via an authenticated proxy. While HttpClient does detect the system proxy it does not detect the required authentication credentials.

For example, that's the HttpClient I use in my app:

    var systemProxy = HttpClient.DefaultProxy.GetProxy(new(MyAppClient.BaseUrl)) ;
    if(systemProxy != null && _httpClientHandler.Proxy == null && _httpClientHandler.SupportsProxy)
    {
        _httpClientHandler.UseProxy = true;
        _httpClientHandler.Proxy = HttpClient.DefaultProxy;
        _httpClientHandler.DefaultProxyCredentials = CredentialCache.DefaultNetworkCredentials;
    }
    _httpClient = new(_httpClientHandler);

I don't need any proxy auto-configuration from this library, but I'd be great if at least GoogleLogListApi could support another constructor to which I could supply my own HttpClient. Then I could create my own Instance class and continue using that. Would that be a change you're willing to implement?

Thanks!

dmariogatto commented 1 year ago

Would something like this suffice... GoogleLogListApi HttpClient Ctor? Note, that you will have to correctly set the base address of the HttpClient that you pass through.

Otherwise if you wanted to do something more complex, like support a different log list provider, you could create a custom implementation of ILogListApi.

amather commented 1 year ago

Hi! I just discovered that it is indeed possible to configure the auth from the outside, so that no changes to this project are required.

While the DefaultProxy itself is read-only, its .Credentials property can be set. This way, I can configure the DefaultProxy with credentials before the HttpClient instance within GoogleLogListApi is created.

My code now looks like this and works fine with this library:

    var systemProxy = HttpClient.DefaultProxy.GetProxy(new(GeorgeClient.BaseUrl)) ;
    if(systemProxy != null && _httpClientHandler.Proxy == null && _httpClientHandler.SupportsProxy)
    {
        HttpClient.DefaultProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
    }
    _httpClient = new(_httpClientHandler);

Thanks for your efforts though!