microsoft/healthcare-shared-components
Publicmirrored from https://github.com/microsoft/healthcare-shared-componentsAvailable
src/Microsoft.Health.Client/Authentication/AuthenticationHttpMessageHandler.cs
36lines · modecode
| 1 | // ------------------------------------------------------------------------------------------------- |
| 2 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. |
| 4 | // ------------------------------------------------------------------------------------------------- |
| 5 | |
| 6 | using System.Net.Http; |
| 7 | using System.Net.Http.Headers; |
| 8 | using System.Threading; |
| 9 | using System.Threading.Tasks; |
| 10 | using EnsureThat; |
| 11 | |
| 12 | namespace Microsoft.Health.Client.Authentication; |
| 13 | |
| 14 | public class AuthenticationHttpMessageHandler : DelegatingHandler |
| 15 | { |
| 16 | private readonly ICredentialProvider _credentialProvider; |
| 17 | |
| 18 | public AuthenticationHttpMessageHandler(ICredentialProvider credentialProvider) |
| 19 | { |
| 20 | EnsureArg.IsNotNull(credentialProvider, nameof(credentialProvider)); |
| 21 | |
| 22 | _credentialProvider = credentialProvider; |
| 23 | } |
| 24 | |
| 25 | protected override async Task<HttpResponseMessage> SendAsync( |
| 26 | HttpRequestMessage request, |
| 27 | CancellationToken cancellationToken) |
| 28 | { |
| 29 | EnsureArg.IsNotNull(request, nameof(request)); |
| 30 | |
| 31 | request.Headers.Authorization = |
| 32 | new AuthenticationHeaderValue("Bearer", await _credentialProvider.GetBearerTokenAsync(cancellationToken).ConfigureAwait(false)); |
| 33 | |
| 34 | return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| 35 | } |
| 36 | } |
| 37 | |