Skip to content

Commit

Permalink
Updated the method to be static and updated the unit tests and refere…
Browse files Browse the repository at this point in the history
…nces
  • Loading branch information
prajon84 committed Jan 25, 2020
1 parent e7ba136 commit 440f91f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
16 changes: 10 additions & 6 deletions src/Medidata.MAuth.Core/MAuthAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,37 +123,41 @@ private Task<ApplicationInfo> GetApplicationInfo(Guid applicationUuid) =>
return result;
});

private HttpRequestMessage CreateRequest(Guid applicationUuid) =>
new HttpRequestMessage(HttpMethod.Get, new Uri(options.MAuthServiceUrl,
$"{Constants.MAuthTokenRequestPath}{applicationUuid.ToHyphenString()}.json"));

/// <summary>
/// Extracts the authentication information from a <see cref="HttpRequestMessage"/>.
/// </summary>
/// <param name="request">The request that has the authentication information.</param>
/// <param name="mAuthCore">Instantiation of mAuthCore class.</param>
/// <returns>The authentication information with the payload from the request.</returns>
internal PayloadAuthenticationInfo GetAuthenticationInfo(HttpRequestMessage request, IMAuthCore mAuthCore)
internal static PayloadAuthenticationInfo GetAuthenticationInfo(HttpRequestMessage request, IMAuthCore mAuthCore)
{
var headerKeys = mAuthCore.GetHeaderKeys();
var authHeader = request.Headers.GetFirstValueOrDefault<string>(headerKeys.mAuthHeaderKey);

if (authHeader == null)
{
throw new ArgumentNullException(nameof(authHeader), "The MAuth header is missing from the request.");
}

var signedTime = request.Headers.GetFirstValueOrDefault<long>(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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,10 +25,7 @@ protected override async Task<HttpResponseMessage> SendAsync(

if (currentNumberOfAttempts < SucceedAfterThisManyAttempts)
return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);

var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLogger<MAuthAuthenticator>.Instance);

var authInfo = authenticator.GetAuthenticationInfo(request, mAuthCore);
var authInfo = MAuthAuthenticator.GetAuthenticationInfo(request, mAuthCore);

if (!mAuthCore.Verify(authInfo.Payload,
await mAuthCore.GetSignature(request, authInfo),
Expand Down
6 changes: 2 additions & 4 deletions tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MAuthAuthenticator>.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);
Expand All @@ -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<MAuthAuthenticator>.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);
Expand Down

0 comments on commit 440f91f

Please sign in to comment.