|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Net.Http.Headers; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace System.Net.Http |
| 11 | +{ |
| 12 | + internal sealed partial class AuthenticationHandler : HttpMessageHandler |
| 13 | + { |
| 14 | + private readonly HttpMessageHandler _innerHandler; |
| 15 | + private readonly bool _preAuthenticate; |
| 16 | + private readonly ICredentials _credentials; |
| 17 | + |
| 18 | + public AuthenticationHandler(bool preAuthenticate, ICredentials credentials, HttpMessageHandler innerHandler) |
| 19 | + { |
| 20 | + Debug.Assert(innerHandler != null); |
| 21 | + Debug.Assert(credentials != null); |
| 22 | + |
| 23 | + _preAuthenticate = preAuthenticate; |
| 24 | + _credentials = credentials; |
| 25 | + _innerHandler = innerHandler; |
| 26 | + } |
| 27 | + |
| 28 | + protected internal override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 29 | + { |
| 30 | + if (_preAuthenticate) |
| 31 | + { |
| 32 | + AuthenticationHelper.TrySetBasicAuthToken(request, _credentials); |
| 33 | + } |
| 34 | + |
| 35 | + HttpResponseMessage response = await _innerHandler.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| 36 | + |
| 37 | + if (response.StatusCode == HttpStatusCode.Unauthorized) |
| 38 | + { |
| 39 | + AuthenticationHeaderValue selectedAuth = GetSupportedAuthScheme(response.Headers.WwwAuthenticate); |
| 40 | + if (selectedAuth != null) |
| 41 | + { |
| 42 | + switch (selectedAuth.Scheme) |
| 43 | + { |
| 44 | + case AuthenticationHelper.Digest: |
| 45 | + // Update digest response with new parameter from WWWAuthenticate |
| 46 | + var digestResponse = new AuthenticationHelper.DigestResponse(selectedAuth.Parameter); |
| 47 | + if (await AuthenticationHelper.TrySetDigestAuthToken(request, _credentials, digestResponse, HttpKnownHeaderNames.Authorization).ConfigureAwait(false)) |
| 48 | + { |
| 49 | + response.Dispose(); |
| 50 | + response = await _innerHandler.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| 51 | + |
| 52 | + // Retry in case of nonce timeout in server. |
| 53 | + if (response.StatusCode == HttpStatusCode.Unauthorized) |
| 54 | + { |
| 55 | + foreach (AuthenticationHeaderValue ahv in response.Headers.WwwAuthenticate) |
| 56 | + { |
| 57 | + if (ahv.Scheme == AuthenticationHelper.Digest) |
| 58 | + { |
| 59 | + digestResponse = new AuthenticationHelper.DigestResponse(ahv.Parameter); |
| 60 | + if (AuthenticationHelper.IsServerNonceStale(digestResponse) && |
| 61 | + await AuthenticationHelper.TrySetDigestAuthToken(request, _credentials, digestResponse, HttpKnownHeaderNames.Authorization).ConfigureAwait(false)) |
| 62 | + { |
| 63 | + response.Dispose(); |
| 64 | + response = await _innerHandler.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| 65 | + } |
| 66 | + |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + break; |
| 73 | + |
| 74 | + case AuthenticationHelper.Basic: |
| 75 | + if (_preAuthenticate) |
| 76 | + { |
| 77 | + // We already tried these credentials via preauthentication, so no need to try again |
| 78 | + break; |
| 79 | + } |
| 80 | + |
| 81 | + if (AuthenticationHelper.TrySetBasicAuthToken(request, _credentials)) |
| 82 | + { |
| 83 | + response.Dispose(); |
| 84 | + response = await _innerHandler.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| 85 | + } |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return response; |
| 92 | + } |
| 93 | + |
| 94 | + private static AuthenticationHeaderValue GetSupportedAuthScheme(HttpHeaderValueCollection<AuthenticationHeaderValue> authenticateValues) |
| 95 | + { |
| 96 | + AuthenticationHeaderValue basicAuthenticationHeaderValue = null; |
| 97 | + |
| 98 | + // Only Digest and Basic auth supported, ignore others. |
| 99 | + foreach (AuthenticationHeaderValue ahv in authenticateValues) |
| 100 | + { |
| 101 | + if (ahv.Scheme == AuthenticationHelper.Digest) |
| 102 | + { |
| 103 | + return ahv; |
| 104 | + } |
| 105 | + else if (ahv.Scheme == AuthenticationHelper.Basic) |
| 106 | + { |
| 107 | + basicAuthenticationHeaderValue = ahv; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return basicAuthenticationHeaderValue; |
| 112 | + } |
| 113 | + |
| 114 | + protected override void Dispose(bool disposing) |
| 115 | + { |
| 116 | + if (disposing) |
| 117 | + { |
| 118 | + _innerHandler.Dispose(); |
| 119 | + } |
| 120 | + |
| 121 | + base.Dispose(disposing); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
0 commit comments