< Summary

Class:Azure.Messaging.ServiceBus.ServiceBusSharedKeyCredential
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Authorization\ServiceBusSharedKeyCredential.cs
Covered lines:0
Uncovered lines:21
Coverable lines:21
Total lines:128
Line coverage:0% (0 of 21)
Covered branches:0
Total branches:2
Branch coverage:0% (0 of 2)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_SharedAccessKeyName()-0%100%
get_SharedAccessKey()-0%100%
get_SharedAccessSignatureCredential()-0%100%
.ctor(...)-0%100%
GetToken(...)-0%100%
GetTokenAsync(...)-0%100%
UpdateSharedAccessKey(...)-0%0%
AsSharedAccessSignatureCredential(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Authorization\ServiceBusSharedKeyCredential.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;
 9
 10namespace Azure.Messaging.ServiceBus
 11{
 12    /// <summary>
 13    ///   Provides a credential based on a shared access signature for a given
 14    ///   Service Bus entity.
 15    /// </summary>
 16    ///
 17    /// <seealso cref="Azure.Core.TokenCredential" />
 18    ///
 19    internal sealed class ServiceBusSharedKeyCredential : TokenCredential
 20    {
 21        /// <summary>
 22        ///   The name of the shared access key to be used for authorization, as
 23        ///   reported by the Azure portal.
 24        /// </summary>
 25        ///
 026        private string SharedAccessKeyName { get; set; }
 27
 28        /// <summary>
 29        ///   The value of the shared access key to be used for authorization, as
 30        ///   reported by the Azure portal.
 31        /// </summary>
 32        ///
 033        private string SharedAccessKey { get; set; }
 34
 35        /// <summary>
 36        ///   A reference to a corresponding SharedAccessSignatureCredential.
 37        /// </summary>
 38        ///
 039        private SharedAccessSignatureCredential SharedAccessSignatureCredential { get; set; }
 40
 41        /// <summary>
 42        ///   Initializes a new instance of the <see cref="ServiceBusSharedKeyCredential"/> class.
 43        /// </summary>
 44        ///
 45        /// <param name="sharedAccessKeyName">The name of the shared access key to be used for authorization, as reporte
 46        /// <param name="sharedAccessKey">The value of the shared access key to be used for authorization, as reported b
 47        ///
 048        public ServiceBusSharedKeyCredential(
 049            string sharedAccessKeyName,
 050            string sharedAccessKey)
 51        {
 052            Argument.AssertNotNullOrEmpty(sharedAccessKeyName, nameof(sharedAccessKeyName));
 053            Argument.AssertNotNullOrEmpty(sharedAccessKey, nameof(sharedAccessKey));
 54
 055            SharedAccessKeyName = sharedAccessKeyName;
 056            SharedAccessKey = sharedAccessKey;
 057        }
 58
 59        /// <summary>
 60        ///   Retrieves the token that represents the shared access signature credential, for
 61        ///   use in authorization against an Service Bus entity.
 62        /// </summary>
 63        ///
 64        /// <param name="requestContext">The details of the authentication request.</param>
 65        /// <param name="cancellationToken">The token used to request cancellation of the operation.</param>
 66        ///
 67        /// <returns>The token representing the shared access signature for this credential.</returns>
 68        ///
 69        public override AccessToken GetToken(
 70            TokenRequestContext requestContext,
 71            CancellationToken cancellationToken) =>
 072            throw new InvalidOperationException(Resources.SharedKeyCredentialCannotGenerateTokens);
 73
 74        /// <summary>
 75        ///   Retrieves the token that represents the shared access signature credential, for
 76        ///   use in authorization against an Service Bus entity.
 77        /// </summary>
 78        ///
 79        /// <param name="requestContext">The details of the authentication request.</param>
 80        /// <param name="cancellationToken">The token used to request cancellation of the operation.</param>
 81        ///
 82        /// <returns>The token representing the shared access signature for this credential.</returns>
 83        ///
 84        public override ValueTask<AccessToken> GetTokenAsync(
 85            TokenRequestContext requestContext,
 86            CancellationToken cancellationToken) =>
 087            throw new InvalidOperationException(Resources.SharedKeyCredentialCannotGenerateTokens);
 88
 89        /// <summary>
 90        ///   Allows the rotation of Shared Access Signatures.
 91        /// </summary>
 92        ///
 93        /// <param name="keyName">The name of the shared access key that the signature should be based on.</param>
 94        /// <param name="keyValue">The value of the shared access key for the signature.</param>
 95        ///
 96        public void UpdateSharedAccessKey(
 97            string keyName,
 98            string keyValue)
 99        {
 0100            Argument.AssertNotNullOrEmpty(keyName, nameof(keyName));
 0101            Argument.AssertNotNullOrEmpty(keyValue, nameof(keyValue));
 102
 0103            SharedAccessKeyName = keyName;
 0104            SharedAccessKey = keyValue;
 105
 0106            SharedAccessSignatureCredential?.UpdateSharedAccessKey(keyName, keyValue);
 0107        }
 108
 109        /// <summary>
 110        ///   Coverts to shared access signature credential.
 111        ///   It retains a reference to the generated SharedAccessSignatureCredential.
 112        /// </summary>
 113        ///
 114        /// <param name="serviceBusResource">The Service Bus resource to which the token is intended to serve as authori
 115        /// <param name="signatureValidityDuration">The duration that the signature should be considered valid; if not s
 116        ///
 117        /// <returns>A new <see cref="SharedAccessSignatureCredential" /> based on the requested shared access key.</ret
 118        ///
 119        internal SharedAccessSignatureCredential AsSharedAccessSignatureCredential(
 120            string serviceBusResource,
 121            TimeSpan? signatureValidityDuration = default)
 122        {
 0123            SharedAccessSignatureCredential = new SharedAccessSignatureCredential(new SharedAccessSignature(serviceBusRe
 124
 0125            return SharedAccessSignatureCredential;
 126        }
 127    }
 128}