|
| 1 | +// ------------------------------------------------------------------------------ |
| 2 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. |
| 3 | +// ------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +namespace Microsoft.Graph |
| 6 | +{ |
| 7 | + using System.Net.Http; |
| 8 | + using System.Threading; |
| 9 | + using System.Threading.Tasks; |
| 10 | + using System.Net; |
| 11 | + /// <summary> |
| 12 | + /// A <see cref="DelegatingHandler"/> implementation using standard .NET libraries. |
| 13 | + /// </summary> |
| 14 | + public class AuthenticationHandler: DelegatingHandler |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// MaxRetry property for 401's |
| 18 | + /// </summary> |
| 19 | + public int MaxRetry { get; set; } = 1; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// AuthenticationProvider property |
| 23 | + /// </summary> |
| 24 | + public IAuthenticationProvider AuthenticationProvider { get; set; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Construct a new <see cref="AuthenticationHandler"/> |
| 28 | + /// </summary> |
| 29 | + public AuthenticationHandler() |
| 30 | + { |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Construct a new <see cref="AuthenticationHandler"/> |
| 36 | + /// <param name="authenticationProvider">An authentication provider to pass to <see cref="AuthenticationHandler"/> for authenticating requests.</param> |
| 37 | + /// </summary> |
| 38 | + public AuthenticationHandler(IAuthenticationProvider authenticationProvider) |
| 39 | + { |
| 40 | + AuthenticationProvider = authenticationProvider; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Construct a new <see cref="AuthenticationHandler"/> |
| 45 | + /// </summary> |
| 46 | + /// <param name="authenticationProvider">An authentication provider to pass to <see cref="AuthenticationHandler"/> for authenticating requests.</param> |
| 47 | + /// <param name="innerHandler">A HTTP message handler to pass to the <see cref="AuthenticationHandler"/> for sending requests.</param> |
| 48 | + public AuthenticationHandler(IAuthenticationProvider authenticationProvider, HttpMessageHandler innerHandler) |
| 49 | + { |
| 50 | + InnerHandler = innerHandler; |
| 51 | + AuthenticationProvider = authenticationProvider; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Checks HTTP response message status code if it's unauthorized (401) or not |
| 56 | + /// </summary> |
| 57 | + /// <param name="httpResponseMessage">The <see cref="HttpResponseMessage"/>to send.</param> |
| 58 | + /// <returns></returns> |
| 59 | + private bool IsUnauthorized(HttpResponseMessage httpResponseMessage) |
| 60 | + { |
| 61 | + return httpResponseMessage.StatusCode == HttpStatusCode.Unauthorized; |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Retry sending HTTP request |
| 66 | + /// </summary> |
| 67 | + /// <param name="httpResponseMessage">The <see cref="HttpResponseMessage"/>to send.</param> |
| 68 | + /// <param name="cancellationToken">The <see cref="CancellationToken"/>to send.</param> |
| 69 | + /// <returns></returns> |
| 70 | + private async Task<HttpResponseMessage> SendRetryAsync(HttpResponseMessage httpResponseMessage, CancellationToken cancellationToken) |
| 71 | + { |
| 72 | + int retryAttempt = 0; |
| 73 | + while (retryAttempt < MaxRetry) |
| 74 | + { |
| 75 | + var originalRequest = httpResponseMessage.RequestMessage; |
| 76 | + |
| 77 | + // Authenticate request using AuthenticationProvider |
| 78 | + await AuthenticationProvider.AuthenticateRequestAsync(originalRequest); |
| 79 | + httpResponseMessage = await base.SendAsync(originalRequest, cancellationToken); |
| 80 | + |
| 81 | + retryAttempt++; |
| 82 | + |
| 83 | + if (!IsUnauthorized(httpResponseMessage) || !originalRequest.IsBuffered()) |
| 84 | + { |
| 85 | + // Re-issue the request to get a new access token |
| 86 | + return httpResponseMessage; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return httpResponseMessage; |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Sends a HTTP request and retries the request when the response is unauthorized. |
| 95 | + /// This can happen when a token from the cache expires between graph getting the request and the backend receiving the request |
| 96 | + /// </summary> |
| 97 | + /// <param name="httpRequestMessage">The <see cref="HttpRequestMessage"/> to send.</param> |
| 98 | + /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> |
| 99 | + /// <returns></returns> |
| 100 | + protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage httpRequestMessage, CancellationToken cancellationToken) |
| 101 | + { |
| 102 | + // Authenticate request using AuthenticationProvider |
| 103 | + if (AuthenticationProvider != null) |
| 104 | + { |
| 105 | + await AuthenticationProvider.AuthenticateRequestAsync(httpRequestMessage); |
| 106 | + } |
| 107 | + |
| 108 | + HttpResponseMessage response = await base.SendAsync(httpRequestMessage, cancellationToken); |
| 109 | + |
| 110 | + // Chcek if response is a 401 & is not a streamed body (is buffered) |
| 111 | + if (IsUnauthorized(response) && httpRequestMessage.IsBuffered() && (AuthenticationProvider != null)) |
| 112 | + { |
| 113 | + // re-issue the request to get a new access token |
| 114 | + response = await SendRetryAsync(response, cancellationToken); |
| 115 | + } |
| 116 | + |
| 117 | + return response; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments