Skip to content

Commit

Permalink
Removed Logging Part from the current PR as suggested.
Browse files Browse the repository at this point in the history
Logging will be done in follow up PR.
  • Loading branch information
prajon84 committed Aug 14, 2019
1 parent ebe375a commit 13676aa
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 63 deletions.
7 changes: 1 addition & 6 deletions src/Medidata.MAuth.AspNetCore/MAuthAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Medidata.MAuth.AspNetCore
{
Expand All @@ -29,10 +26,8 @@ public static IApplicationBuilder UseMAuthAuthentication(this IApplicationBuilde
if (options == null)
throw new ArgumentNullException(nameof(options));

var loggerFactory = app.ApplicationServices.GetService<ILoggerFactory>() ??
NullLoggerFactory.Instance;

return app.UseMiddleware<MAuthMiddleware>(options, loggerFactory);
return app.UseMiddleware<MAuthMiddleware>(options);
}

/// <summary>
Expand Down
5 changes: 2 additions & 3 deletions src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Medidata.MAuth.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.Extensions.Logging;

namespace Medidata.MAuth.AspNetCore
{
Expand All @@ -13,11 +12,11 @@ internal class MAuthMiddleware
private readonly MAuthAuthenticator authenticator;
private readonly RequestDelegate next;

public MAuthMiddleware(RequestDelegate next, MAuthMiddlewareOptions options, ILoggerFactory loggerFactory)
public MAuthMiddleware(RequestDelegate next, MAuthMiddlewareOptions options)
{
this.next = next;
this.options = options;
this.authenticator = new MAuthAuthenticator(options, loggerFactory);
this.authenticator = new MAuthAuthenticator(options);
}

public async Task Invoke(HttpContext context)
Expand Down
15 changes: 2 additions & 13 deletions src/Medidata.MAuth.Core/MAuthAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
using Microsoft.Extensions.Caching.Memory;
using Org.BouncyCastle.Crypto;
using Medidata.MAuth.Core.Models;
using Microsoft.Extensions.Logging;

namespace Medidata.MAuth.Core
{
internal class MAuthAuthenticator
{
private readonly MAuthOptionsBase options;
private readonly IMemoryCache cache = new MemoryCache(new MemoryCacheOptions());
private readonly ILogger _logger;

public Guid ApplicationUuid => options.ApplicationUuid;

public MAuthAuthenticator(MAuthOptionsBase options, ILoggerFactory loggerFactory)
public MAuthAuthenticator(MAuthOptionsBase options)
{
if (options.ApplicationUuid == default(Guid))
throw new ArgumentException(nameof(options.ApplicationUuid));
Expand All @@ -29,8 +27,6 @@ public MAuthAuthenticator(MAuthOptionsBase options, ILoggerFactory loggerFactory
throw new ArgumentNullException(nameof(options.PrivateKey));

this.options = options;

this._logger = loggerFactory.CreateLogger<MAuthAuthenticator>();
}

/// <summary>
Expand All @@ -42,11 +38,8 @@ public async Task<bool> AuthenticateRequest(HttpRequestMessage request)
{
try
{
_logger.LogInformation($"Initiating Authentication of request", request);
var version = request.GetAuthHeaderValue().GetVersionFromAuthenticationHeader();

_logger.LogInformation($"Authentication is for the request with {version} version.");

if (options.DisableV1 && version == MAuthVersion.MWS)
throw new InvalidVersionException($"Authentication with {version} version is disabled.");

Expand All @@ -59,29 +52,25 @@ public async Task<bool> AuthenticateRequest(HttpRequestMessage request)
}
catch (ArgumentException ex)
{
_logger.LogError($"Unable to authenticate due to invalid MAuth authentication headers. Exception: {ex.Message}");
throw new AuthenticationException("The request has invalid MAuth authentication headers.", ex);
}
catch (RetriedRequestException ex)
{
_logger.LogError($"Unable to query the application information from MAuth server. Exception:{ex.Message}");
throw new AuthenticationException(
"Could not query the application information for the application from the MAuth server.", ex);
}
catch (InvalidCipherTextException ex)
{
_logger.LogError($"Unable to authenticate due to invalid payload information. Exception: {ex.Message}");

throw new AuthenticationException(
"The request verification failed due to an invalid payload information.", ex);
}
catch (InvalidVersionException ex)
{
_logger.LogError(ex, $"Unable to authenticate due to invalid version. Exception: {ex.Message}");
throw new InvalidVersionException(ex.Message, ex);
}
catch (Exception ex)
{
_logger.LogError($"Unable to authenticate due to unexpected error. Exception: {ex.Message}");
throw new AuthenticationException(
"An unexpected error occured during authentication. Please see the inner exception for details.",
ex
Expand Down
6 changes: 2 additions & 4 deletions src/Medidata.MAuth.Core/UtilityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Net.Http;
using System.Threading.Tasks;
using Medidata.MAuth.Core.Models;
using Microsoft.Extensions.Logging;

namespace Medidata.MAuth.Core
{
Expand Down Expand Up @@ -66,11 +65,10 @@ public static bool TryParseAuthenticationHeader(this string headerValue,
/// </summary>
/// <param name="request">The request message to authenticate.</param>
/// <param name="options">The MAuth options to use for the authentication.</param>
/// <param name="loggerFactory">The logger factory used with authentication.</param>
/// <returns>The task for the operation that is when completes will result in <see langword="true"/> if
/// the authentication is successful; otherwise <see langword="false"/>.</returns>
public static Task<bool> Authenticate(this HttpRequestMessage request, MAuthOptionsBase options, ILoggerFactory loggerFactory) =>
new MAuthAuthenticator(options, loggerFactory).AuthenticateRequest(request);
public static Task<bool> Authenticate(this HttpRequestMessage request, MAuthOptionsBase options) =>
new MAuthAuthenticator(options).AuthenticateRequest(request);

/// <summary>
/// Determines the MAuth version enumerator reading authHeader.
Expand Down
8 changes: 1 addition & 7 deletions src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Linq;
using Microsoft.Extensions.Logging.Abstractions;
using Owin;
using Microsoft.Extensions.Logging;

namespace Medidata.MAuth.Owin
{
Expand All @@ -28,10 +25,7 @@ public static IAppBuilder UseMAuthAuthentication(this IAppBuilder app, MAuthMidd
if (options == null)
throw new ArgumentNullException(nameof(options));

var loggerFactory = (ILoggerFactory)app.Properties.Where(x => x.Key == "ILoggerFactory")?
.FirstOrDefault().Value ?? NullLoggerFactory.Instance;

return app.Use<MAuthMiddleware>(options, loggerFactory);
return app.Use<MAuthMiddleware>(options);
}

/// <summary>
Expand Down
5 changes: 2 additions & 3 deletions src/Medidata.MAuth.Owin/MAuthMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Net;
using System.Threading.Tasks;
using Medidata.MAuth.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Owin;

namespace Medidata.MAuth.Owin
Expand All @@ -11,10 +10,10 @@ internal class MAuthMiddleware: OwinMiddleware
private readonly MAuthMiddlewareOptions options;
private readonly MAuthAuthenticator authenticator;

public MAuthMiddleware(OwinMiddleware next, MAuthMiddlewareOptions options, ILoggerFactory loggerFactory): base(next)
public MAuthMiddleware(OwinMiddleware next, MAuthMiddlewareOptions options): base(next)
{
this.options = options;
authenticator = new MAuthAuthenticator(options, loggerFactory);
authenticator = new MAuthAuthenticator(options);
}

public override async Task Invoke(IOwinContext context)
Expand Down
15 changes: 4 additions & 11 deletions src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Threading;
using System.Threading.Tasks;
using Medidata.MAuth.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Medidata.MAuth.WebApi
{
Expand All @@ -26,13 +24,11 @@ public class MAuthAuthenticatingHandler : DelegatingHandler
/// <see cref="MAuthWebApiOptions"/>.
/// </summary>
/// <param name="options">The options for this message handler.</param>
/// <param name="loggerFactory">The logger factory used by this message handler.</param>
public MAuthAuthenticatingHandler(MAuthWebApiOptions options, ILoggerFactory loggerFactory = null)
public MAuthAuthenticatingHandler(MAuthWebApiOptions options)
{
this.options = options;
var loggerFact = loggerFactory ?? NullLoggerFactory.Instance;

authenticator = new MAuthAuthenticator(options, loggerFact);
authenticator = new MAuthAuthenticator(options);
}

/// <summary>
Expand All @@ -43,13 +39,10 @@ public MAuthAuthenticatingHandler(MAuthWebApiOptions options, ILoggerFactory log
/// <param name="innerHandler">
/// The inner handler which is responsible for processing the HTTP response messages.
/// </param>
/// <param name="loggerFactory">The logger factory used by this message handler.</param>
public MAuthAuthenticatingHandler(MAuthWebApiOptions options, HttpMessageHandler innerHandler,
ILoggerFactory loggerFactory = null) : base(innerHandler)
public MAuthAuthenticatingHandler(MAuthWebApiOptions options, HttpMessageHandler innerHandler) : base(innerHandler)
{
this.options = options;
var loggerFact = loggerFactory ?? NullLoggerFactory.Instance;
authenticator = new MAuthAuthenticator(options, loggerFact);
authenticator = new MAuthAuthenticator(options);
}

/// <summary>
Expand Down
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 Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;

namespace Medidata.MAuth.Tests.Infrastructure
Expand All @@ -26,7 +25,7 @@ protected override async Task<HttpResponseMessage> SendAsync(
if (currentNumberOfAttempts < SucceedAfterThisManyAttempts)
return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);

var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLoggerFactory.Instance);
var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions);

var authInfo = authenticator.GetAuthenticationInfo(request, version);

Expand Down
23 changes: 11 additions & 12 deletions tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Medidata.MAuth.Core.Exceptions;
using Medidata.MAuth.Core.Models;
using Medidata.MAuth.Tests.Infrastructure;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;

namespace Medidata.MAuth.Tests
Expand All @@ -23,14 +22,14 @@ public static void MAuthAuthenticator_WithInvalidOptions_WillThrowException(
ApplicationUuid = TestExtensions.ClientUuid,
MAuthServiceUrl = mauthServiceUrl != null ? new Uri(mauthServiceUrl) : null,
PrivateKey = privateKey
}, NullLoggerFactory.Instance));
}));

[Fact]
public static void MAuthAuthenticator_WithDefaultUuid_WillThrowException() =>
Assert.Throws<ArgumentException>(() => new MAuthAuthenticator(new MAuthTestOptions()
{
ApplicationUuid = default(Guid)
}, NullLoggerFactory.Instance));
}));

[Theory]
[InlineData("GET")]
Expand All @@ -42,7 +41,7 @@ public static async Task AuthenticateRequest_WithValidRequest_WillAuthenticate(s
// Arrange
var testData = await method.FromResource();

var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLoggerFactory.Instance);
var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions);
var mAuthCore = new MAuthCore();

var signedRequest = await mAuthCore
Expand Down Expand Up @@ -70,7 +69,7 @@ public static async Task AuthenticateRequest_WithValidMWSV2Request_WillAuthentic
// Arrange
var testData = await method.FromResourceV2();
var version = MAuthVersion.MWSV2;
var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLoggerFactory.Instance);
var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions);
var mAuthCore = new MAuthCoreV2();

var signedRequest = await mAuthCore
Expand Down Expand Up @@ -101,7 +100,7 @@ public static async Task AuthenticateRequest_WithNumberOfAttempts_WillAuthentica
var testData = await "GET".FromResource();

var authenticator = new MAuthAuthenticator(TestExtensions.GetServerOptionsWithAttempts(
policy, shouldSucceedWithin: true), NullLoggerFactory.Instance);
policy, shouldSucceedWithin: true));
var mAuthCore = new MAuthCore();

var signedRequest = await mAuthCore
Expand Down Expand Up @@ -131,7 +130,7 @@ public static async Task AuthenticateRequest_WithMWSV2Request_WithNumberOfAttemp
var testData = await "GET".FromResourceV2();
var version = MAuthVersion.MWSV2;
var authenticator = new MAuthAuthenticator(TestExtensions.GetServerOptionsWithAttempts(
policy, shouldSucceedWithin: true), NullLoggerFactory.Instance);
policy, shouldSucceedWithin: true));
var mAuthCore = new MAuthCoreV2();

var signedRequest = await mAuthCore
Expand Down Expand Up @@ -161,7 +160,7 @@ public static async Task AuthenticateRequest_AfterNumberOfAttempts_WillThrowExce
var testData = await "GET".FromResource();

var authenticator = new MAuthAuthenticator(TestExtensions.GetServerOptionsWithAttempts(
policy, shouldSucceedWithin: false), NullLoggerFactory.Instance);
policy, shouldSucceedWithin: false));
var mAuthCore = new MAuthCore();

var signedRequest = await mAuthCore
Expand Down Expand Up @@ -196,7 +195,7 @@ public static async Task AuthenticateRequest_WithMWSV2Request_AfterNumberOfAttem
var testData = await "GET".FromResource();
var version = MAuthVersion.MWSV2;
var authenticator = new MAuthAuthenticator(TestExtensions.GetServerOptionsWithAttempts(
policy, shouldSucceedWithin: false), NullLoggerFactory.Instance);
policy, shouldSucceedWithin: false));
var mAuthCore = new MAuthCoreV2();

var signedRequest = await mAuthCore
Expand Down Expand Up @@ -278,7 +277,7 @@ public static async Task AuthenticateRequest_WithMWSVersion_WithDisableV1_WillTh
var testData = await method.FromResource();
var testOptions = TestExtensions.ServerOptions;
testOptions.DisableV1 = true;
var authenticator = new MAuthAuthenticator(testOptions, NullLoggerFactory.Instance);
var authenticator = new MAuthAuthenticator(testOptions);
var mAuthCore = new MAuthCore();

var signedRequest = await mAuthCore
Expand Down Expand Up @@ -309,7 +308,7 @@ 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, NullLoggerFactory.Instance);
var authenticator = new MAuthAuthenticator(testOptions);

// Act
var actual = authenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), version);
Expand All @@ -331,7 +330,7 @@ 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, NullLoggerFactory.Instance);
var authenticator = new MAuthAuthenticator(testOptions);

// Act
var actual = authenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), version);
Expand Down
3 changes: 1 addition & 2 deletions tests/Medidata.MAuth.Tests/UtilityExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Threading.Tasks;
using Medidata.MAuth.Core;
using Medidata.MAuth.Tests.Infrastructure;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;

namespace Medidata.MAuth.Tests
Expand Down Expand Up @@ -68,7 +67,7 @@ public static async Task Authenticate_WithValidRequest_WillAuthenticate(string m
});

// Act
var isAuthenticated = await signedRequest.Authenticate(TestExtensions.ServerOptions, NullLoggerFactory.Instance);
var isAuthenticated = await signedRequest.Authenticate(TestExtensions.ServerOptions);

// Assert
Assert.True(isAuthenticated);
Expand Down

0 comments on commit 13676aa

Please sign in to comment.