< Summary

Class:Azure.Messaging.ServiceBus.Authorization.ServiceBusTokenCredential
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Authorization\ServiceBusTokenCredential.cs
Covered lines:15
Uncovered lines:3
Coverable lines:18
Total lines:105
Line coverage:83.3% (15 of 18)
Covered branches:6
Total branches:6
Branch coverage:100% (6 of 6)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_Resource()-0%100%
get_IsSharedAccessSignatureCredential()-100%100%
get_Credential()-100%100%
.ctor(...)-100%100%
GetToken(...)-0%100%
GetTokenAsync(...)-100%100%
GetTokenUsingDefaultScopeAsync(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Authorization\ServiceBusTokenCredential.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Threading;
 5using System.Threading.Tasks;
 6using Azure.Core;
 7using Azure.Messaging.ServiceBus;
 8
 9namespace 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        ///
 023        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        ///
 74432        public bool IsSharedAccessSignatureCredential { get; }
 33
 34        /// <summary>
 35        ///   The <see cref="TokenCredential" /> that forms the basis of this security token.
 36        /// </summary>
 37        ///
 32038        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        ///
 13447        public ServiceBusTokenCredential(
 13448            TokenCredential tokenCredential,
 13449            string serviceBusResource)
 50        {
 13451            Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));
 13452            Argument.AssertNotNullOrEmpty(serviceBusResource, nameof(serviceBusResource));
 53
 13454            Credential = tokenCredential;
 13455            Resource = serviceBusResource;
 56
 13457            IsSharedAccessSignatureCredential =
 13458                (tokenCredential is ServiceBusSharedKeyCredential)
 13459                || (tokenCredential is SharedAccessSignatureCredential)
 13460                || ((tokenCredential as ServiceBusTokenCredential)?.IsSharedAccessSignatureCredential == true);
 13461        }
 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) =>
 076            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) =>
 32091            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) =>
 0103            GetTokenAsync(new TokenRequestContext(new string[] { Constants.DefaultScope }), cancellationToken);
 104    }
 105}