diff --git a/src/Medidata.MAuth.Core/MAuthCoreExtensions.cs b/src/Medidata.MAuth.Core/MAuthCoreExtensions.cs index 1a48dc5..3217da7 100644 --- a/src/Medidata.MAuth.Core/MAuthCoreExtensions.cs +++ b/src/Medidata.MAuth.Core/MAuthCoreExtensions.cs @@ -8,7 +8,6 @@ using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; -using static Medidata.MAuth.Core.Constants; using Newtonsoft.Json.Linq; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Encodings; @@ -76,10 +75,11 @@ public static bool Verify(this byte[] signedData, byte[] signature, string publi public static async Task GetSignature(this HttpRequestMessage request, AuthenticationInfo authInfo) => new byte[][] { - request.Method.Method.ToBytes(), NewLine, - request.RequestUri.AbsolutePath.ToBytes(), NewLine, - (request.Content != null ? await request.Content.ReadAsByteArrayAsync() : new byte[] { }), NewLine, - authInfo.ApplicationUuid.ToHyphenString().ToBytes(), NewLine, + request.Method.Method.ToBytes(), Constants.NewLine, + request.RequestUri.AbsolutePath.ToBytes(), Constants.NewLine, + (request.Content != null ? await request.Content.ReadAsByteArrayAsync() : new byte[] { }), + Constants.NewLine, + authInfo.ApplicationUuid.ToHyphenString().ToBytes(), Constants.NewLine, authInfo.SignedTime.ToUnixTimeSeconds().ToString().ToBytes() } .Concat() @@ -129,8 +129,8 @@ public async static Task AddAuthenticationInfo( $"MWS {authInfo.ApplicationUuid.ToHyphenString()}:" + $"{await request.CalculatePayload(authInfo)}"; - request.Headers.Add(MAuthHeaderKey, authHeader); - request.Headers.Add(MAuthTimeHeaderKey, authInfo.SignedTime.ToUnixTimeSeconds().ToString()); + request.Headers.Add(Constants.MAuthHeaderKey, authHeader); + request.Headers.Add(Constants.MAuthTimeHeaderKey, authInfo.SignedTime.ToUnixTimeSeconds().ToString()); return request; } @@ -170,7 +170,7 @@ public async static Task FromResponse(this HttpContent content) /// The date and time to be converted. /// The total seconds elapsed from the Unix epoch (1970-01-01 00:00:00). public static long ToUnixTimeSeconds(this DateTimeOffset value) => - (long)(value - UnixEpoch).TotalSeconds; + (long)(value - Constants.UnixEpoch).TotalSeconds; /// /// Converts a value that is a Unix time in seconds to a UTC . @@ -178,7 +178,7 @@ public static long ToUnixTimeSeconds(this DateTimeOffset value) => /// The Unix time seconds to be converted. /// The equivalent of the Unix time. public static DateTimeOffset FromUnixTimeSeconds(this long value) => - UnixEpoch.AddSeconds(value); + Constants.UnixEpoch.AddSeconds(value); /// @@ -197,14 +197,10 @@ public static string ToHyphenString(this Guid uuid) => /// The collection of the HTTP headers to search in. /// The key to search in the headers collection. /// The value if found; otherwise a default value for the given type. - public static TValue GetFirstValueOrDefault(this HttpHeaders headers, string key) - { - IEnumerable values; - - return headers.TryGetValues(key, out values) ? + public static TValue GetFirstValueOrDefault(this HttpHeaders headers, string key) => + headers.TryGetValues(key, out IEnumerable values) ? (TValue)Convert.ChangeType(values.First(), typeof(TValue)) : default(TValue); - } /// /// Provides an SHA512 hash value of a string. @@ -264,10 +260,10 @@ private static string RemoveLineBreaks(this string key) => key.Replace("\r", string.Empty).Replace("\n", string.Empty); private static string InsertLineBreakAfterBegin(this string key) => - Regex.Replace(key, KeyNormalizeLinesStartRegexPattern, "${begin}\n"); + Regex.Replace(key, Constants.KeyNormalizeLinesStartRegexPattern, "${begin}\n"); private static string InsertLineBreakBeforeEnd(this string key) => - Regex.Replace(key, KeyNormalizeLinesEndRegexPattern, "\n${end}"); + Regex.Replace(key, Constants.KeyNormalizeLinesEndRegexPattern, "\n${end}"); private static byte[] ToBytes(this string value) => Encoding.UTF8.GetBytes(value);