| | 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 | | /// This abstract base class can be extended to implement additional token providers. |
| | 11 | | /// </summary> |
| | 12 | | public abstract class TokenProvider : ITokenProvider |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Construct a TokenProvider based on a sharedAccessSignature. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="sharedAccessSignature">The shared access signature</param> |
| | 18 | | /// <returns>A TokenProvider initialized with the shared access signature</returns> |
| | 19 | | /// <remarks><see cref="TokenProvider.GetTokenAsync(string, TimeSpan)"/> parameters will not be used |
| | 20 | | /// to manipulate target URL and token TTL.</remarks> |
| | 21 | | public static TokenProvider CreateSharedAccessSignatureTokenProvider(string sharedAccessSignature) |
| | 22 | | { |
| 0 | 23 | | return new SharedAccessSignatureTokenProvider(sharedAccessSignature); |
| | 24 | | } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Construct a TokenProvider based on the provided Key Name and Shared Access Key. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="keyName">The key name of the corresponding SharedAccessKeyAuthorizationRule.</param> |
| | 30 | | /// <param name="sharedAccessKey">The key associated with the SharedAccessKeyAuthorizationRule</param> |
| | 31 | | /// <returns>A TokenProvider initialized with the provided RuleId and Password</returns> |
| | 32 | | /// <remarks>Default token TTL is 1 hour and token scope is at entity level.</remarks> |
| | 33 | | public static TokenProvider CreateSharedAccessSignatureTokenProvider(string keyName, string sharedAccessKey) |
| | 34 | | { |
| 0 | 35 | | return new SharedAccessSignatureTokenProvider(keyName, sharedAccessKey); |
| | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Construct a TokenProvider based on the provided Key Name and Shared Access Key. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="keyName">The key name of the corresponding SharedAccessKeyAuthorizationRule.</param> |
| | 42 | | /// <param name="sharedAccessKey">The key associated with the SharedAccessKeyAuthorizationRule</param> |
| | 43 | | /// <param name="tokenTimeToLive">The token time to live</param> |
| | 44 | | /// <returns>A TokenProvider initialized with the provided keyName and key.</returns> |
| | 45 | | public static TokenProvider CreateSharedAccessSignatureTokenProvider(string keyName, string sharedAccessKey, Tim |
| | 46 | | { |
| 0 | 47 | | return new SharedAccessSignatureTokenProvider(keyName, sharedAccessKey, tokenTimeToLive); |
| | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Construct a TokenProvider based on the provided Key Name and Shared Access Key. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="keyName">The key name of the corresponding SharedAccessKeyAuthorizationRule.</param> |
| | 54 | | /// <param name="sharedAccessKey">The key associated with the SharedAccessKeyAuthorizationRule</param> |
| | 55 | | /// <param name="tokenScope">The tokenScope of tokens to request.</param> |
| | 56 | | /// <returns>A TokenProvider initialized with the provided keyName and key</returns> |
| | 57 | | /// <remarks>Default token TTL is 1 hour.</remarks> |
| | 58 | | public static TokenProvider CreateSharedAccessSignatureTokenProvider(string keyName, string sharedAccessKey, Tok |
| | 59 | | { |
| 0 | 60 | | return new SharedAccessSignatureTokenProvider(keyName, sharedAccessKey, tokenScope); |
| | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Construct a TokenProvider based on the provided Key Name and Shared Access Key. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="keyName">The key name of the corresponding SharedAccessKeyAuthorizationRule.</param> |
| | 67 | | /// <param name="sharedAccessKey">The key associated with the SharedAccessKeyAuthorizationRule</param> |
| | 68 | | /// <param name="tokenTimeToLive">The token time to live</param> |
| | 69 | | /// <param name="tokenScope">The tokenScope of tokens to request.</param> |
| | 70 | | /// <returns>A TokenProvider initialized with the provided RuleId and Password</returns> |
| | 71 | | public static TokenProvider CreateSharedAccessSignatureTokenProvider(string keyName, string sharedAccessKey, Tim |
| | 72 | | { |
| 0 | 73 | | return new SharedAccessSignatureTokenProvider(keyName, sharedAccessKey, tokenTimeToLive, tokenScope); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | /// <param name="authCallback">The user defined authentication delegate to provide access token.</param> |
| | 77 | | /// <param name="authority">URL of the Azure Active Directory instance to issue token.</param> |
| | 78 | | /// <param name="state">Custom parameters that may be passed into the authentication delegate.</param> |
| | 79 | | /// <returns>The <see cref="Microsoft.ServiceBus.TokenProvider" /> for returning Json web token.</returns> |
| | 80 | | public static TokenProvider CreateAzureActiveDirectoryTokenProvider( |
| | 81 | | AzureActiveDirectoryTokenProvider.AuthenticationCallback authCallback, |
| | 82 | | string authority, |
| | 83 | | object state = null) |
| | 84 | | { |
| 0 | 85 | | if (authCallback == null) |
| | 86 | | { |
| 0 | 87 | | throw new ArgumentNullException(nameof(authCallback)); |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | return new AzureActiveDirectoryTokenProvider(authCallback, authority, state); |
| | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary>Creates Azure Managed Identity token provider.</summary> |
| | 94 | | /// <returns>The <see cref="TokenProvider" /> for returning Json web token.</returns> |
| | 95 | | public static TokenProvider CreateManagedIdentityTokenProvider() |
| | 96 | | { |
| 0 | 97 | | return new ManagedIdentityTokenProvider(); |
| | 98 | | } |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Gets a <see cref="SecurityToken"/> for the given audience and duration. |
| | 102 | | /// </summary> |
| | 103 | | /// <param name="appliesTo">The URI which the access token applies to</param> |
| | 104 | | /// <param name="timeout">The timeout value for how long it takes to get the security token (not the token time |
| | 105 | | /// <remarks>This parameter <paramref name="timeout"/> is here for compatibility, but is not currently used.</re |
| | 106 | | public abstract Task<SecurityToken> GetTokenAsync(string appliesTo, TimeSpan timeout); |
| | 107 | | } |
| | 108 | | } |