| | 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.ServiceBus.Authorization; |
| | 9 | |
|
| | 10 | | namespace Azure.Messaging.ServiceBus |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Provides a credential based on a shared access signature for a given |
| | 14 | | /// Service Bus entity. |
| | 15 | | /// </summary> |
| | 16 | | /// |
| | 17 | | /// <seealso cref="Azure.Core.TokenCredential" /> |
| | 18 | | /// |
| | 19 | | internal sealed class ServiceBusSharedKeyCredential : 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="ServiceBusSharedKeyCredential"/> 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 ServiceBusSharedKeyCredential( |
| 0 | 49 | | string sharedAccessKeyName, |
| 0 | 50 | | string sharedAccessKey) |
| | 51 | | { |
| 0 | 52 | | Argument.AssertNotNullOrEmpty(sharedAccessKeyName, nameof(sharedAccessKeyName)); |
| 0 | 53 | | Argument.AssertNotNullOrEmpty(sharedAccessKey, nameof(sharedAccessKey)); |
| | 54 | |
|
| 0 | 55 | | SharedAccessKeyName = sharedAccessKeyName; |
| 0 | 56 | | SharedAccessKey = sharedAccessKey; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Retrieves the token that represents the shared access signature credential, for |
| | 61 | | /// use in authorization against an Service Bus entity. |
| | 62 | | /// </summary> |
| | 63 | | /// |
| | 64 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | 65 | | /// <param name="cancellationToken">The token used to request cancellation of the operation.</param> |
| | 66 | | /// |
| | 67 | | /// <returns>The token representing the shared access signature for this credential.</returns> |
| | 68 | | /// |
| | 69 | | public override AccessToken GetToken( |
| | 70 | | TokenRequestContext requestContext, |
| | 71 | | CancellationToken cancellationToken) => |
| 0 | 72 | | throw new InvalidOperationException(Resources.SharedKeyCredentialCannotGenerateTokens); |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Retrieves the token that represents the shared access signature credential, for |
| | 76 | | /// use in authorization against an Service Bus entity. |
| | 77 | | /// </summary> |
| | 78 | | /// |
| | 79 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | 80 | | /// <param name="cancellationToken">The token used to request cancellation of the operation.</param> |
| | 81 | | /// |
| | 82 | | /// <returns>The token representing the shared access signature for this credential.</returns> |
| | 83 | | /// |
| | 84 | | public override ValueTask<AccessToken> GetTokenAsync( |
| | 85 | | TokenRequestContext requestContext, |
| | 86 | | CancellationToken cancellationToken) => |
| 0 | 87 | | throw new InvalidOperationException(Resources.SharedKeyCredentialCannotGenerateTokens); |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Allows the rotation of Shared Access Signatures. |
| | 91 | | /// </summary> |
| | 92 | | /// |
| | 93 | | /// <param name="keyName">The name of the shared access key that the signature should be based on.</param> |
| | 94 | | /// <param name="keyValue">The value of the shared access key for the signature.</param> |
| | 95 | | /// |
| | 96 | | public void UpdateSharedAccessKey( |
| | 97 | | string keyName, |
| | 98 | | string keyValue) |
| | 99 | | { |
| 0 | 100 | | Argument.AssertNotNullOrEmpty(keyName, nameof(keyName)); |
| 0 | 101 | | Argument.AssertNotNullOrEmpty(keyValue, nameof(keyValue)); |
| | 102 | |
|
| 0 | 103 | | SharedAccessKeyName = keyName; |
| 0 | 104 | | SharedAccessKey = keyValue; |
| | 105 | |
|
| 0 | 106 | | SharedAccessSignatureCredential?.UpdateSharedAccessKey(keyName, keyValue); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Coverts to shared access signature credential. |
| | 111 | | /// It retains a reference to the generated SharedAccessSignatureCredential. |
| | 112 | | /// </summary> |
| | 113 | | /// |
| | 114 | | /// <param name="serviceBusResource">The Service Bus resource to which the token is intended to serve as authori |
| | 115 | | /// <param name="signatureValidityDuration">The duration that the signature should be considered valid; if not s |
| | 116 | | /// |
| | 117 | | /// <returns>A new <see cref="SharedAccessSignatureCredential" /> based on the requested shared access key.</ret |
| | 118 | | /// |
| | 119 | | internal SharedAccessSignatureCredential AsSharedAccessSignatureCredential( |
| | 120 | | string serviceBusResource, |
| | 121 | | TimeSpan? signatureValidityDuration = default) |
| | 122 | | { |
| 0 | 123 | | SharedAccessSignatureCredential = new SharedAccessSignatureCredential(new SharedAccessSignature(serviceBusRe |
| | 124 | |
|
| 0 | 125 | | return SharedAccessSignatureCredential; |
| | 126 | | } |
| | 127 | | } |
| | 128 | | } |