| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Threading; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Azure.Core; |
| | 7 | |
|
| | 8 | | namespace Azure.Messaging.EventHubs.Authorization |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Provides a generic token-based credential for a given Event Hub instance. |
| | 12 | | /// </summary> |
| | 13 | | /// |
| | 14 | | /// <seealso cref="Azure.Core.TokenCredential" /> |
| | 15 | | /// |
| | 16 | | internal class EventHubTokenCredential : TokenCredential |
| | 17 | | { |
| | 18 | | /// <summary>The default scope used for token authentication with EventHubs.</summary> |
| | 19 | | private const string DefaultScope = "https://eventhubs.azure.net/.default"; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// The Event Hubs resource to which the token is intended to serve as authorization. |
| | 23 | | /// </summary> |
| | 24 | | /// |
| 0 | 25 | | public string Resource { get; } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Indicates whether the credential is based on an Event Hubs |
| | 29 | | /// shared access signature. |
| | 30 | | /// </summary> |
| | 31 | | /// |
| | 32 | | /// <value><c>true</c> if the credential should be considered a SAS credential; otherwise, <c>false</c>.</value> |
| | 33 | | /// |
| 0 | 34 | | public bool IsSharedAccessSignatureCredential { get; } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// The <see cref="TokenCredential" /> that forms the basis of this security token. |
| | 38 | | /// </summary> |
| | 39 | | /// |
| 0 | 40 | | private TokenCredential Credential { get; } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Initializes a new instance of the <see cref="EventHubTokenCredential"/> class. |
| | 44 | | /// </summary> |
| | 45 | | /// |
| | 46 | | /// <param name="tokenCredential">The <see cref="TokenCredential" /> on which to base the token.</param> |
| | 47 | | /// <param name="eventHubResource">The Event Hubs resource to which the token is intended to serve as authorizat |
| | 48 | | /// |
| 0 | 49 | | public EventHubTokenCredential(TokenCredential tokenCredential, |
| 0 | 50 | | string eventHubResource) |
| | 51 | | { |
| 0 | 52 | | Argument.AssertNotNull(tokenCredential, nameof(tokenCredential)); |
| 0 | 53 | | Argument.AssertNotNullOrEmpty(eventHubResource, nameof(eventHubResource)); |
| | 54 | |
|
| 0 | 55 | | Credential = tokenCredential; |
| 0 | 56 | | Resource = eventHubResource; |
| | 57 | |
|
| 0 | 58 | | IsSharedAccessSignatureCredential = |
| 0 | 59 | | (tokenCredential is EventHubSharedKeyCredential) |
| 0 | 60 | | || (tokenCredential is SharedAccessSignatureCredential) |
| 0 | 61 | | || ((tokenCredential as EventHubTokenCredential)?.IsSharedAccessSignatureCredential == true); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Retrieves the token that represents the shared access signature credential, for |
| | 66 | | /// use in authorization against an Event Hub. |
| | 67 | | /// </summary> |
| | 68 | | /// |
| | 69 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | 70 | | /// <param name="cancellationToken">The token used to request cancellation of the operation.</param> |
| | 71 | | /// |
| | 72 | | /// <returns>The token representing the shared access signature for this credential.</returns> |
| | 73 | | /// |
| | 74 | | public override AccessToken GetToken(TokenRequestContext requestContext, |
| 0 | 75 | | CancellationToken cancellationToken) => Credential.GetToken(requestContext, |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Retrieves the token that represents the shared access signature credential, for |
| | 79 | | /// use in authorization against an Event Hub. |
| | 80 | | /// </summary> |
| | 81 | | /// |
| | 82 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | 83 | | /// <param name="cancellationToken">The token used to request cancellation of the operation.</param> |
| | 84 | | /// |
| | 85 | | /// <returns>The token representing the shared access signature for this credential.</returns> |
| | 86 | | /// |
| | 87 | | public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, |
| 0 | 88 | | CancellationToken cancellationToken) => Credential.GetToken |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Retrieves the token that represents the shared access signature credential, for |
| | 92 | | /// use in authorization against an Event Hub. It provides a default value for the Token Request Context. |
| | 93 | | /// </summary> |
| | 94 | | /// |
| | 95 | | /// <param name="cancellationToken">The token used to request cancellation of the operation.</param> |
| | 96 | | /// |
| | 97 | | /// <returns>The token representing the shared access signature for this credential.</returns> |
| | 98 | | /// |
| 0 | 99 | | public ValueTask<AccessToken> GetTokenUsingDefaultScopeAsync(CancellationToken cancellationToken) => GetTokenAsy |
| | 100 | | } |
| | 101 | | } |