diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs index 271b3ef..9cb1bf7 100644 --- a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs +++ b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs @@ -123,37 +123,41 @@ private Task GetApplicationInfo(Guid applicationUuid) => return result; }); - private HttpRequestMessage CreateRequest(Guid applicationUuid) => - new HttpRequestMessage(HttpMethod.Get, new Uri(options.MAuthServiceUrl, - $"{Constants.MAuthTokenRequestPath}{applicationUuid.ToHyphenString()}.json")); - /// /// Extracts the authentication information from a . /// /// The request that has the authentication information. /// Instantiation of mAuthCore class. /// The authentication information with the payload from the request. - internal PayloadAuthenticationInfo GetAuthenticationInfo(HttpRequestMessage request, IMAuthCore mAuthCore) + internal static PayloadAuthenticationInfo GetAuthenticationInfo(HttpRequestMessage request, IMAuthCore mAuthCore) { var headerKeys = mAuthCore.GetHeaderKeys(); var authHeader = request.Headers.GetFirstValueOrDefault(headerKeys.mAuthHeaderKey); if (authHeader == null) + { throw new ArgumentNullException(nameof(authHeader), "The MAuth header is missing from the request."); + } var signedTime = request.Headers.GetFirstValueOrDefault(headerKeys.mAuthTimeHeaderKey); if (signedTime == default(long)) + { throw new ArgumentException("Invalid MAuth signed time header value.", nameof(signedTime)); + } var (uuid, payload) = authHeader.ParseAuthenticationHeader(); - return new PayloadAuthenticationInfo() + return new PayloadAuthenticationInfo { ApplicationUuid = uuid, Payload = Convert.FromBase64String(payload), SignedTime = signedTime.FromUnixTimeSeconds() }; } + + private HttpRequestMessage CreateRequest(Guid applicationUuid) => + new HttpRequestMessage(HttpMethod.Get, new Uri(options.MAuthServiceUrl, + $"{Constants.MAuthTokenRequestPath}{applicationUuid.ToHyphenString()}.json")); } } diff --git a/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs b/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs index c0b4e8c..3499c89 100644 --- a/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs +++ b/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs @@ -4,7 +4,6 @@ using System.Threading; using System.Threading.Tasks; using Medidata.MAuth.Core; -using Medidata.MAuth.Core.Models; using Microsoft.Extensions.Logging.Abstractions; using Newtonsoft.Json; @@ -26,10 +25,7 @@ protected override async Task SendAsync( if (currentNumberOfAttempts < SucceedAfterThisManyAttempts) return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable); - - var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLogger.Instance); - - var authInfo = authenticator.GetAuthenticationInfo(request, mAuthCore); + var authInfo = MAuthAuthenticator.GetAuthenticationInfo(request, mAuthCore); if (!mAuthCore.Verify(authInfo.Payload, await mAuthCore.GetSignature(request, authInfo), diff --git a/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs b/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs index a780d2d..b112142 100644 --- a/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs +++ b/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs @@ -311,11 +311,10 @@ public static async Task GetAuthenticationInfo_WithSignedRequest_ForMWSV2Version var testData = await method.FromResourceV2(); var version = MAuthVersion.MWSV2; var testOptions = TestExtensions.ServerOptions; - var authenticator = new MAuthAuthenticator(testOptions, NullLogger.Instance); var mAuthCore = MAuthCoreFactory.Instantiate(version); // Act - var actual = authenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), mAuthCore); + var actual = MAuthAuthenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), mAuthCore); // Assert Assert.Equal(testData.ApplicationUuid, actual.ApplicationUuid); @@ -334,11 +333,10 @@ public static async Task GetAuthenticationInfo_WithSignedRequest_ForMWSVersion_W var testData = await method.FromResource(); var version = MAuthVersion.MWS; var testOptions = TestExtensions.ServerOptions; - var authenticator = new MAuthAuthenticator(testOptions, NullLogger.Instance); var mAuthCore = MAuthCoreFactory.Instantiate(version); // Act - var actual = authenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), mAuthCore); + var actual = MAuthAuthenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), mAuthCore); // Assert Assert.Equal(testData.ApplicationUuid, actual.ApplicationUuid);