| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.Primitives |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Represents the Azure Active Directory token provider for the Service Bus. |
| | 11 | | /// </summary> |
| | 12 | | public class AzureActiveDirectoryTokenProvider : TokenProvider |
| | 13 | | { |
| | 14 | | readonly string authority; |
| | 15 | | readonly object authCallbackState; |
| | 16 | | event AuthenticationCallback AuthCallback; |
| | 17 | |
|
| | 18 | | public delegate Task<string> AuthenticationCallback(string audience, string authority, object state); |
| | 19 | |
|
| 0 | 20 | | public AzureActiveDirectoryTokenProvider(AuthenticationCallback authenticationCallback, string authority, object |
| | 21 | | { |
| 0 | 22 | | this.AuthCallback = authenticationCallback ?? throw Fx.Exception.ArgumentNull(nameof(authenticationCallback) |
| 0 | 23 | | this.authority = authority ?? throw Fx.Exception.ArgumentNull(nameof(authority)); |
| 0 | 24 | | this.authCallbackState = state; |
| 0 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Gets a <see cref="SecurityToken"/> for the given audience and duration. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="appliesTo">The URI which the access token applies to</param> |
| | 31 | | /// <param name="timeout">The time span that specifies the timeout value for the message that gets the security |
| | 32 | | /// <returns><see cref="SecurityToken"/></returns> |
| | 33 | | public override async Task<SecurityToken> GetTokenAsync(string appliesTo, TimeSpan timeout) |
| | 34 | | { |
| 0 | 35 | | var tokenString = await this.AuthCallback(Constants.AadServiceBusAudience, this.authority, this.authCallback |
| 0 | 36 | | return new JsonSecurityToken(tokenString, appliesTo); |
| 0 | 37 | | } |
| | 38 | | } |
| | 39 | | } |