| | 1 | | // |
| | 2 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 3 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.Batch.Protocol |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.Net.Http; |
| | 9 | | using System.Net.Http.Headers; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | | using Rest; |
| | 13 | |
|
| | 14 | | public class BatchTokenProvider : ITokenProvider |
| | 15 | | { |
| | 16 | | private const string BearerAuthenticationScheme = "Bearer"; |
| | 17 | |
|
| 3 | 18 | | private Func<Task<string>> TokenProvider { get; } |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Initializes a new instance of the <see cref="BatchTokenProvider"/> class. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="tokenProvider">A token provider function which retrieves an AAD token.</param> |
| 2 | 24 | | public BatchTokenProvider(Func<Task<string>> tokenProvider) |
| | 25 | | { |
| 2 | 26 | | this.TokenProvider = tokenProvider; |
| 2 | 27 | | } |
| | 28 | |
|
| | 29 | | public async Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync(CancellationToken cancellationToken) |
| | 30 | | { |
| 3 | 31 | | string token = await this.TokenProvider().ConfigureAwait(false); |
| 3 | 32 | | return new AuthenticationHeaderValue(BearerAuthenticationScheme, token); |
| 3 | 33 | | } |
| | 34 | | } |
| | 35 | | } |