| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | // Copied from https://raw.githubusercontent.com/dotnet/corefx/master/src/System.Net.Http/src/System/Net/Http/SocketsHtt |
| | 5 | |
|
| | 6 | | #nullable disable |
| | 7 | |
|
| | 8 | | using System; |
| | 9 | | using System.Net; |
| | 10 | |
|
| | 11 | | namespace Azure.Core.Pipeline |
| | 12 | | { |
| | 13 | | internal sealed class HttpEnvironmentProxyCredentials : ICredentials |
| | 14 | | { |
| | 15 | | // Wrapper class for cases when http and https have different authentication. |
| | 16 | | private readonly NetworkCredential _httpCred; |
| | 17 | | private readonly NetworkCredential _httpsCred; |
| | 18 | | private readonly Uri _httpProxy; |
| | 19 | | private readonly Uri _httpsProxy; |
| | 20 | |
|
| 26 | 21 | | public HttpEnvironmentProxyCredentials(Uri httpProxy, NetworkCredential httpCred, |
| 26 | 22 | | Uri httpsProxy, NetworkCredential httpsCred) |
| | 23 | | { |
| 26 | 24 | | _httpCred = httpCred; |
| 26 | 25 | | _httpsCred = httpsCred; |
| 26 | 26 | | _httpProxy = httpProxy; |
| 26 | 27 | | _httpsProxy = httpsProxy; |
| 26 | 28 | | } |
| | 29 | |
|
| | 30 | | public NetworkCredential GetCredential(Uri uri, string authType) |
| | 31 | | { |
| 18 | 32 | | if (uri == null) |
| | 33 | | { |
| 2 | 34 | | return null; |
| | 35 | | } |
| 16 | 36 | | return uri.Equals(_httpProxy) ? _httpCred : |
| 16 | 37 | | uri.Equals(_httpsProxy) ? _httpsCred : null; |
| | 38 | | } |
| | 39 | |
|
| | 40 | | public static HttpEnvironmentProxyCredentials TryCreate(Uri httpProxy, Uri httpsProxy) |
| | 41 | | { |
| 58 | 42 | | NetworkCredential httpCred = null; |
| 58 | 43 | | NetworkCredential httpsCred = null; |
| | 44 | |
|
| 58 | 45 | | if (httpProxy != null) |
| | 46 | | { |
| 58 | 47 | | httpCred = GetCredentialsFromString(httpProxy.UserInfo); |
| | 48 | | } |
| 58 | 49 | | if (httpsProxy != null) |
| | 50 | | { |
| 46 | 51 | | httpsCred = GetCredentialsFromString(httpsProxy.UserInfo); |
| | 52 | | } |
| 58 | 53 | | if (httpCred == null && httpsCred == null) |
| | 54 | | { |
| 32 | 55 | | return null; |
| | 56 | | } |
| 26 | 57 | | return new HttpEnvironmentProxyCredentials(httpProxy, httpCred, httpsProxy, httpsCred); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Converts string containing user:password to NetworkCredential object. |
| | 62 | | /// </summary> |
| | 63 | | private static NetworkCredential GetCredentialsFromString(string value) |
| | 64 | | { |
| 104 | 65 | | if (string.IsNullOrWhiteSpace(value)) |
| | 66 | | { |
| 60 | 67 | | return null; |
| | 68 | | } |
| | 69 | |
|
| 44 | 70 | | value = Uri.UnescapeDataString(value); |
| | 71 | |
|
| 44 | 72 | | string password = ""; |
| 44 | 73 | | string domain = null; |
| 44 | 74 | | int idx = value.IndexOf(':'); |
| 44 | 75 | | if (idx != -1) |
| | 76 | | { |
| 34 | 77 | | password = value.Substring(idx + 1); |
| 34 | 78 | | value = value.Substring(0, idx); |
| | 79 | | } |
| | 80 | |
|
| 44 | 81 | | idx = value.IndexOf('\\'); |
| 44 | 82 | | if (idx != -1) |
| | 83 | | { |
| 8 | 84 | | domain = value.Substring(0, idx); |
| 8 | 85 | | value = value.Substring(idx + 1); |
| | 86 | | } |
| | 87 | |
|
| 44 | 88 | | return new NetworkCredential(value, password, domain); |
| | 89 | | } |
| | 90 | | } |
| | 91 | | } |