< Summary

Class:Azure.Messaging.ServiceBus.Amqp.CbsTokenProvider
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Amqp\CbsTokenProvider.cs
Covered lines:9
Uncovered lines:3
Coverable lines:12
Total lines:75
Line coverage:75% (9 of 12)
Covered branches:2
Total branches:2
Branch coverage:100% (2 of 2)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
GetTokenAsync()-0%100%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Azure.Core;
 8using Azure.Messaging.ServiceBus.Authorization;
 9using Microsoft.Azure.Amqp;
 10
 11namespace Azure.Messaging.ServiceBus.Amqp
 12{
 13    /// <summary>
 14    ///   Performs the actions needed to generate <see cref="CbsToken" /> instances for
 15    ///   authorization within an AMQP scope.
 16    /// </summary>
 17    ///
 18    /// <seealso cref="Microsoft.Azure.Amqp.ICbsTokenProvider" />
 19    ///
 20    internal sealed class CbsTokenProvider : ICbsTokenProvider
 21    {
 22        /// <summary>The type to consider a token if it is based on an Service Bus shared access signature.</summary>
 23        private const string SharedAccessSignatureTokenType = "servicebus.windows.net:sastoken";
 24
 25        /// <summary>The type to consider a token if not based on a shared access signature.</summary>
 26        private const string JsonWebTokenType = "jwt";
 27
 28        /// <summary>The type to consider a token generated from the associated <see cref="Credential" />.</summary>
 29        private readonly string TokenType;
 30
 31        /// <summary>The credential used to generate access tokens.</summary>
 32        private readonly ServiceBusTokenCredential Credential;
 33
 34        /// <summary>The cancellation token to consider when making requests.</summary>
 35        private readonly CancellationToken CancellationToken;
 36
 37        /// <summary>
 38        ///   Initializes a new instance of the <see cref="CbsTokenProvider"/> class.
 39        /// </summary>
 40        ///
 41        /// <param name="credential">The credential to use for access token generation.</param>
 42        /// <param name="cancellationToken">The cancellation token to consider when making requests.</param>
 43        ///
 4244        public CbsTokenProvider(ServiceBusTokenCredential credential,
 4245                                CancellationToken cancellationToken)
 46        {
 4247            Argument.AssertNotNull(credential, nameof(credential));
 48
 4249            Credential = credential;
 4250            CancellationToken = cancellationToken;
 51
 4252            TokenType = (credential.IsSharedAccessSignatureCredential)
 4253                ? SharedAccessSignatureTokenType
 4254                : JsonWebTokenType;
 4255        }
 56
 57        /// <summary>
 58        ///   Asynchronously requests a CBS token to be used for authorization within an AMQP
 59        ///   scope.
 60        /// </summary>
 61        ///
 62        /// <param name="namespaceAddress">The address of the namespace to be authorized.</param>
 63        /// <param name="appliesTo">The resource to which the token should apply.</param>
 64        /// <param name="requiredClaims">The set of claims that are required for authorization.</param>
 65        /// <returns>The token to use for authorization.</returns>
 66        ///
 67        public async Task<CbsToken> GetTokenAsync(Uri namespaceAddress,
 68                                                  string appliesTo,
 69                                                  string[] requiredClaims)
 70        {
 071            AccessToken token = await Credential.GetTokenUsingDefaultScopeAsync(CancellationToken).ConfigureAwait(false)
 072            return new CbsToken(token.Token, TokenType, token.ExpiresOn.UtcDateTime);
 073        }
 74    }
 75}

Methods/Properties

.ctor(...)
GetTokenAsync()