< Summary

Class:Azure.Messaging.EventHubs.Amqp.CbsTokenProvider
Assembly:Azure.Messaging.EventHubs
File(s):C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs\src\Amqp\CbsTokenProvider.cs
Covered lines:12
Uncovered lines:0
Coverable lines:12
Total lines:75
Line coverage:100% (12 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()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs\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.EventHubs.Authorization;
 9using Microsoft.Azure.Amqp;
 10
 11namespace Azure.Messaging.EventHubs.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 Event Hubs 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 EventHubTokenCredential 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        ///
 24644        public CbsTokenProvider(EventHubTokenCredential credential,
 24645                                CancellationToken cancellationToken)
 46        {
 24647            Argument.AssertNotNull(credential, nameof(credential));
 48
 24449            Credential = credential;
 24450            CancellationToken = cancellationToken;
 51
 24452            TokenType = (credential.IsSharedAccessSignatureCredential)
 24453                ? SharedAccessSignatureTokenType
 24454                : JsonWebTokenType;
 24455        }
 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        {
 671            AccessToken token = await Credential.GetTokenUsingDefaultScopeAsync(CancellationToken).ConfigureAwait(false)
 672            return new CbsToken(token.Token, TokenType, token.ExpiresOn.UtcDateTime);
 673        }
 74    }
 75}

Methods/Properties

.ctor(...)
GetTokenAsync()