< Summary

Class:Microsoft.Azure.ServiceBus.Management.SharedAccessAuthorizationRule
Assembly:Microsoft.Azure.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Management\SharedAccessAuthorizationRule.cs
Covered lines:0
Uncovered lines:123
Coverable lines:123
Total lines:329
Line coverage:0% (0 of 123)
Covered branches:0
Total branches:86
Branch coverage:0% (0 of 86)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-0%100%
.ctor(...)-0%100%
.ctor(...)-0%100%
.ctor(...)-0%100%
get_ClaimType()-0%100%
get_ClaimValue()-0%100%
get_KeyName()-0%100%
set_KeyName(...)-0%0%
get_PrimaryKey()-0%100%
set_PrimaryKey(...)-0%0%
get_SecondaryKey()-0%100%
set_SecondaryKey(...)-0%0%
get_Rights()-0%100%
set_Rights(...)-0%0%
GetHashCode()-0%0%
Equals(...)-0%100%
Equals(...)-0%0%
op_Equality(...)-0%0%
op_Inequality(...)-0%100%
GenerateRandomKey()-0%100%
CheckBase64(...)-0%100%
ParseFromXElement(...)-0%0%
Serialize()-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Management\SharedAccessAuthorizationRule.cs

#LineLine coverage
 1// Copyright (c) Microsoft. All rights reserved.
 2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
 3
 4namespace Microsoft.Azure.ServiceBus.Management
 5{
 6    using System;
 7    using System.Collections.Generic;
 8    using System.Linq;
 9    using System.Security.Cryptography;
 10    using System.Text;
 11    using System.Web;
 12    using System.Xml;
 13    using System.Xml.Linq;
 14    using Microsoft.Azure.ServiceBus.Primitives;
 15
 16    /// <summary>
 17    /// Defines the authorization rule for an entity using SAS.
 18    /// </summary>
 19    public class SharedAccessAuthorizationRule : AuthorizationRule
 20    {
 21        const int SupportedSASKeyLength = 44;
 22        const string FixedClaimType = "SharedAccessKey";
 23
 24        private string internalKeyName;
 25        private string internalPrimaryKey;
 26        private string internalSecondaryKey;
 27        private List<AccessRights> internalRights;
 28
 029        internal SharedAccessAuthorizationRule()
 30        {
 031        }
 32
 33        /// <summary>Initializes a new instance of the <see cref="SharedAccessAuthorizationRule" /> class.</summary>
 34        /// <param name="keyName">The authorization rule key name.</param>
 35        /// <param name="rights">The list of rights.</param>
 36        public SharedAccessAuthorizationRule(string keyName, IEnumerable<AccessRights> rights)
 037            : this(keyName, SharedAccessAuthorizationRule.GenerateRandomKey(), SharedAccessAuthorizationRule.GenerateRan
 38        {
 039        }
 40
 41        /// <summary>Initializes a new instance of the <see cref="SharedAccessAuthorizationRule" /> class.</summary>
 42        /// <param name="keyName">The authorization rule key name.</param>
 43        /// <param name="primaryKey">The primary key for the authorization rule.</param>
 44        /// <param name="rights">The list of rights.</param>
 45        public SharedAccessAuthorizationRule(string keyName, string primaryKey, IEnumerable<AccessRights> rights)
 046            : this(keyName, primaryKey, SharedAccessAuthorizationRule.GenerateRandomKey(), rights)
 47        {
 048        }
 49
 50        /// <summary>Initializes a new instance of the <see cref="SharedAccessAuthorizationRule" /> class.</summary>
 51        /// <param name="keyName">The authorization rule key name.</param>
 52        /// <param name="primaryKey">The primary key for the authorization rule.</param>
 53        /// <param name="secondaryKey">The secondary key for the authorization rule.</param>
 54        /// <param name="rights">The list of rights.</param>
 055        public SharedAccessAuthorizationRule(string keyName, string primaryKey, string secondaryKey, IEnumerable<AccessR
 56        {
 057            this.PrimaryKey = primaryKey;
 058            this.SecondaryKey = secondaryKey;
 059            this.Rights = new List<AccessRights>(rights);
 060            this.KeyName = keyName;
 061        }
 62
 063        public override string ClaimType => FixedClaimType;
 64
 065        internal override string ClaimValue => "None";
 66
 67        /// <summary>Gets or sets the authorization rule key name.</summary>
 68        /// <value>The authorization rule key name.</value>
 69        public override sealed string KeyName
 70        {
 071            get { return this.internalKeyName; }
 72            set
 73            {
 074                if (string.IsNullOrWhiteSpace(value))
 75                {
 076                    throw new ArgumentNullException(nameof(KeyName));
 77                }
 78
 079                if (value.Length > SharedAccessSignatureToken.MaxKeyNameLength)
 80                {
 081                    throw new ArgumentOutOfRangeException(
 082                        nameof(KeyName), $"The argument cannot exceed {SharedAccessSignatureToken.MaxKeyNameLength} char
 83                }
 84
 085                if (!string.Equals(value, HttpUtility.UrlEncode(value)))
 86                {
 087                    throw new ArgumentException("The key name specified contains invalid characters");
 88                }
 89
 090                this.internalKeyName = value;
 091            }
 92        }
 93
 94        /// <summary>Gets or sets the primary key for the authorization rule.</summary>
 95        /// <value>The primary key for the authorization rule.</value>
 96        public string PrimaryKey
 97        {
 098            get { return this.internalPrimaryKey; }
 99            set
 100            {
 0101                if (string.IsNullOrWhiteSpace(value))
 102                {
 0103                    throw new ArgumentNullException(nameof(PrimaryKey));
 104                }
 105
 0106                if (Encoding.ASCII.GetByteCount(value) != SupportedSASKeyLength)
 107                {
 0108                    throw new ArgumentOutOfRangeException(nameof(PrimaryKey), $"{nameof(SharedAccessAuthorizationRule)} 
 109                }
 110
 0111                if (!CheckBase64(value))
 112                {
 0113                    throw new ArgumentException(nameof(PrimaryKey), $"{nameof(SharedAccessAuthorizationRule)} only suppo
 114                }
 115
 0116                this.internalPrimaryKey = value;
 0117            }
 118        }
 119
 120        /// <summary>Gets or sets the secondary key for the authorization rule.</summary>
 121        /// <value>The secondary key for the authorization rule.</value>
 122        public string SecondaryKey
 123        {
 0124            get { return this.internalSecondaryKey; }
 125            set
 126            {
 0127                if (string.IsNullOrWhiteSpace(value))
 128                {
 0129                    throw new ArgumentNullException(nameof(SecondaryKey));
 130                }
 131
 0132                if (Encoding.ASCII.GetByteCount(value) != SupportedSASKeyLength)
 133                {
 0134                    throw new ArgumentOutOfRangeException(nameof(SecondaryKey), $"{nameof(SharedAccessAuthorizationRule)
 135                }
 136
 0137                if (!CheckBase64(value))
 138                {
 0139                    throw new ArgumentException(nameof(SecondaryKey), $"{nameof(SharedAccessAuthorizationRule)} only sup
 140                }
 141
 0142                this.internalSecondaryKey = value;
 0143            }
 144        }
 145
 146        public override List<AccessRights> Rights
 147        {
 0148            get => this.internalRights;
 149            set
 150            {
 0151                if (value == null || value.Count < 0 || value.Count > ManagementClientConstants.SupportedClaimsCount)
 152                {
 0153                    throw new ArgumentException($"Rights cannot be null, empty or greater than {ManagementClientConstant
 154                }
 155
 0156                HashSet<AccessRights> dedupedAccessRights = new HashSet<AccessRights>(value);
 0157                if (value.Count != dedupedAccessRights.Count)
 158                {
 0159                    throw new ArgumentException("Access rights on an authorization rule must be unique");
 160                }
 161
 0162                if (value.Contains(AccessRights.Manage) && value.Count != 3)
 163                {
 0164                    throw new ArgumentException(nameof(Rights), "Manage permission should also include Send and Listen")
 165                }
 166
 0167                this.internalRights = value;
 0168            }
 169        }
 170
 171        /// <summary>Returns the hash code for this instance.</summary>
 172        public override int GetHashCode()
 173        {
 0174            int hash = 13;
 175            unchecked
 176            {
 0177                hash = (hash * 7) + this.KeyName?.GetHashCode() ?? 0;
 0178                hash = (hash * 7) + this.PrimaryKey?.GetHashCode() ?? 0;
 0179                hash = (hash * 7) + this.SecondaryKey?.GetHashCode() ?? 0;
 0180                hash = (hash * 7) + this.Rights.GetHashCode();
 181            }
 182
 0183            return hash;
 184        }
 185
 186        public override bool Equals(object obj)
 187        {
 0188            var other = obj as AuthorizationRule;
 0189            return this.Equals(other);
 190        }
 191
 192        /// <summary>Determines whether the specified object is equal to the current object.</summary>
 193        /// <param name="other">The object to compare with the current object.</param>
 194        /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
 195        public override bool Equals(AuthorizationRule other)
 196        {
 0197            if (!(other is SharedAccessAuthorizationRule))
 198            {
 0199                return false;
 200            }
 201
 0202            SharedAccessAuthorizationRule comparand = (SharedAccessAuthorizationRule)other;
 0203            if (!string.Equals(this.KeyName, comparand.KeyName, StringComparison.OrdinalIgnoreCase) ||
 0204                !string.Equals(this.PrimaryKey, comparand.PrimaryKey, StringComparison.Ordinal) ||
 0205                !string.Equals(this.SecondaryKey, comparand.SecondaryKey, StringComparison.Ordinal))
 206            {
 0207                return false;
 208            }
 209
 0210            if ((this.Rights != null && comparand.Rights == null) ||
 0211                (this.Rights == null && comparand.Rights != null))
 212            {
 0213                return false;
 214            }
 215
 0216            if (this.Rights != null && comparand.Rights != null)
 217            {
 0218                HashSet<AccessRights> thisRights = new HashSet<AccessRights>(this.Rights);
 0219                if (comparand.Rights.Count != thisRights.Count)
 220                {
 0221                    return false;
 222                }
 223
 0224                return thisRights.SetEquals(comparand.Rights);
 225            }
 226
 0227            return true;
 228        }
 229
 230        public static bool operator ==(SharedAccessAuthorizationRule o1, SharedAccessAuthorizationRule o2)
 231        {
 0232            if (ReferenceEquals(o1, o2))
 233            {
 0234                return true;
 235            }
 236
 0237            if (ReferenceEquals(o1, null) || ReferenceEquals(o2, null))
 238            {
 0239                return false;
 240            }
 241
 0242            return o1.Equals(o2);
 243        }
 244
 245        public static bool operator !=(SharedAccessAuthorizationRule o1, SharedAccessAuthorizationRule o2)
 246        {
 0247            return !(o1 == o2);
 248        }
 249
 250        /// <summary>Generates the random key for the authorization rule.</summary>
 251        private static string GenerateRandomKey()
 252        {
 0253            byte[] key256 = new byte[32];
 0254            using (var rngCryptoServiceProvider = new RNGCryptoServiceProvider())
 255            {
 0256                rngCryptoServiceProvider.GetBytes(key256);
 0257            }
 258
 0259            return Convert.ToBase64String(key256);
 260        }
 261
 262        private static bool CheckBase64(string base64EncodedString)
 263        {
 264            try
 265            {
 0266                Convert.FromBase64String(base64EncodedString);
 0267                return true;
 268            }
 0269            catch (Exception)
 270            {
 0271                return false;
 272            }
 0273        }
 274
 275        internal static new SharedAccessAuthorizationRule ParseFromXElement(XElement xElement)
 276        {
 0277            var rule = new SharedAccessAuthorizationRule();
 0278            foreach (var element in xElement.Elements())
 279            {
 0280                switch (element.Name.LocalName)
 281                {
 282                    case "CreatedTime":
 0283                        rule.CreatedTime = XmlConvert.ToDateTime(element.Value, XmlDateTimeSerializationMode.Utc);
 0284                        break;
 285                    case "ModifiedTime":
 0286                        rule.ModifiedTime = XmlConvert.ToDateTime(element.Value, XmlDateTimeSerializationMode.Utc);
 0287                        break;
 288                    case "KeyName":
 0289                        rule.KeyName = element.Value;
 0290                        break;
 291                    case "PrimaryKey":
 0292                        rule.PrimaryKey = element.Value;
 0293                        break;
 294                    case "SecondaryKey":
 0295                        rule.SecondaryKey = element.Value;
 0296                        break;
 297                    case "Rights":
 0298                        var rights = new List<AccessRights>();
 0299                        var xRights = element.Elements(XName.Get("AccessRights", ManagementClientConstants.ServiceBusNam
 0300                        foreach (var xRight in xRights)
 301                        {
 0302                            rights.Add((AccessRights)Enum.Parse(typeof(AccessRights), xRight.Value));
 303                        }
 0304                        rule.Rights = rights;
 305                        break;
 306                }
 307            }
 308
 0309            return rule;
 310        }
 311
 312        internal override XElement Serialize()
 313        {
 0314            XElement rule = new XElement(
 0315                XName.Get("AuthorizationRule", ManagementClientConstants.ServiceBusNamespace),
 0316                new XAttribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace), nameof(SharedAcc
 0317                new XElement(XName.Get("ClaimType", ManagementClientConstants.ServiceBusNamespace), this.ClaimType),
 0318                new XElement(XName.Get("ClaimValue", ManagementClientConstants.ServiceBusNamespace), this.ClaimValue),
 0319                new XElement(XName.Get("Rights", ManagementClientConstants.ServiceBusNamespace),
 0320                    this.Rights.Select(right => new XElement(XName.Get("AccessRights", ManagementClientConstants.Service
 0321                new XElement(XName.Get("KeyName", ManagementClientConstants.ServiceBusNamespace), this.KeyName),
 0322                new XElement(XName.Get("PrimaryKey", ManagementClientConstants.ServiceBusNamespace), this.PrimaryKey),
 0323                new XElement(XName.Get("SecondaryKey", ManagementClientConstants.ServiceBusNamespace), this.SecondaryKey
 0324            );
 325
 0326            return rule;
 327        }
 328    }
 329}