| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Azure.Core; |
| | | 8 | | using Azure.Messaging.EventHubs.Authorization; |
| | | 9 | | |
| | | 10 | | namespace Azure.Messaging.EventHubs |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides a credential based on a shared access signature for a given |
| | | 14 | | /// Event Hub instance. |
| | | 15 | | /// </summary> |
| | | 16 | | /// |
| | | 17 | | /// <seealso cref="Azure.Core.TokenCredential" /> |
| | | 18 | | /// |
| | | 19 | | internal sealed class EventHubSharedKeyCredential : TokenCredential |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// The name of the shared access key to be used for authorization, as |
| | | 23 | | /// reported by the Azure portal. |
| | | 24 | | /// </summary> |
| | | 25 | | /// |
| | 0 | 26 | | private string SharedAccessKeyName { get; set; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The value of the shared access key to be used for authorization, as |
| | | 30 | | /// reported by the Azure portal. |
| | | 31 | | /// </summary> |
| | | 32 | | /// |
| | 0 | 33 | | private string SharedAccessKey { get; set; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// A reference to a corresponding SharedAccessSignatureCredential. |
| | | 37 | | /// </summary> |
| | | 38 | | /// |
| | 0 | 39 | | private SharedAccessSignatureCredential SharedAccessSignatureCredential { get; set; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Initializes a new instance of the <see cref="EventHubSharedKeyCredential"/> class. |
| | | 43 | | /// </summary> |
| | | 44 | | /// |
| | | 45 | | /// <param name="sharedAccessKeyName">The name of the shared access key to be used for authorization, as reporte |
| | | 46 | | /// <param name="sharedAccessKey">The value of the shared access key to be used for authorization, as reported b |
| | | 47 | | /// |
| | 0 | 48 | | public EventHubSharedKeyCredential(string sharedAccessKeyName, |
| | 0 | 49 | | string sharedAccessKey) |
| | | 50 | | { |
| | 0 | 51 | | Argument.AssertNotNullOrEmpty(sharedAccessKeyName, nameof(sharedAccessKeyName)); |
| | 0 | 52 | | Argument.AssertNotNullOrEmpty(sharedAccessKey, nameof(sharedAccessKey)); |
| | | 53 | | |
| | 0 | 54 | | SharedAccessKeyName = sharedAccessKeyName; |
| | 0 | 55 | | SharedAccessKey = sharedAccessKey; |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Retrieves the token that represents the shared access signature credential, for |
| | | 60 | | /// use in authorization against an Event Hub. |
| | | 61 | | /// </summary> |
| | | 62 | | /// |
| | | 63 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | | 64 | | /// <param name="cancellationToken">The token used to request cancellation of the operation.</param> |
| | | 65 | | /// |
| | | 66 | | /// <returns>The token representing the shared access signature for this credential.</returns> |
| | | 67 | | /// |
| | 0 | 68 | | public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) => |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Retrieves the token that represents the shared access signature credential, for |
| | | 72 | | /// use in authorization against an Event Hub. |
| | | 73 | | /// </summary> |
| | | 74 | | /// |
| | | 75 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | | 76 | | /// <param name="cancellationToken">The token used to request cancellation of the operation.</param> |
| | | 77 | | /// |
| | | 78 | | /// <returns>The token representing the shared access signature for this credential.</returns> |
| | | 79 | | /// |
| | 0 | 80 | | public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cance |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Allows the rotation of Shared Access Signatures. |
| | | 84 | | /// </summary> |
| | | 85 | | /// |
| | | 86 | | /// <param name="keyName">The name of the shared access key that the signature should be based on.</param> |
| | | 87 | | /// <param name="keyValue">The value of the shared access key for the signature.</param> |
| | | 88 | | /// |
| | | 89 | | public void UpdateSharedAccessKey(string keyName, |
| | | 90 | | string keyValue) |
| | | 91 | | { |
| | 0 | 92 | | Argument.AssertNotNullOrEmpty(keyName, nameof(keyName)); |
| | 0 | 93 | | Argument.AssertNotNullOrEmpty(keyValue, nameof(keyValue)); |
| | | 94 | | |
| | 0 | 95 | | SharedAccessKeyName = keyName; |
| | 0 | 96 | | SharedAccessKey = keyValue; |
| | | 97 | | |
| | 0 | 98 | | SharedAccessSignatureCredential?.UpdateSharedAccessKey(keyName, keyValue); |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Coverts to shared access signature credential. |
| | | 103 | | /// It retains a reference to the generated SharedAccessSignatureCredential. |
| | | 104 | | /// </summary> |
| | | 105 | | /// |
| | | 106 | | /// <param name="eventHubResource">The Event Hubs resource to which the token is intended to serve as authorizat |
| | | 107 | | /// <param name="signatureValidityDuration">The duration that the signature should be considered valid; if not s |
| | | 108 | | /// |
| | | 109 | | /// <returns>A new <see cref="SharedAccessSignatureCredential" /> based on the requested shared access key.</ret |
| | | 110 | | /// |
| | | 111 | | internal SharedAccessSignatureCredential AsSharedAccessSignatureCredential(string eventHubResource, |
| | | 112 | | TimeSpan? signatureValidityDuration = |
| | | 113 | | { |
| | 0 | 114 | | SharedAccessSignatureCredential = new SharedAccessSignatureCredential(new SharedAccessSignature(eventHubReso |
| | | 115 | | |
| | 0 | 116 | | return SharedAccessSignatureCredential; |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | } |