Skip to content

Commit bc4636e

Browse files
feat: Allow using default credentials for proxy (box/box-codegen#623) (#334)
Fixes: #333
1 parent 61484ec commit bc4636e

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "f073ce3", "specHash": "544d370", "version": "1.4.0" }
1+
{ "engineHash": "a839036", "specHash": "544d370", "version": "1.4.0" }

Box.Sdk.Gen/Networking/Fetch.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,24 @@ private static HttpClient GetOrCreateHttpClient(NetworkSession networkSession)
170170

171171
private static HttpClient CreateProxyClient(ProxyConfig proxyConfig)
172172
{
173+
var webProxy = new WebProxy(proxyConfig.Url)
174+
{
175+
UseDefaultCredentials = proxyConfig.UseDefaultCredentials
176+
};
177+
178+
if (!proxyConfig.UseDefaultCredentials)
179+
{
180+
webProxy.Credentials = new NetworkCredential(
181+
proxyConfig.Username,
182+
proxyConfig.Password,
183+
proxyConfig.Domain);
184+
}
185+
173186
var handler = new HttpClientHandler
174187
{
175-
Proxy = new WebProxy(proxyConfig.Url)
176-
{
177-
Credentials = new NetworkCredential(proxyConfig.Username, proxyConfig.Password, proxyConfig.Domain)
178-
},
188+
Proxy = webProxy,
179189
UseProxy = true,
180-
PreAuthenticate = true,
181-
UseDefaultCredentials = false
190+
PreAuthenticate = true
182191
};
183192

184193
return new HttpClient(handler, disposeHandler: true);

Box.Sdk.Gen/Networking/ProxyConfig/ProxyConfig.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ public class ProxyConfig {
1010

1111
public string? Domain { get; init; }
1212

13-
public ProxyConfig(string url) {
13+
public bool UseDefaultCredentials { get; }
14+
15+
public ProxyConfig(string url, bool useDefaultCredentials = false) {
1416
Url = url;
17+
UseDefaultCredentials = useDefaultCredentials;
1518
}
1619
}
1720
}

docs/Client.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,17 @@ var newClient = client.WithCustomBaseUrls(new BaseUrls(
131131

132132
# Use Proxy for API calls
133133

134-
In order to use a proxy for API calls, calling the `client.WithProxy(proxyConfig)` method creates a new client, leaving the original client unmodified, with the username and password being optional.
134+
In order to use a proxy for API calls, call the `client.WithProxy(proxyConfig)` method that creates a new client with proxy, leaving the original client unmodified.
135+
In config, you can specify the username, password and domain for the proxy server.
136+
Alternatively you can set 'UseDefaultCredentials' to true to use the credentials of the currently logged on user - `DefaultCredentials`.
137+
NOTE: Setting UseDefaultCredentials takes precedence over Username, Password and Domain fields. If UseDefaultCredentials is set to true, the Username, Password and Domain fields will be ignored.
135138

136139
```c#
137140
var newClient = client.WithProxy(new ProxyConfig("http://proxy.com") { Username = "username", Password = "password", Domain = "example" });
138141
```
142+
143+
To use proxy with default credentials:
144+
145+
```c#
146+
var newClient = client.WithProxy(new ProxyConfig("http://proxy.com") { UseDefaultCredentials = true });
147+
```

0 commit comments

Comments
 (0)