microsoft/healthcare-shared-components
Publicmirrored from https://github.com/microsoft/healthcare-shared-componentsAvailable
src/Microsoft.Health.Client/AuthenticationHttpMessageHandler.cs
35lines · 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 |
| 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 | request.Headers.Authorization = |
| 30 | new AuthenticationHeaderValue("Bearer", await _credentialProvider.GetBearerToken(cancellationToken)); |
| 31 | |
| 32 | return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |