< Summary

Class:Azure.Messaging.EventHubs.Authorization.EventHubTokenCredential
Assembly:Azure.Messaging.EventHubs
File(s):C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs.Shared\src\Authorization\EventHubTokenCredential.cs
Covered lines:15
Uncovered lines:2
Coverable lines:17
Total lines:101
Line coverage:88.2% (15 of 17)
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(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs.Shared\src\Authorization\EventHubTokenCredential.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;
 7
 8namespace Azure.Messaging.EventHubs.Authorization
 9{
 10    /// <summary>
 11    ///   Provides a generic token-based credential for a given Event Hub instance.
 12    /// </summary>
 13    ///
 14    /// <seealso cref="Azure.Core.TokenCredential" />
 15    ///
 16    internal class EventHubTokenCredential : TokenCredential
 17    {
 18        /// <summary>The default scope used for token authentication with EventHubs.</summary>
 19        private const string DefaultScope = "https://eventhubs.azure.net/.default";
 20
 21        /// <summary>
 22        ///   The Event Hubs resource to which the token is intended to serve as authorization.
 23        /// </summary>
 24        ///
 025        public string Resource { get; }
 26
 27        /// <summary>
 28        ///   Indicates whether the credential is based on an Event Hubs
 29        ///   shared access signature.
 30        /// </summary>
 31        ///
 32        /// <value><c>true</c> if the credential should be considered a SAS credential; otherwise, <c>false</c>.</value>
 33        ///
 73034        public bool IsSharedAccessSignatureCredential { get; }
 35
 36        /// <summary>
 37        ///   The <see cref="TokenCredential" /> that forms the basis of this security token.
 38        /// </summary>
 39        ///
 640        private TokenCredential Credential { get; }
 41
 42        /// <summary>
 43        ///   Initializes a new instance of the <see cref="EventHubTokenCredential"/> class.
 44        /// </summary>
 45        ///
 46        /// <param name="tokenCredential">The <see cref="TokenCredential" /> on which to base the token.</param>
 47        /// <param name="eventHubResource">The Event Hubs resource to which the token is intended to serve as authorizat
 48        ///
 108249        public EventHubTokenCredential(TokenCredential tokenCredential,
 108250                                       string eventHubResource)
 51        {
 108252            Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));
 108253            Argument.AssertNotNullOrEmpty(eventHubResource, nameof(eventHubResource));
 54
 108255            Credential = tokenCredential;
 108256            Resource = eventHubResource;
 57
 108258            IsSharedAccessSignatureCredential =
 108259                (tokenCredential is EventHubSharedKeyCredential)
 108260                || (tokenCredential is SharedAccessSignatureCredential)
 108261                || ((tokenCredential as EventHubTokenCredential)?.IsSharedAccessSignatureCredential == true);
 108262        }
 63
 64        /// <summary>
 65        ///   Retrieves the token that represents the shared access signature credential, for
 66        ///   use in authorization against an Event Hub.
 67        /// </summary>
 68        ///
 69        /// <param name="requestContext">The details of the authentication request.</param>
 70        /// <param name="cancellationToken">The token used to request cancellation of the operation.</param>
 71        ///
 72        /// <returns>The token representing the shared access signature for this credential.</returns>
 73        ///
 74        public override AccessToken GetToken(TokenRequestContext requestContext,
 075                                             CancellationToken cancellationToken) => Credential.GetToken(requestContext,
 76
 77        /// <summary>
 78        ///   Retrieves the token that represents the shared access signature credential, for
 79        ///   use in authorization against an Event Hub.
 80        /// </summary>
 81        ///
 82        /// <param name="requestContext">The details of the authentication request.</param>
 83        /// <param name="cancellationToken">The token used to request cancellation of the operation.</param>
 84        ///
 85        /// <returns>The token representing the shared access signature for this credential.</returns>
 86        ///
 87        public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext,
 688                                                             CancellationToken cancellationToken) => Credential.GetToken
 89
 90        /// <summary>
 91        ///   Retrieves the token that represents the shared access signature credential, for
 92        ///   use in authorization against an Event Hub. It provides a default value for the Token Request Context.
 93        /// </summary>
 94        ///
 95        /// <param name="cancellationToken">The token used to request cancellation of the operation.</param>
 96        ///
 97        /// <returns>The token representing the shared access signature for this credential.</returns>
 98        ///
 4299        public ValueTask<AccessToken> GetTokenUsingDefaultScopeAsync(CancellationToken cancellationToken) => GetTokenAsy
 100    }
 101}