| | | 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 | | using Azure.Messaging.ServiceBus; |
| | | 8 | | |
| | | 9 | | namespace Azure.Messaging.ServiceBus.Authorization |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides a generic token-based credential for a given Service Bus entity instance. |
| | | 13 | | /// </summary> |
| | | 14 | | /// |
| | | 15 | | /// <seealso cref="Azure.Core.TokenCredential" /> |
| | | 16 | | /// |
| | | 17 | | internal class ServiceBusTokenCredential : TokenCredential |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// The Service Bus resource to which the token is intended to serve as authorization. |
| | | 21 | | /// </summary> |
| | | 22 | | /// |
| | 0 | 23 | | public string Resource { get; } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Indicates whether the credential is based on an Service Bus |
| | | 27 | | /// shared access signature. |
| | | 28 | | /// </summary> |
| | | 29 | | /// |
| | | 30 | | /// <value><c>true</c> if the credential should be considered a SAS credential; otherwise, <c>false</c>.</value> |
| | | 31 | | /// |
| | 744 | 32 | | public bool IsSharedAccessSignatureCredential { get; } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The <see cref="TokenCredential" /> that forms the basis of this security token. |
| | | 36 | | /// </summary> |
| | | 37 | | /// |
| | 320 | 38 | | private TokenCredential Credential { get; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Initializes a new instance of the <see cref="ServiceBusTokenCredential"/> class. |
| | | 42 | | /// </summary> |
| | | 43 | | /// |
| | | 44 | | /// <param name="tokenCredential">The <see cref="TokenCredential" /> on which to base the token.</param> |
| | | 45 | | /// <param name="serviceBusResource">The Service Bus resource to which the token is intended to serve as authori |
| | | 46 | | /// |
| | 134 | 47 | | public ServiceBusTokenCredential( |
| | 134 | 48 | | TokenCredential tokenCredential, |
| | 134 | 49 | | string serviceBusResource) |
| | | 50 | | { |
| | 134 | 51 | | Argument.AssertNotNull(tokenCredential, nameof(tokenCredential)); |
| | 134 | 52 | | Argument.AssertNotNullOrEmpty(serviceBusResource, nameof(serviceBusResource)); |
| | | 53 | | |
| | 134 | 54 | | Credential = tokenCredential; |
| | 134 | 55 | | Resource = serviceBusResource; |
| | | 56 | | |
| | 134 | 57 | | IsSharedAccessSignatureCredential = |
| | 134 | 58 | | (tokenCredential is ServiceBusSharedKeyCredential) |
| | 134 | 59 | | || (tokenCredential is SharedAccessSignatureCredential) |
| | 134 | 60 | | || ((tokenCredential as ServiceBusTokenCredential)?.IsSharedAccessSignatureCredential == true); |
| | 134 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Retrieves the token that represents the shared access signature credential, for |
| | | 65 | | /// use in authorization against an Service Bus entity. |
| | | 66 | | /// </summary> |
| | | 67 | | /// |
| | | 68 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | | 69 | | /// <param name="cancellationToken">The token used to request cancellation of the operation.</param> |
| | | 70 | | /// |
| | | 71 | | /// <returns>The token representing the shared access signature for this credential.</returns> |
| | | 72 | | /// |
| | | 73 | | public override AccessToken GetToken( |
| | | 74 | | TokenRequestContext requestContext, |
| | | 75 | | CancellationToken cancellationToken) => |
| | 0 | 76 | | Credential.GetToken(requestContext, cancellationToken); |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Retrieves the token that represents the shared access signature credential, for |
| | | 80 | | /// use in authorization against an Service Bus entity. |
| | | 81 | | /// </summary> |
| | | 82 | | /// |
| | | 83 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | | 84 | | /// <param name="cancellationToken">The token used to request cancellation of the operation.</param> |
| | | 85 | | /// |
| | | 86 | | /// <returns>The token representing the shared access signature for this credential.</returns> |
| | | 87 | | /// |
| | | 88 | | public override ValueTask<AccessToken> GetTokenAsync( |
| | | 89 | | TokenRequestContext requestContext, |
| | | 90 | | CancellationToken cancellationToken) => |
| | 320 | 91 | | Credential.GetTokenAsync(requestContext, cancellationToken); |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Retrieves the token that represents the shared access signature credential, for |
| | | 95 | | /// use in authorization against an Service Bus entity. It provides a default value for the Token Request Cont |
| | | 96 | | /// </summary> |
| | | 97 | | /// |
| | | 98 | | /// <param name="cancellationToken">The token used to request cancellation of the operation.</param> |
| | | 99 | | /// |
| | | 100 | | /// <returns>The token representing the shared access signature for this credential.</returns> |
| | | 101 | | /// |
| | | 102 | | public ValueTask<AccessToken> GetTokenUsingDefaultScopeAsync(CancellationToken cancellationToken) => |
| | 0 | 103 | | GetTokenAsync(new TokenRequestContext(new string[] { Constants.DefaultScope }), cancellationToken); |
| | | 104 | | } |
| | | 105 | | } |