< Summary

Class:Azure.Iot.Hub.Service.Authentication.SharedAccessSignatureBuilder
Assembly:Azure.Iot.Hub.Service
File(s):C:\Git\azure-sdk-for-net\sdk\iot\Azure.Iot.Hub.Service\src\Authentication\SharedAccessSignatureBuilder.cs
Covered lines:28
Uncovered lines:0
Coverable lines:28
Total lines:80
Line coverage:100% (28 of 28)
Covered branches:2
Total branches:2
Branch coverage:100% (2 of 2)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_SharedAccessPolicy()-100%100%
get_SharedAccessKey()-100%100%
get_HostName()-100%100%
get_TimeToLive()-100%100%
ToSignature()-100%100%
BuildSignature(...)-100%100%
BuildExpiresOn(...)-100%100%
Sign(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\iot\Azure.Iot.Hub.Service\src\Authentication\SharedAccessSignatureBuilder.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Globalization;
 7using System.Net;
 8using System.Security.Cryptography;
 9using System.Text;
 10
 11namespace Azure.Iot.Hub.Service.Authentication
 12{
 13    /// <summary>
 14    /// Builds the shared access signature based on the access policy passed.
 15    /// </summary>
 16    internal class SharedAccessSignatureBuilder
 17    {
 16418        internal string SharedAccessPolicy { get; set; }
 19
 16420        internal string SharedAccessKey { get; set; }
 21
 16422        internal string HostName { get; set; }
 23
 16424        internal TimeSpan TimeToLive { get; set; }
 25
 26        internal string ToSignature()
 27        {
 8228            return BuildSignature(SharedAccessPolicy, SharedAccessKey, HostName, TimeToLive);
 29        }
 30
 31        private static string BuildSignature(string sharedAccessPolicy, string sharedAccessKey, string hostName, TimeSpa
 32        {
 8233            string expiresOn = BuildExpiresOn(timeToLive);
 8234            string audience = WebUtility.UrlEncode(hostName);
 8235            var fields = new List<string>
 8236            {
 8237                audience,
 8238                expiresOn
 8239            };
 40
 41            // Example string to be signed:
 42            // dh://myiothub.azure-devices.net/a/b/c?myvalue1=a
 43            // <Value for ExpiresOn>
 44
 8245            string signature = Sign(string.Join("\n", fields), sharedAccessKey);
 46
 47            // Example returned string:
 48            // SharedAccessSignature sr=ENCODED(dh://myiothub.azure-devices.net/a/b/c?myvalue1=a)&sig=<Signature>&se=<Ex
 49
 8250            var buffer = new StringBuilder();
 8251            buffer.Append($"{SharedAccessSignatureConstants.SharedAccessSignatureIdentifier} " +
 8252                $"{SharedAccessSignatureConstants.AudienceFieldName}={audience}" +
 8253                $"&{SharedAccessSignatureConstants.SignatureFieldName}={WebUtility.UrlEncode(signature)}" +
 8254                $"&{SharedAccessSignatureConstants.ExpiryFieldName}={WebUtility.UrlEncode(expiresOn)}");
 55
 8256            if (!string.IsNullOrWhiteSpace(sharedAccessPolicy))
 57            {
 8258                buffer.Append($"&{SharedAccessSignatureConstants.KeyNameFieldName}={WebUtility.UrlEncode(sharedAccessPol
 59            }
 60
 8261            return buffer.ToString();
 62        }
 63
 64        private static string BuildExpiresOn(TimeSpan timeToLive)
 65        {
 8266            DateTimeOffset expiresOn = DateTimeOffset.UtcNow.Add(timeToLive);
 8267            TimeSpan secondsFromBaseTime = expiresOn.Subtract(SharedAccessSignatureConstants.s_epochTime);
 8268            long seconds = Convert.ToInt64(secondsFromBaseTime.TotalSeconds, CultureInfo.InvariantCulture);
 8269            return Convert.ToString(seconds, CultureInfo.InvariantCulture);
 70        }
 71
 72        private static string Sign(string requestString, string key)
 73        {
 8274            using (var hmac = new HMACSHA256(Convert.FromBase64String(key)))
 75            {
 8276                return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(requestString)));
 77            }
 8278        }
 79    }
 80}