| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.Batch.Auth |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Azure Active Directory token credentials for an Azure Batch account. |
| | 11 | | /// </summary> |
| | 12 | | public class BatchTokenCredentials : BatchCredentials |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Gets a function which returns an authentication token. |
| | 16 | | /// </summary> |
| 0 | 17 | | public Func<Task<string>> TokenProvider { get; } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Initializes a new instance of the <see cref="BatchTokenCredentials"/> class with the specified Batch service |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="baseUrl">The Batch service endpoint.</param> |
| | 23 | | /// <param name="token">An authentication token provided by Azure Active Directory.</param> |
| 0 | 24 | | public BatchTokenCredentials(string baseUrl, string token) |
| | 25 | | { |
| 0 | 26 | | if (string.IsNullOrEmpty(baseUrl)) |
| | 27 | | { |
| 0 | 28 | | throw new ArgumentNullException(nameof(baseUrl)); |
| | 29 | | } |
| 0 | 30 | | if (string.IsNullOrEmpty(token)) |
| | 31 | | { |
| 0 | 32 | | throw new ArgumentNullException(nameof(token)); |
| | 33 | | } |
| | 34 | |
|
| 0 | 35 | | this.BaseUrl = baseUrl; |
| 0 | 36 | | var tokenTask = Task.FromResult(token); |
| 0 | 37 | | TokenProvider = () => tokenTask; |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Initializes a new instance of the <see cref="BatchTokenCredentials" /> class with the specified Batch servic |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="baseUrl">The Batch service endpoint.</param> |
| | 44 | | /// <param name="tokenProvider">A function which returns an authentication token.</param> |
| 0 | 45 | | public BatchTokenCredentials(string baseUrl, Func<Task<string>> tokenProvider) |
| | 46 | | { |
| 0 | 47 | | if (string.IsNullOrEmpty(baseUrl)) |
| | 48 | | { |
| 0 | 49 | | throw new ArgumentNullException(nameof(baseUrl)); |
| | 50 | | } |
| 0 | 51 | | if (tokenProvider == null) |
| | 52 | | { |
| 0 | 53 | | throw new ArgumentNullException(nameof(tokenProvider)); |
| | 54 | | } |
| 0 | 55 | | this.BaseUrl = baseUrl; |
| 0 | 56 | | this.TokenProvider = tokenProvider; |
| 0 | 57 | | } |
| | 58 | | } |
| | 59 | | } |