From 18c44997799c4da4ede5cb7b871195bbe2c04ad3 Mon Sep 17 00:00:00 2001 From: Prajon Date: Mon, 19 Aug 2019 13:01:56 -0400 Subject: [PATCH 01/12] Adding logging functionality during authentication. --- CHANGELOG.md | 3 +++ README.md | 7 +++++- .../MAuthAppBuilderExtensions.cs | 8 +++++-- .../MAuthMiddleware.cs | 5 ++-- src/Medidata.MAuth.Core/MAuthAuthenticator.cs | 14 +++++++++-- src/Medidata.MAuth.Core/UtilityExtensions.cs | 6 +++-- .../MAuthAppBuilderExtensions.cs | 7 ++++-- src/Medidata.MAuth.Owin/MAuthMiddleware.cs | 7 +++--- .../MAuthAuthenticatingHandler.cs | 16 +++++++++---- .../Infrastructure/MAuthServerHandler.cs | 3 ++- .../MAuthAuthenticatorTests.cs | 23 ++++++++++--------- .../UtilityExtensionsTest.cs | 3 ++- version.props | 2 +- 13 files changed, 71 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74b55db..7853654 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changes in Medidata.MAuth +## v4.0.1 +- **[All]** Added logging suuport during authentication. + ## v4.0.0 - **[All]** Added implementation for MWSV2 signinig and authentication. diff --git a/README.md b/README.md index 316c1fa..c2b5b93 100644 --- a/README.md +++ b/README.md @@ -265,6 +265,10 @@ public static class WebApiConfig }; config.MessageHandlers.Add(new MAuthAuthenticatingHandler(options)); + + // or if there is a loggerFactory in the application + var loggerFactory = // get the loggerFactory of the logger being used. + config.MessageHandlers.Add(new MAuthAuthenticatingHandler(options, loggerFactory)); } } ``` @@ -279,13 +283,14 @@ public static class WebApiConfig public static void Register(HttpConfiguration config) { var options = // See the previous example + var loggerFactory = // See the previous example config.Routes.MapHttpRoute( name: "Route1", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }, constraints: null, - handler: new MAuthAuthenticatingHandler(options) + handler: new MAuthAuthenticatingHandler(options, loggerFactory) ); } } diff --git a/src/Medidata.MAuth.AspNetCore/MAuthAppBuilderExtensions.cs b/src/Medidata.MAuth.AspNetCore/MAuthAppBuilderExtensions.cs index 35c90a1..c447a83 100644 --- a/src/Medidata.MAuth.AspNetCore/MAuthAppBuilderExtensions.cs +++ b/src/Medidata.MAuth.AspNetCore/MAuthAppBuilderExtensions.cs @@ -1,5 +1,8 @@ using System; using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace Medidata.MAuth.AspNetCore { @@ -26,8 +29,9 @@ public static IApplicationBuilder UseMAuthAuthentication(this IApplicationBuilde if (options == null) throw new ArgumentNullException(nameof(options)); - - return app.UseMiddleware(options); + var loggerFactory = app.ApplicationServices.GetService() ?? + NullLoggerFactory.Instance; + return app.UseMiddleware(options, loggerFactory); } /// diff --git a/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs b/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs index 0efdee7..69f679d 100644 --- a/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs @@ -3,6 +3,7 @@ using Medidata.MAuth.Core; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Internal; +using Microsoft.Extensions.Logging; namespace Medidata.MAuth.AspNetCore { @@ -12,11 +13,11 @@ internal class MAuthMiddleware private readonly MAuthAuthenticator authenticator; private readonly RequestDelegate next; - public MAuthMiddleware(RequestDelegate next, MAuthMiddlewareOptions options) + public MAuthMiddleware(RequestDelegate next, MAuthMiddlewareOptions options, ILoggerFactory loggerFactory) { this.next = next; this.options = options; - this.authenticator = new MAuthAuthenticator(options); + this.authenticator = new MAuthAuthenticator(options, loggerFactory); } public async Task Invoke(HttpContext context) diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs index df5eb95..d4768b5 100644 --- a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs +++ b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Caching.Memory; using Org.BouncyCastle.Crypto; using Medidata.MAuth.Core.Models; +using Microsoft.Extensions.Logging; namespace Medidata.MAuth.Core { @@ -12,10 +13,11 @@ 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) + public MAuthAuthenticator(MAuthOptionsBase options, ILoggerFactory loggerFactory) { if (options.ApplicationUuid == default(Guid)) throw new ArgumentException(nameof(options.ApplicationUuid)); @@ -27,6 +29,7 @@ public MAuthAuthenticator(MAuthOptionsBase options) throw new ArgumentNullException(nameof(options.PrivateKey)); this.options = options; + this._logger = loggerFactory.CreateLogger(); } /// @@ -38,8 +41,11 @@ public async Task 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."); @@ -52,25 +58,29 @@ public async Task 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 diff --git a/src/Medidata.MAuth.Core/UtilityExtensions.cs b/src/Medidata.MAuth.Core/UtilityExtensions.cs index 7fb474b..bc7ee05 100644 --- a/src/Medidata.MAuth.Core/UtilityExtensions.cs +++ b/src/Medidata.MAuth.Core/UtilityExtensions.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Threading.Tasks; using Medidata.MAuth.Core.Models; +using Microsoft.Extensions.Logging; namespace Medidata.MAuth.Core { @@ -65,10 +66,11 @@ public static bool TryParseAuthenticationHeader(this string headerValue, /// /// The request message to authenticate. /// The MAuth options to use for the authentication. + /// The logger factory used with authentication. /// The task for the operation that is when completes will result in if /// the authentication is successful; otherwise . - public static Task Authenticate(this HttpRequestMessage request, MAuthOptionsBase options) => - new MAuthAuthenticator(options).AuthenticateRequest(request); + public static Task Authenticate(this HttpRequestMessage request, MAuthOptionsBase options, ILoggerFactory loggerFactory) => + new MAuthAuthenticator(options, loggerFactory).AuthenticateRequest(request); /// /// Determines the MAuth version enumerator reading authHeader. diff --git a/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs b/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs index 5a9cd17..ef2bb5f 100644 --- a/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs +++ b/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs @@ -1,4 +1,7 @@ using System; +using System.Linq; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Owin; namespace Medidata.MAuth.Owin @@ -24,8 +27,8 @@ public static IAppBuilder UseMAuthAuthentication(this IAppBuilder app, MAuthMidd if (options == null) throw new ArgumentNullException(nameof(options)); - - return app.Use(options); + var loggerFactory = NullLoggerFactory.Instance; + return app.Use(options, loggerFactory); } /// diff --git a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs index 3883f9e..de3fe03 100644 --- a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs @@ -1,6 +1,7 @@ using System.Net; using System.Threading.Tasks; using Medidata.MAuth.Core; +using Microsoft.Extensions.Logging; using Microsoft.Owin; namespace Medidata.MAuth.Owin @@ -10,16 +11,16 @@ internal class MAuthMiddleware: OwinMiddleware private readonly MAuthMiddlewareOptions options; private readonly MAuthAuthenticator authenticator; - public MAuthMiddleware(OwinMiddleware next, MAuthMiddlewareOptions options): base(next) + public MAuthMiddleware(OwinMiddleware next, MAuthMiddlewareOptions options, ILoggerFactory loggerFactory): base(next) { this.options = options; - authenticator = new MAuthAuthenticator(options); + authenticator = new MAuthAuthenticator(options, loggerFactory); } public override async Task Invoke(IOwinContext context) { await context.EnsureRequestBodyStreamSeekable(); - + var test = context.Get("test"); if (!options.Bypass(context.Request) && !await context.TryAuthenticate(authenticator, options.HideExceptionsAndReturnUnauthorized)) { diff --git a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs index c652d69..34e84ba 100644 --- a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs +++ b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs @@ -4,6 +4,8 @@ using System.Threading; using System.Threading.Tasks; using Medidata.MAuth.Core; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace Medidata.MAuth.WebApi { @@ -24,11 +26,12 @@ public class MAuthAuthenticatingHandler : DelegatingHandler /// . /// /// The options for this message handler. - public MAuthAuthenticatingHandler(MAuthWebApiOptions options) + /// The logger factory used by this message handler. + public MAuthAuthenticatingHandler(MAuthWebApiOptions options, ILoggerFactory loggerFactory = null) { this.options = options; - - authenticator = new MAuthAuthenticator(options); + var loggerFact = loggerFactory ?? NullLoggerFactory.Instance; + authenticator = new MAuthAuthenticator(options, loggerFact); } /// @@ -39,10 +42,13 @@ public MAuthAuthenticatingHandler(MAuthWebApiOptions options) /// /// The inner handler which is responsible for processing the HTTP response messages. /// - public MAuthAuthenticatingHandler(MAuthWebApiOptions options, HttpMessageHandler innerHandler) : base(innerHandler) + /// The logger factory used by this message handler. + public MAuthAuthenticatingHandler(MAuthWebApiOptions options, HttpMessageHandler innerHandler, + ILoggerFactory loggerFactory = null) : base(innerHandler) { this.options = options; - authenticator = new MAuthAuthenticator(options); + var loggerFact = loggerFactory ?? NullLoggerFactory.Instance; + authenticator = new MAuthAuthenticator(options, loggerFact); } /// diff --git a/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs b/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs index 2d497a3..0515bb5 100644 --- a/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs +++ b/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using Medidata.MAuth.Core; +using Microsoft.Extensions.Logging.Abstractions; using Newtonsoft.Json; namespace Medidata.MAuth.Tests.Infrastructure @@ -25,7 +26,7 @@ protected override async Task SendAsync( if (currentNumberOfAttempts < SucceedAfterThisManyAttempts) return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable); - var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions); + var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLoggerFactory.Instance); var authInfo = authenticator.GetAuthenticationInfo(request, version); diff --git a/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs b/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs index 442924e..82d5bde 100644 --- a/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs +++ b/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs @@ -6,6 +6,7 @@ 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 @@ -22,14 +23,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(() => new MAuthAuthenticator(new MAuthTestOptions() { ApplicationUuid = default(Guid) - })); + }, NullLoggerFactory.Instance)); [Theory] [InlineData("GET")] @@ -41,7 +42,7 @@ public static async Task AuthenticateRequest_WithValidRequest_WillAuthenticate(s // Arrange var testData = await method.FromResource(); - var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions); + var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLoggerFactory.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -69,7 +70,7 @@ public static async Task AuthenticateRequest_WithValidMWSV2Request_WillAuthentic // Arrange var testData = await method.FromResourceV2(); var version = MAuthVersion.MWSV2; - var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions); + var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLoggerFactory.Instance); var mAuthCore = new MAuthCoreV2(); var signedRequest = await mAuthCore @@ -100,7 +101,7 @@ public static async Task AuthenticateRequest_WithNumberOfAttempts_WillAuthentica var testData = await "GET".FromResource(); var authenticator = new MAuthAuthenticator(TestExtensions.GetServerOptionsWithAttempts( - policy, shouldSucceedWithin: true)); + policy, shouldSucceedWithin: true), NullLoggerFactory.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -130,7 +131,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)); + policy, shouldSucceedWithin: true), NullLoggerFactory.Instance); var mAuthCore = new MAuthCoreV2(); var signedRequest = await mAuthCore @@ -160,7 +161,7 @@ public static async Task AuthenticateRequest_AfterNumberOfAttempts_WillThrowExce var testData = await "GET".FromResource(); var authenticator = new MAuthAuthenticator(TestExtensions.GetServerOptionsWithAttempts( - policy, shouldSucceedWithin: false)); + policy, shouldSucceedWithin: false), NullLoggerFactory.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -195,7 +196,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)); + policy, shouldSucceedWithin: false), NullLoggerFactory.Instance); var mAuthCore = new MAuthCoreV2(); var signedRequest = await mAuthCore @@ -277,7 +278,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); + var authenticator = new MAuthAuthenticator(testOptions, NullLoggerFactory.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -308,7 +309,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); + var authenticator = new MAuthAuthenticator(testOptions, NullLoggerFactory.Instance); // Act var actual = authenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), version); @@ -330,7 +331,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); + var authenticator = new MAuthAuthenticator(testOptions, NullLoggerFactory.Instance); // Act var actual = authenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), version); diff --git a/tests/Medidata.MAuth.Tests/UtilityExtensionsTest.cs b/tests/Medidata.MAuth.Tests/UtilityExtensionsTest.cs index 432f3dc..d07c67b 100644 --- a/tests/Medidata.MAuth.Tests/UtilityExtensionsTest.cs +++ b/tests/Medidata.MAuth.Tests/UtilityExtensionsTest.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using Medidata.MAuth.Core; using Medidata.MAuth.Tests.Infrastructure; +using Microsoft.Extensions.Logging.Abstractions; using Xunit; namespace Medidata.MAuth.Tests @@ -67,7 +68,7 @@ public static async Task Authenticate_WithValidRequest_WillAuthenticate(string m }); // Act - var isAuthenticated = await signedRequest.Authenticate(TestExtensions.ServerOptions); + var isAuthenticated = await signedRequest.Authenticate(TestExtensions.ServerOptions, NullLoggerFactory.Instance); // Assert Assert.True(isAuthenticated); diff --git a/version.props b/version.props index ceb060e..4716aab 100644 --- a/version.props +++ b/version.props @@ -1,6 +1,6 @@  - 4.0.0 + 4.0.1 From 4fb1ae1b46f16d734ad91f864c61a8ab60f9a0c2 Mon Sep 17 00:00:00 2001 From: Prajon Date: Tue, 20 Aug 2019 10:51:35 -0400 Subject: [PATCH 02/12] Updated feedback --- .../MAuthAppBuilderExtensions.cs | 7 +------ .../MAuthMiddleware.cs | 17 ++++++++++++++++- src/Medidata.MAuth.Core/MAuthAuthenticator.cs | 14 +++++++------- .../MAuthAppBuilderExtensions.cs | 7 ++----- src/Medidata.MAuth.Owin/MAuthMiddleware.cs | 7 +++---- .../MAuthAuthenticatingHandler.cs | 6 ++---- 6 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/Medidata.MAuth.AspNetCore/MAuthAppBuilderExtensions.cs b/src/Medidata.MAuth.AspNetCore/MAuthAppBuilderExtensions.cs index c447a83..84a8192 100644 --- a/src/Medidata.MAuth.AspNetCore/MAuthAppBuilderExtensions.cs +++ b/src/Medidata.MAuth.AspNetCore/MAuthAppBuilderExtensions.cs @@ -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 { @@ -29,9 +26,7 @@ public static IApplicationBuilder UseMAuthAuthentication(this IApplicationBuilde if (options == null) throw new ArgumentNullException(nameof(options)); - var loggerFactory = app.ApplicationServices.GetService() ?? - NullLoggerFactory.Instance; - return app.UseMiddleware(options, loggerFactory); + return app.UseMiddleware(options); } /// diff --git a/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs b/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs index 69f679d..f948a48 100644 --- a/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs @@ -4,22 +4,37 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Internal; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace Medidata.MAuth.AspNetCore { + /// + /// Enables the middleware for the aspnet core applications. + /// internal class MAuthMiddleware { private readonly MAuthMiddlewareOptions options; private readonly MAuthAuthenticator authenticator; private readonly RequestDelegate next; + /// + /// Creates a new + /// + /// The representing the next middleware in the pipeline. + /// The representing the options for the middleware. + /// The representing the factory that used to create logger instances. public MAuthMiddleware(RequestDelegate next, MAuthMiddlewareOptions options, ILoggerFactory loggerFactory) { this.next = next; this.options = options; - this.authenticator = new MAuthAuthenticator(options, loggerFactory); + this.authenticator = new MAuthAuthenticator(options, loggerFactory ?? NullLoggerFactory.Instance); } + /// + /// Invokes the logic of the middleware. + /// + /// The . + /// A that completes when the middleware has completed processing. public async Task Invoke(HttpContext context) { context.Request.EnableRewind(); diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs index d4768b5..e78c14d 100644 --- a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs +++ b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs @@ -41,10 +41,10 @@ public async Task AuthenticateRequest(HttpRequestMessage request) { try { - _logger.LogInformation($"Initiating Authentication of request", request); + _logger.LogInformation("Initiating Authentication of {request}.", request); var version = request.GetAuthHeaderValue().GetVersionFromAuthenticationHeader(); - _logger.LogInformation($"Authentication is for the request with {version} version."); + _logger.LogInformation("Authentication is for {version}.", version); if (options.DisableV1 && version == MAuthVersion.MWS) throw new InvalidVersionException($"Authentication with {version} version is disabled."); @@ -58,29 +58,29 @@ public async Task AuthenticateRequest(HttpRequestMessage request) } catch (ArgumentException ex) { - _logger.LogError($"Unable to authenticate due to invalid MAuth authentication headers. Exception: {ex.Message}"); + _logger.LogError(ex, "Unable to authenticate due to invalid MAuth authentication headers."); 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}"); + _logger.LogError(ex, "Unable to query the application information from MAuth server."); 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}"); + _logger.LogError(ex, "Unable to authenticate due to invalid payload information."); 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}"); + _logger.LogError(ex, "Unable to authenticate due to invalid version."); throw new InvalidVersionException(ex.Message, ex); } catch (Exception ex) { - _logger.LogError($"Unable to authenticate due to unexpected error. Exception: {ex.Message}"); + _logger.LogError(ex, "Unable to authenticate due to unexpected error."); throw new AuthenticationException( "An unexpected error occured during authentication. Please see the inner exception for details.", ex diff --git a/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs b/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs index ef2bb5f..5a9cd17 100644 --- a/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs +++ b/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs @@ -1,7 +1,4 @@ using System; -using System.Linq; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; using Owin; namespace Medidata.MAuth.Owin @@ -27,8 +24,8 @@ public static IAppBuilder UseMAuthAuthentication(this IAppBuilder app, MAuthMidd if (options == null) throw new ArgumentNullException(nameof(options)); - var loggerFactory = NullLoggerFactory.Instance; - return app.Use(options, loggerFactory); + + return app.Use(options); } /// diff --git a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs index de3fe03..7bec6f3 100644 --- a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs @@ -1,7 +1,7 @@ using System.Net; using System.Threading.Tasks; using Medidata.MAuth.Core; -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Owin; namespace Medidata.MAuth.Owin @@ -11,16 +11,15 @@ 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, NullLoggerFactory.Instance); } public override async Task Invoke(IOwinContext context) { await context.EnsureRequestBodyStreamSeekable(); - var test = context.Get("test"); if (!options.Bypass(context.Request) && !await context.TryAuthenticate(authenticator, options.HideExceptionsAndReturnUnauthorized)) { diff --git a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs index 34e84ba..5ff8500 100644 --- a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs +++ b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs @@ -30,8 +30,7 @@ public class MAuthAuthenticatingHandler : DelegatingHandler public MAuthAuthenticatingHandler(MAuthWebApiOptions options, ILoggerFactory loggerFactory = null) { this.options = options; - var loggerFact = loggerFactory ?? NullLoggerFactory.Instance; - authenticator = new MAuthAuthenticator(options, loggerFact); + authenticator = new MAuthAuthenticator(options, loggerFactory ?? NullLoggerFactory.Instance); } /// @@ -47,8 +46,7 @@ public MAuthAuthenticatingHandler(MAuthWebApiOptions options, HttpMessageHandler ILoggerFactory loggerFactory = null) : base(innerHandler) { this.options = options; - var loggerFact = loggerFactory ?? NullLoggerFactory.Instance; - authenticator = new MAuthAuthenticator(options, loggerFact); + authenticator = new MAuthAuthenticator(options, loggerFactory ?? NullLoggerFactory.Instance); } /// From a00f128be5223c67e814d7d239ba37e09a2b690a Mon Sep 17 00:00:00 2001 From: Prajon Date: Thu, 22 Aug 2019 16:14:26 -0400 Subject: [PATCH 03/12] Feedback implementation - indents, version update revert and logging --- CHANGELOG.md | 5 +---- README.md | 6 +++--- src/Medidata.MAuth.Core/MAuthAuthenticator.cs | 4 ++-- version.props | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7853654..60d2941 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,7 @@ # Changes in Medidata.MAuth -## v4.0.1 -- **[All]** Added logging suuport during authentication. - ## v4.0.0 -- **[All]** Added implementation for MWSV2 signinig and authentication. +- **[All]** Added implementation for MWSV2 signinig and authentication. Added logging support during MAuthentication. ## v3.1.3 - **[Core]** Refactored `MAuthCoreExtensions.cs` and moved Signing and Verification method into `IMAuthCore.cs`. diff --git a/README.md b/README.md index c2b5b93..1a15198 100644 --- a/README.md +++ b/README.md @@ -266,9 +266,9 @@ public static class WebApiConfig config.MessageHandlers.Add(new MAuthAuthenticatingHandler(options)); - // or if there is a loggerFactory in the application - var loggerFactory = // get the loggerFactory of the logger being used. - config.MessageHandlers.Add(new MAuthAuthenticatingHandler(options, loggerFactory)); + // or if there is a loggerFactory in the application + var loggerFactory = // get the loggerFactory of the logger being used. + config.MessageHandlers.Add(new MAuthAuthenticatingHandler(options, loggerFactory)); } } ``` diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs index e78c14d..e22435b 100644 --- a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs +++ b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs @@ -41,7 +41,7 @@ public async Task AuthenticateRequest(HttpRequestMessage request) { try { - _logger.LogInformation("Initiating Authentication of {request}.", request); + _logger.LogInformation("Initiating Authentication of the request"); var version = request.GetAuthHeaderValue().GetVersionFromAuthenticationHeader(); _logger.LogInformation("Authentication is for {version}.", version); @@ -69,7 +69,7 @@ public async Task AuthenticateRequest(HttpRequestMessage request) } catch (InvalidCipherTextException ex) { - _logger.LogError(ex, "Unable to authenticate due to invalid payload information."); + _logger.LogWarning(ex, "Unable to authenticate due to invalid payload information."); throw new AuthenticationException( "The request verification failed due to an invalid payload information.", ex); } diff --git a/version.props b/version.props index 4716aab..ceb060e 100644 --- a/version.props +++ b/version.props @@ -1,6 +1,6 @@  - 4.0.1 + 4.0.0 From 856455139e757cbdafbc4f57ee9d33df606d1a1f Mon Sep 17 00:00:00 2001 From: Prajon Date: Tue, 27 Aug 2019 10:23:09 -0400 Subject: [PATCH 04/12] Updated for MAuth.Owin as per feedback --- README.md | 5 ++++- src/Medidata.MAuth.Owin/MAuthMiddleware.cs | 2 +- src/Medidata.MAuth.Owin/MAuthMiddlewareOptions.cs | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1a15198..4270b52 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,9 @@ public class Startup // when ready to disable authentication of V1 protococl options.DisableV1 = true; + + //or if ILoggerFactory instance available + options.LoggerFactory = // get the loggerFactory of the logger being used. }); } } @@ -283,7 +286,7 @@ public static class WebApiConfig public static void Register(HttpConfiguration config) { var options = // See the previous example - var loggerFactory = // See the previous example + var loggerFactory = // See the previous example config.Routes.MapHttpRoute( name: "Route1", diff --git a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs index 7bec6f3..fe6681d 100644 --- a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs @@ -14,7 +14,7 @@ internal class MAuthMiddleware: OwinMiddleware public MAuthMiddleware(OwinMiddleware next, MAuthMiddlewareOptions options) : base(next) { this.options = options; - authenticator = new MAuthAuthenticator(options, NullLoggerFactory.Instance); + authenticator = new MAuthAuthenticator(options, options.LoggerFactory ?? NullLoggerFactory.Instance); } public override async Task Invoke(IOwinContext context) diff --git a/src/Medidata.MAuth.Owin/MAuthMiddlewareOptions.cs b/src/Medidata.MAuth.Owin/MAuthMiddlewareOptions.cs index 96e3811..27a10be 100644 --- a/src/Medidata.MAuth.Owin/MAuthMiddlewareOptions.cs +++ b/src/Medidata.MAuth.Owin/MAuthMiddlewareOptions.cs @@ -1,5 +1,7 @@ using System; using Medidata.MAuth.Core; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Owin; namespace Medidata.MAuth.Owin @@ -26,5 +28,11 @@ public class MAuthMiddlewareOptions: MAuthOptionsBase /// the options, every request will be authenticated by default. /// public Func Bypass { get; set; } = (request) => false; + + /// + /// Determines the instance passed by the application to create the logger. + /// The default is . + /// + public ILoggerFactory LoggerFactory { get; set; } = NullLoggerFactory.Instance; } } From d3f7d33d8a31bc7fbc7551ce68b52ecb688d5b69 Mon Sep 17 00:00:00 2001 From: Prajon Date: Wed, 4 Sep 2019 20:51:31 -0400 Subject: [PATCH 05/12] Modified to use log4net for logging via OWIN and older dotnet framework --- README.md | 10 +-- .../MAuthAspNetCoreExtensions.cs | 5 +- .../MAuthMiddleware.cs | 6 +- src/Medidata.MAuth.Core/MAuthAuthenticator.cs | 77 ++++++++++++++++--- .../Medidata.MAuth.Core.csproj | 1 + src/Medidata.MAuth.Core/UtilityExtensions.cs | 2 +- src/Medidata.MAuth.Owin/MAuthMiddleware.cs | 3 +- .../MAuthMiddlewareOptions.cs | 8 -- .../Medidata.MAuth.Owin.csproj | 4 + .../MAuthAuthenticatingHandler.cs | 13 +--- .../Infrastructure/MAuthServerHandler.cs | 2 +- .../MAuthAuthenticatorTests.cs | 23 +++--- 12 files changed, 98 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 4270b52..316c1fa 100644 --- a/README.md +++ b/README.md @@ -174,9 +174,6 @@ public class Startup // when ready to disable authentication of V1 protococl options.DisableV1 = true; - - //or if ILoggerFactory instance available - options.LoggerFactory = // get the loggerFactory of the logger being used. }); } } @@ -268,10 +265,6 @@ public static class WebApiConfig }; config.MessageHandlers.Add(new MAuthAuthenticatingHandler(options)); - - // or if there is a loggerFactory in the application - var loggerFactory = // get the loggerFactory of the logger being used. - config.MessageHandlers.Add(new MAuthAuthenticatingHandler(options, loggerFactory)); } } ``` @@ -286,14 +279,13 @@ public static class WebApiConfig public static void Register(HttpConfiguration config) { var options = // See the previous example - var loggerFactory = // See the previous example config.Routes.MapHttpRoute( name: "Route1", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }, constraints: null, - handler: new MAuthAuthenticatingHandler(options, loggerFactory) + handler: new MAuthAuthenticatingHandler(options) ); } } diff --git a/src/Medidata.MAuth.AspNetCore/MAuthAspNetCoreExtensions.cs b/src/Medidata.MAuth.AspNetCore/MAuthAspNetCoreExtensions.cs index 7be333b..78d9c38 100644 --- a/src/Medidata.MAuth.AspNetCore/MAuthAspNetCoreExtensions.cs +++ b/src/Medidata.MAuth.AspNetCore/MAuthAspNetCoreExtensions.cs @@ -5,6 +5,7 @@ using Medidata.MAuth.Core; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Extensions; +using Microsoft.Extensions.Logging; namespace Medidata.MAuth.AspNetCore { @@ -45,11 +46,11 @@ public static HttpRequestMessage ToHttpRequestMessage(this HttpRequest request) /// will throw an exception if any errors occurred during the authentication. /// public static async Task TryAuthenticate( - this HttpContext context, MAuthAuthenticator authenticator, bool shouldIgnoreExceptions) + this HttpContext context, MAuthAuthenticator authenticator, bool shouldIgnoreExceptions, ILoggerFactory loggerFactory) { try { - return await authenticator.AuthenticateRequest(context.Request.ToHttpRequestMessage()); + return await authenticator.AuthenticateRequest(context.Request.ToHttpRequestMessage(), loggerFactory); } catch (Exception) { diff --git a/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs b/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs index f948a48..b370b9e 100644 --- a/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs @@ -16,6 +16,7 @@ internal class MAuthMiddleware private readonly MAuthMiddlewareOptions options; private readonly MAuthAuthenticator authenticator; private readonly RequestDelegate next; + private readonly ILoggerFactory loggerFactory; /// /// Creates a new @@ -27,7 +28,8 @@ public MAuthMiddleware(RequestDelegate next, MAuthMiddlewareOptions options, ILo { this.next = next; this.options = options; - this.authenticator = new MAuthAuthenticator(options, loggerFactory ?? NullLoggerFactory.Instance); + this.loggerFactory = loggerFactory ?? NullLoggerFactory.Instance; + this.authenticator = new MAuthAuthenticator(options); //, loggerFactory ?? NullLoggerFactory.Instance); } /// @@ -40,7 +42,7 @@ public async Task Invoke(HttpContext context) context.Request.EnableRewind(); if (!options.Bypass(context.Request) && - !await context.TryAuthenticate(authenticator, options.HideExceptionsAndReturnUnauthorized)) + !await context.TryAuthenticate(authenticator, options.HideExceptionsAndReturnUnauthorized, loggerFactory)) { context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; return; diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs index e22435b..e02bfdd 100644 --- a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs +++ b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs @@ -6,6 +6,7 @@ using Org.BouncyCastle.Crypto; using Medidata.MAuth.Core.Models; using Microsoft.Extensions.Logging; +using log4net; namespace Medidata.MAuth.Core { @@ -13,11 +14,10 @@ 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)); @@ -29,22 +29,79 @@ public MAuthAuthenticator(MAuthOptionsBase options, ILoggerFactory loggerFactory throw new ArgumentNullException(nameof(options.PrivateKey)); this.options = options; - this._logger = loggerFactory.CreateLogger(); } /// /// Verifies if the request is authenticated or not. /// /// The request. + /// The used with authentication. + /// A task object of the boolean value that verifies if the request is authenticated or not. + public async Task AuthenticateRequest(HttpRequestMessage request, ILoggerFactory loggerFactory) + { + var logger = loggerFactory.CreateLogger(); + try + { + logger.LogInformation("Initiating Authentication of the request"); + var version = request.GetAuthHeaderValue().GetVersionFromAuthenticationHeader(); + + logger.LogInformation("Authentication is for {version}.", version); + + if (options.DisableV1 && version == MAuthVersion.MWS) + throw new InvalidVersionException($"Authentication with {version} version is disabled."); + + var mAuthCore = MAuthCoreFactory.Instantiate(version); + var authInfo = GetAuthenticationInfo(request, version); + var appInfo = await GetApplicationInfo(authInfo.ApplicationUuid, version); + + return mAuthCore.Verify(authInfo.Payload, await mAuthCore.GetSignature(request, authInfo), + appInfo.PublicKey); + } + catch (ArgumentException ex) + { + logger.LogError(ex, "Unable to authenticate due to invalid MAuth authentication headers."); + throw new AuthenticationException("The request has invalid MAuth authentication headers.", ex); + } + catch (RetriedRequestException ex) + { + logger.LogError(ex, "Unable to query the application information from MAuth server."); + throw new AuthenticationException( + "Could not query the application information for the application from the MAuth server.", ex); + } + catch (InvalidCipherTextException ex) + { + logger.LogWarning(ex, "Unable to authenticate due to invalid payload information."); + 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."); + throw new InvalidVersionException(ex.Message, ex); + } + catch (Exception ex) + { + logger.LogError(ex, "Unable to authenticate due to unexpected error."); + throw new AuthenticationException( + "An unexpected error occured during authentication. Please see the inner exception for details.", + ex + ); + } + } + /// + /// Verifies if the request is authenticated or not. + /// + /// The request. /// A task object of the boolean value that verifies if the request is authenticated or not. public async Task AuthenticateRequest(HttpRequestMessage request) { + var logger = LogManager.GetLogger(typeof(MAuthAuthenticator)); try { - _logger.LogInformation("Initiating Authentication of the request"); + logger.Info("Initiating Authentication of the request"); var version = request.GetAuthHeaderValue().GetVersionFromAuthenticationHeader(); - _logger.LogInformation("Authentication is for {version}.", version); + logger.InfoFormat("Authentication is for the {0}.", version); if (options.DisableV1 && version == MAuthVersion.MWS) throw new InvalidVersionException($"Authentication with {version} version is disabled."); @@ -58,29 +115,29 @@ public async Task AuthenticateRequest(HttpRequestMessage request) } catch (ArgumentException ex) { - _logger.LogError(ex, "Unable to authenticate due to invalid MAuth authentication headers."); + logger.Error("Unable to authenticate due to invalid MAuth authentication headers.", ex); throw new AuthenticationException("The request has invalid MAuth authentication headers.", ex); } catch (RetriedRequestException ex) { - _logger.LogError(ex, "Unable to query the application information from MAuth server."); + logger.Error("Unable to query the application information from MAuth server.", ex); throw new AuthenticationException( "Could not query the application information for the application from the MAuth server.", ex); } catch (InvalidCipherTextException ex) { - _logger.LogWarning(ex, "Unable to authenticate due to invalid payload information."); + logger.Warn("Unable to authenticate due to invalid payload information.", ex); 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."); + logger.Error("Unable to authenticate due to invalid version.", ex); throw new InvalidVersionException(ex.Message, ex); } catch (Exception ex) { - _logger.LogError(ex, "Unable to authenticate due to unexpected error."); + logger.Error("Unable to authenticate due to unexpected error.", ex); throw new AuthenticationException( "An unexpected error occured during authentication. Please see the inner exception for details.", ex diff --git a/src/Medidata.MAuth.Core/Medidata.MAuth.Core.csproj b/src/Medidata.MAuth.Core/Medidata.MAuth.Core.csproj index 1c15fe7..23aaadf 100644 --- a/src/Medidata.MAuth.Core/Medidata.MAuth.Core.csproj +++ b/src/Medidata.MAuth.Core/Medidata.MAuth.Core.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Medidata.MAuth.Core/UtilityExtensions.cs b/src/Medidata.MAuth.Core/UtilityExtensions.cs index bc7ee05..6c826ec 100644 --- a/src/Medidata.MAuth.Core/UtilityExtensions.cs +++ b/src/Medidata.MAuth.Core/UtilityExtensions.cs @@ -70,7 +70,7 @@ public static bool TryParseAuthenticationHeader(this string headerValue, /// The task for the operation that is when completes will result in if /// the authentication is successful; otherwise . public static Task Authenticate(this HttpRequestMessage request, MAuthOptionsBase options, ILoggerFactory loggerFactory) => - new MAuthAuthenticator(options, loggerFactory).AuthenticateRequest(request); + new MAuthAuthenticator(options).AuthenticateRequest(request, loggerFactory); /// /// Determines the MAuth version enumerator reading authHeader. diff --git a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs index fe6681d..9dc975b 100644 --- a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs @@ -1,7 +1,6 @@ using System.Net; using System.Threading.Tasks; using Medidata.MAuth.Core; -using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Owin; namespace Medidata.MAuth.Owin @@ -14,7 +13,7 @@ internal class MAuthMiddleware: OwinMiddleware public MAuthMiddleware(OwinMiddleware next, MAuthMiddlewareOptions options) : base(next) { this.options = options; - authenticator = new MAuthAuthenticator(options, options.LoggerFactory ?? NullLoggerFactory.Instance); + authenticator = new MAuthAuthenticator(options); } public override async Task Invoke(IOwinContext context) diff --git a/src/Medidata.MAuth.Owin/MAuthMiddlewareOptions.cs b/src/Medidata.MAuth.Owin/MAuthMiddlewareOptions.cs index 27a10be..96e3811 100644 --- a/src/Medidata.MAuth.Owin/MAuthMiddlewareOptions.cs +++ b/src/Medidata.MAuth.Owin/MAuthMiddlewareOptions.cs @@ -1,7 +1,5 @@ using System; using Medidata.MAuth.Core; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Owin; namespace Medidata.MAuth.Owin @@ -28,11 +26,5 @@ public class MAuthMiddlewareOptions: MAuthOptionsBase /// the options, every request will be authenticated by default. /// public Func Bypass { get; set; } = (request) => false; - - /// - /// Determines the instance passed by the application to create the logger. - /// The default is . - /// - public ILoggerFactory LoggerFactory { get; set; } = NullLoggerFactory.Instance; } } diff --git a/src/Medidata.MAuth.Owin/Medidata.MAuth.Owin.csproj b/src/Medidata.MAuth.Owin/Medidata.MAuth.Owin.csproj index a99ed86..cacb895 100644 --- a/src/Medidata.MAuth.Owin/Medidata.MAuth.Owin.csproj +++ b/src/Medidata.MAuth.Owin/Medidata.MAuth.Owin.csproj @@ -9,6 +9,10 @@ medidata;mauth;hmac;authentication;core;owin;middleware;webapi + + + + diff --git a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs index 5ff8500..daf5cb6 100644 --- a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs +++ b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs @@ -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 { @@ -26,11 +24,10 @@ public class MAuthAuthenticatingHandler : DelegatingHandler /// . /// /// The options for this message handler. - /// The logger factory used by this message handler. - public MAuthAuthenticatingHandler(MAuthWebApiOptions options, ILoggerFactory loggerFactory = null) + public MAuthAuthenticatingHandler(MAuthWebApiOptions options) { this.options = options; - authenticator = new MAuthAuthenticator(options, loggerFactory ?? NullLoggerFactory.Instance); + authenticator = new MAuthAuthenticator(options); } /// @@ -41,12 +38,10 @@ public MAuthAuthenticatingHandler(MAuthWebApiOptions options, ILoggerFactory log /// /// The inner handler which is responsible for processing the HTTP response messages. /// - /// The logger factory used by this message handler. - public MAuthAuthenticatingHandler(MAuthWebApiOptions options, HttpMessageHandler innerHandler, - ILoggerFactory loggerFactory = null) : base(innerHandler) + public MAuthAuthenticatingHandler(MAuthWebApiOptions options, HttpMessageHandler innerHandler) : base(innerHandler) { this.options = options; - authenticator = new MAuthAuthenticator(options, loggerFactory ?? NullLoggerFactory.Instance); + authenticator = new MAuthAuthenticator(options); } /// diff --git a/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs b/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs index 0515bb5..e7eb451 100644 --- a/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs +++ b/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs @@ -26,7 +26,7 @@ protected override async Task SendAsync( if (currentNumberOfAttempts < SucceedAfterThisManyAttempts) return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable); - var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLoggerFactory.Instance); + var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions);//, NullLoggerFactory.Instance); var authInfo = authenticator.GetAuthenticationInfo(request, version); diff --git a/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs b/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs index 82d5bde..49ea6eb 100644 --- a/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs +++ b/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs @@ -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 @@ -23,14 +22,14 @@ public static void MAuthAuthenticator_WithInvalidOptions_WillThrowException( ApplicationUuid = TestExtensions.ClientUuid, MAuthServiceUrl = mauthServiceUrl != null ? new Uri(mauthServiceUrl) : null, PrivateKey = privateKey - }, NullLoggerFactory.Instance)); + }));//, NullLoggerFactory.Instance)); [Fact] public static void MAuthAuthenticator_WithDefaultUuid_WillThrowException() => Assert.Throws(() => new MAuthAuthenticator(new MAuthTestOptions() { ApplicationUuid = default(Guid) - }, NullLoggerFactory.Instance)); + }));//, NullLoggerFactory.Instance)); [Theory] [InlineData("GET")] @@ -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);//, NullLoggerFactory.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -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);//, NullLoggerFactory.Instance); var mAuthCore = new MAuthCoreV2(); var signedRequest = await mAuthCore @@ -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));//, NullLoggerFactory.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -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));//, NullLoggerFactory.Instance); var mAuthCore = new MAuthCoreV2(); var signedRequest = await mAuthCore @@ -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));//, NullLoggerFactory.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -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));//, NullLoggerFactory.Instance); var mAuthCore = new MAuthCoreV2(); var signedRequest = await mAuthCore @@ -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);//, NullLoggerFactory.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -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);//, NullLoggerFactory.Instance); // Act var actual = authenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), version); @@ -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);//, NullLoggerFactory.Instance); // Act var actual = authenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), version); From 9e979f18411c4fb69171ed52b846519b2f77fd8e Mon Sep 17 00:00:00 2001 From: Prajon Date: Thu, 5 Sep 2019 15:10:55 -0400 Subject: [PATCH 06/12] Refactored by adding customLogger class and removed commented codes --- .../MAuthMiddleware.cs | 2 +- src/Medidata.MAuth.Core/MAuthAuthenticator.cs | 74 +++++-------------- .../MAuthAuthenticatorLogger.cs | 64 ++++++++++++++++ .../Infrastructure/MAuthServerHandler.cs | 2 +- .../MAuthAuthenticatorTests.cs | 22 +++--- 5 files changed, 97 insertions(+), 67 deletions(-) create mode 100644 src/Medidata.MAuth.Core/MAuthAuthenticatorLogger.cs diff --git a/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs b/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs index b370b9e..34b9d01 100644 --- a/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs @@ -29,7 +29,7 @@ public MAuthMiddleware(RequestDelegate next, MAuthMiddlewareOptions options, ILo this.next = next; this.options = options; this.loggerFactory = loggerFactory ?? NullLoggerFactory.Instance; - this.authenticator = new MAuthAuthenticator(options); //, loggerFactory ?? NullLoggerFactory.Instance); + this.authenticator = new MAuthAuthenticator(options); } /// diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs index e02bfdd..67f4b1c 100644 --- a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs +++ b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs @@ -7,6 +7,7 @@ using Medidata.MAuth.Core.Models; using Microsoft.Extensions.Logging; using log4net; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace Medidata.MAuth.Core { @@ -39,54 +40,11 @@ public MAuthAuthenticator(MAuthOptionsBase options) /// A task object of the boolean value that verifies if the request is authenticated or not. public async Task AuthenticateRequest(HttpRequestMessage request, ILoggerFactory loggerFactory) { - var logger = loggerFactory.CreateLogger(); - try - { - logger.LogInformation("Initiating Authentication of the request"); - var version = request.GetAuthHeaderValue().GetVersionFromAuthenticationHeader(); - - logger.LogInformation("Authentication is for {version}.", version); - - if (options.DisableV1 && version == MAuthVersion.MWS) - throw new InvalidVersionException($"Authentication with {version} version is disabled."); + ILogger logger = loggerFactory.CreateLogger(); - var mAuthCore = MAuthCoreFactory.Instantiate(version); - var authInfo = GetAuthenticationInfo(request, version); - var appInfo = await GetApplicationInfo(authInfo.ApplicationUuid, version); + MAuthAuthenticatorLogger customLogger = new MAuthAuthenticatorLogger(logger); - return mAuthCore.Verify(authInfo.Payload, await mAuthCore.GetSignature(request, authInfo), - appInfo.PublicKey); - } - catch (ArgumentException ex) - { - logger.LogError(ex, "Unable to authenticate due to invalid MAuth authentication headers."); - throw new AuthenticationException("The request has invalid MAuth authentication headers.", ex); - } - catch (RetriedRequestException ex) - { - logger.LogError(ex, "Unable to query the application information from MAuth server."); - throw new AuthenticationException( - "Could not query the application information for the application from the MAuth server.", ex); - } - catch (InvalidCipherTextException ex) - { - logger.LogWarning(ex, "Unable to authenticate due to invalid payload information."); - 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."); - throw new InvalidVersionException(ex.Message, ex); - } - catch (Exception ex) - { - logger.LogError(ex, "Unable to authenticate due to unexpected error."); - throw new AuthenticationException( - "An unexpected error occured during authentication. Please see the inner exception for details.", - ex - ); - } + return await AuthenticateRequestCore(request, customLogger); } /// /// Verifies if the request is authenticated or not. @@ -95,13 +53,21 @@ public async Task AuthenticateRequest(HttpRequestMessage request, ILoggerF /// A task object of the boolean value that verifies if the request is authenticated or not. public async Task AuthenticateRequest(HttpRequestMessage request) { - var logger = LogManager.GetLogger(typeof(MAuthAuthenticator)); + ILog logger = LogManager.GetLogger(typeof(MAuthAuthenticator)); + + MAuthAuthenticatorLogger customLogger = new MAuthAuthenticatorLogger(logger); + + return await AuthenticateRequestCore(request, customLogger); + } + + private async Task AuthenticateRequestCore(HttpRequestMessage request, MAuthAuthenticatorLogger logger) + { try { - logger.Info("Initiating Authentication of the request"); + logger.LogInformation("Initiating Authentication of the request"); var version = request.GetAuthHeaderValue().GetVersionFromAuthenticationHeader(); - logger.InfoFormat("Authentication is for the {0}.", version); + logger.LogInformation($"Authentication is for the {version}."); if (options.DisableV1 && version == MAuthVersion.MWS) throw new InvalidVersionException($"Authentication with {version} version is disabled."); @@ -115,29 +81,29 @@ public async Task AuthenticateRequest(HttpRequestMessage request) } catch (ArgumentException ex) { - logger.Error("Unable to authenticate due to invalid MAuth authentication headers.", ex); + logger.LogError(ex, "Unable to authenticate due to invalid MAuth authentication headers."); throw new AuthenticationException("The request has invalid MAuth authentication headers.", ex); } catch (RetriedRequestException ex) { - logger.Error("Unable to query the application information from MAuth server.", ex); + logger.LogError(ex, "Unable to query the application information from MAuth server."); throw new AuthenticationException( "Could not query the application information for the application from the MAuth server.", ex); } catch (InvalidCipherTextException ex) { - logger.Warn("Unable to authenticate due to invalid payload information.", ex); + logger.LogWarning(ex, "Unable to authenticate due to invalid payload information."); throw new AuthenticationException( "The request verification failed due to an invalid payload information.", ex); } catch (InvalidVersionException ex) { - logger.Error("Unable to authenticate due to invalid version.", ex); + logger.LogError(ex, "Unable to authenticate due to invalid version."); throw new InvalidVersionException(ex.Message, ex); } catch (Exception ex) { - logger.Error("Unable to authenticate due to unexpected error.", ex); + logger.LogError(ex, "Unable to authenticate due to unexpected error."); throw new AuthenticationException( "An unexpected error occured during authentication. Please see the inner exception for details.", ex diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticatorLogger.cs b/src/Medidata.MAuth.Core/MAuthAuthenticatorLogger.cs new file mode 100644 index 0000000..6122ffb --- /dev/null +++ b/src/Medidata.MAuth.Core/MAuthAuthenticatorLogger.cs @@ -0,0 +1,64 @@ +using Microsoft.Extensions.Logging; +using System; +using log4net; +using ILogger = Microsoft.Extensions.Logging.ILogger; + +namespace Medidata.MAuth.Core +{ + /// + /// MAuthAuthenticatorLogger Class which exposes logging methods used for MAuthenticator + /// + internal class MAuthAuthenticatorLogger + { + private readonly log4net.ILog _log; + + private readonly ILogger _logger; + public MAuthAuthenticatorLogger(ILog logger) + { + _log = logger; + } + + public MAuthAuthenticatorLogger(ILogger logger) + { + _logger = logger; + } + + /* + * Extremely boring code ahead + */ + + #region This code is extremely dull + + public void LogWarning(Exception exception, string message) + { + if (_log is null) + { + _logger.LogWarning(exception, message); + } + else + { _log.Warn(message, exception);} + } + + public void LogInformation(string message) + { + if (_log is null) + { + _logger.LogInformation(message); + } + else + { _log.Info(message);} + } + + public void LogError(Exception exception, string message) + { + if (_log is null) + { + _logger.LogError(exception, message); + } + else + { _log.Error(message, exception);} + } + #endregion + + } +} diff --git a/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs b/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs index e7eb451..12c5055 100644 --- a/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs +++ b/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs @@ -26,7 +26,7 @@ protected override async Task 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); diff --git a/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs b/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs index 49ea6eb..442924e 100644 --- a/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs +++ b/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs @@ -22,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(() => new MAuthAuthenticator(new MAuthTestOptions() { ApplicationUuid = default(Guid) - }));//, NullLoggerFactory.Instance)); + })); [Theory] [InlineData("GET")] @@ -41,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 @@ -69,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 @@ -100,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 @@ -130,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 @@ -160,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 @@ -195,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 @@ -277,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 @@ -308,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); @@ -330,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); From 4f6a74879c72e8c55b8ab86c9289f83485e53258 Mon Sep 17 00:00:00 2001 From: Prajon Date: Fri, 6 Sep 2019 09:51:13 -0400 Subject: [PATCH 07/12] Refactored to use custom log4netlogger class implementing ILogger interface and removed other custom logger class --- src/Medidata.MAuth.Core/Log4NetLogger.cs | 88 +++++++++++++++++++ src/Medidata.MAuth.Core/MAuthAuthenticator.cs | 22 +++-- .../MAuthAuthenticatorLogger.cs | 64 -------------- 3 files changed, 101 insertions(+), 73 deletions(-) create mode 100644 src/Medidata.MAuth.Core/Log4NetLogger.cs delete mode 100644 src/Medidata.MAuth.Core/MAuthAuthenticatorLogger.cs diff --git a/src/Medidata.MAuth.Core/Log4NetLogger.cs b/src/Medidata.MAuth.Core/Log4NetLogger.cs new file mode 100644 index 0000000..86bb980 --- /dev/null +++ b/src/Medidata.MAuth.Core/Log4NetLogger.cs @@ -0,0 +1,88 @@ +using System; +using log4net; +using Microsoft.Extensions.Logging; + +namespace Medidata.MAuth.Core +{ + /// + /// Log4NetLogger Class which implements ILogger interface for logging. + /// + internal class Log4NetLogger : ILogger + { + private readonly ILog _log; + + public Log4NetLogger(ILog log) + { + _log = log; + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + { + if (!IsEnabled(logLevel)) + { + return; + } + + if (formatter == null) + { + throw new ArgumentNullException(nameof(formatter)); + } + string message = null; + if (null != formatter) + { + message = formatter(state, exception); + } + if (!string.IsNullOrEmpty(message) || exception != null) + { + switch (logLevel) + { + case LogLevel.Critical: + _log.Fatal(message); + break; + case LogLevel.Debug: + case LogLevel.Trace: + _log.Debug(message); + break; + case LogLevel.Error: + _log.Error(message, exception); + break; + case LogLevel.Information: + _log.Info(message); + break; + case LogLevel.Warning: + _log.Warn(message, exception); + break; + default: + _log.Warn($"Encountered unknown log level {logLevel}, writing out as Info."); + _log.Info(message, exception); + break; + } + } + } + + public bool IsEnabled(LogLevel logLevel) + { + switch (logLevel) + { + case LogLevel.Critical: + return _log.IsFatalEnabled; + case LogLevel.Debug: + case LogLevel.Trace: + return _log.IsDebugEnabled; + case LogLevel.Error: + return _log.IsErrorEnabled; + case LogLevel.Information: + return _log.IsInfoEnabled; + case LogLevel.Warning: + return _log.IsWarnEnabled; + default: + throw new ArgumentOutOfRangeException(nameof(logLevel)); + } + } + + public IDisposable BeginScope(TState state) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs index 67f4b1c..c45391d 100644 --- a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs +++ b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs @@ -7,6 +7,7 @@ using Medidata.MAuth.Core.Models; using Microsoft.Extensions.Logging; using log4net; +using Microsoft.Extensions.Logging.Abstractions; using ILogger = Microsoft.Extensions.Logging.ILogger; namespace Medidata.MAuth.Core @@ -42,10 +43,9 @@ public async Task AuthenticateRequest(HttpRequestMessage request, ILoggerF { ILogger logger = loggerFactory.CreateLogger(); - MAuthAuthenticatorLogger customLogger = new MAuthAuthenticatorLogger(logger); - - return await AuthenticateRequestCore(request, customLogger); + return await AuthenticateRequestCore(request, logger); } + /// /// Verifies if the request is authenticated or not. /// @@ -53,21 +53,25 @@ public async Task AuthenticateRequest(HttpRequestMessage request, ILoggerF /// A task object of the boolean value that verifies if the request is authenticated or not. public async Task AuthenticateRequest(HttpRequestMessage request) { - ILog logger = LogManager.GetLogger(typeof(MAuthAuthenticator)); + ILog log = LogManager.GetLogger(typeof(MAuthAuthenticator)); - MAuthAuthenticatorLogger customLogger = new MAuthAuthenticatorLogger(logger); + ILogger logger; + if(log != null) + logger = new Log4NetLogger(log); + else + logger = NullLoggerFactory.Instance.CreateLogger(); - return await AuthenticateRequestCore(request, customLogger); + return await AuthenticateRequestCore(request, logger); } - private async Task AuthenticateRequestCore(HttpRequestMessage request, MAuthAuthenticatorLogger logger) + private async Task AuthenticateRequestCore(HttpRequestMessage request, ILogger logger) { try { - logger.LogInformation("Initiating Authentication of the request"); + logger.LogInformation("Initiating Authentication of the request."); var version = request.GetAuthHeaderValue().GetVersionFromAuthenticationHeader(); - logger.LogInformation($"Authentication is for the {version}."); + logger.LogInformation("Authentication is for the {version}.",version); if (options.DisableV1 && version == MAuthVersion.MWS) throw new InvalidVersionException($"Authentication with {version} version is disabled."); diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticatorLogger.cs b/src/Medidata.MAuth.Core/MAuthAuthenticatorLogger.cs deleted file mode 100644 index 6122ffb..0000000 --- a/src/Medidata.MAuth.Core/MAuthAuthenticatorLogger.cs +++ /dev/null @@ -1,64 +0,0 @@ -using Microsoft.Extensions.Logging; -using System; -using log4net; -using ILogger = Microsoft.Extensions.Logging.ILogger; - -namespace Medidata.MAuth.Core -{ - /// - /// MAuthAuthenticatorLogger Class which exposes logging methods used for MAuthenticator - /// - internal class MAuthAuthenticatorLogger - { - private readonly log4net.ILog _log; - - private readonly ILogger _logger; - public MAuthAuthenticatorLogger(ILog logger) - { - _log = logger; - } - - public MAuthAuthenticatorLogger(ILogger logger) - { - _logger = logger; - } - - /* - * Extremely boring code ahead - */ - - #region This code is extremely dull - - public void LogWarning(Exception exception, string message) - { - if (_log is null) - { - _logger.LogWarning(exception, message); - } - else - { _log.Warn(message, exception);} - } - - public void LogInformation(string message) - { - if (_log is null) - { - _logger.LogInformation(message); - } - else - { _log.Info(message);} - } - - public void LogError(Exception exception, string message) - { - if (_log is null) - { - _logger.LogError(exception, message); - } - else - { _log.Error(message, exception);} - } - #endregion - - } -} From 73476545da4e05104c164059cf9f21777a7e022f Mon Sep 17 00:00:00 2001 From: Prajon Date: Tue, 10 Sep 2019 15:20:53 -0400 Subject: [PATCH 08/12] Removed log4Net dependency and do logging using microsoft.extensions.logging and microsoft.owin.logging --- README.md | 13 +++ .../MAuthAspNetCoreExtensions.cs | 4 +- .../MAuthMiddleware.cs | 8 +- src/Medidata.MAuth.Core/MAuthAuthenticator.cs | 56 ++++--------- .../Medidata.MAuth.Core.csproj | 1 - src/Medidata.MAuth.Core/UtilityExtensions.cs | 7 +- .../MAuthAppBuilderExtensions.cs | 2 +- src/Medidata.MAuth.Owin/MAuthMiddleware.cs | 11 ++- .../Medidata.MAuth.Owin.csproj | 4 - .../OwinLoggerWrapper.cs} | 80 +++++++++---------- .../MAuthAuthenticatingHandler.cs | 10 ++- .../MAuthWebApiOptions.cs | 6 ++ .../Infrastructure/MAuthServerHandler.cs | 2 +- .../MAuthAuthenticatorTests.cs | 23 +++--- .../UtilityExtensionsTest.cs | 2 +- 15 files changed, 114 insertions(+), 115 deletions(-) rename src/{Medidata.MAuth.Core/Log4NetLogger.cs => Medidata.MAuth.Owin/OwinLoggerWrapper.cs} (62%) diff --git a/README.md b/README.md index 316c1fa..973d0e3 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,16 @@ public class Startup } } ``` +For enabling the logging from Owin application, the follofing configuration will need to be added or updated: +```C# + + + + + + + +``` A similar way can be implemented for ASP.NET Core (also in the `Startup` class): @@ -262,6 +272,9 @@ public static class WebApiConfig // when ready to disable authentication of V1 protococl options.DisableV1 = true + + // if loggerfactory is present + options.loggerfactory = new Microsoft.Extensions.Logging.LoggerFactory(); // or provide the existing loggerfactory }; config.MessageHandlers.Add(new MAuthAuthenticatingHandler(options)); diff --git a/src/Medidata.MAuth.AspNetCore/MAuthAspNetCoreExtensions.cs b/src/Medidata.MAuth.AspNetCore/MAuthAspNetCoreExtensions.cs index 78d9c38..6d38b1d 100644 --- a/src/Medidata.MAuth.AspNetCore/MAuthAspNetCoreExtensions.cs +++ b/src/Medidata.MAuth.AspNetCore/MAuthAspNetCoreExtensions.cs @@ -46,11 +46,11 @@ public static HttpRequestMessage ToHttpRequestMessage(this HttpRequest request) /// will throw an exception if any errors occurred during the authentication. /// public static async Task TryAuthenticate( - this HttpContext context, MAuthAuthenticator authenticator, bool shouldIgnoreExceptions, ILoggerFactory loggerFactory) + this HttpContext context, MAuthAuthenticator authenticator, bool shouldIgnoreExceptions) { try { - return await authenticator.AuthenticateRequest(context.Request.ToHttpRequestMessage(), loggerFactory); + return await authenticator.AuthenticateRequest(context.Request.ToHttpRequestMessage()); } catch (Exception) { diff --git a/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs b/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs index 34b9d01..d18c7b8 100644 --- a/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.AspNetCore/MAuthMiddleware.cs @@ -16,7 +16,6 @@ internal class MAuthMiddleware private readonly MAuthMiddlewareOptions options; private readonly MAuthAuthenticator authenticator; private readonly RequestDelegate next; - private readonly ILoggerFactory loggerFactory; /// /// Creates a new @@ -28,8 +27,9 @@ public MAuthMiddleware(RequestDelegate next, MAuthMiddlewareOptions options, ILo { this.next = next; this.options = options; - this.loggerFactory = loggerFactory ?? NullLoggerFactory.Instance; - this.authenticator = new MAuthAuthenticator(options); + loggerFactory = loggerFactory ?? NullLoggerFactory.Instance; + ILogger logger = loggerFactory.CreateLogger(); + this.authenticator = new MAuthAuthenticator(options, logger); } /// @@ -42,7 +42,7 @@ public async Task Invoke(HttpContext context) context.Request.EnableRewind(); if (!options.Bypass(context.Request) && - !await context.TryAuthenticate(authenticator, options.HideExceptionsAndReturnUnauthorized, loggerFactory)) + !await context.TryAuthenticate(authenticator, options.HideExceptionsAndReturnUnauthorized)) { context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; return; diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs index c45391d..44198a0 100644 --- a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs +++ b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs @@ -6,8 +6,6 @@ using Org.BouncyCastle.Crypto; using Medidata.MAuth.Core.Models; using Microsoft.Extensions.Logging; -using log4net; -using Microsoft.Extensions.Logging.Abstractions; using ILogger = Microsoft.Extensions.Logging.ILogger; namespace Medidata.MAuth.Core @@ -16,10 +14,11 @@ 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) + public MAuthAuthenticator(MAuthOptionsBase options, ILogger logger) { if (options.ApplicationUuid == default(Guid)) throw new ArgumentException(nameof(options.ApplicationUuid)); @@ -31,47 +30,22 @@ public MAuthAuthenticator(MAuthOptionsBase options) throw new ArgumentNullException(nameof(options.PrivateKey)); this.options = options; + this._logger = logger; } - /// - /// Verifies if the request is authenticated or not. - /// - /// The request. - /// The used with authentication. - /// A task object of the boolean value that verifies if the request is authenticated or not. - public async Task AuthenticateRequest(HttpRequestMessage request, ILoggerFactory loggerFactory) - { - ILogger logger = loggerFactory.CreateLogger(); - - return await AuthenticateRequestCore(request, logger); - } - - /// - /// Verifies if the request is authenticated or not. - /// - /// The request. - /// A task object of the boolean value that verifies if the request is authenticated or not. + ///// + ///// Verifies if the request is authenticated or not. + ///// + ///// The request. + ///// A task object of the boolean value that verifies if the request is authenticated or not. public async Task AuthenticateRequest(HttpRequestMessage request) - { - ILog log = LogManager.GetLogger(typeof(MAuthAuthenticator)); - - ILogger logger; - if(log != null) - logger = new Log4NetLogger(log); - else - logger = NullLoggerFactory.Instance.CreateLogger(); - - return await AuthenticateRequestCore(request, logger); - } - - private async Task AuthenticateRequestCore(HttpRequestMessage request, ILogger logger) { try { - logger.LogInformation("Initiating Authentication of the request."); + _logger.LogInformation("Initiating Authentication of the request."); var version = request.GetAuthHeaderValue().GetVersionFromAuthenticationHeader(); - logger.LogInformation("Authentication is for the {version}.",version); + _logger.LogInformation("Authentication is for the {version}.",version); if (options.DisableV1 && version == MAuthVersion.MWS) throw new InvalidVersionException($"Authentication with {version} version is disabled."); @@ -85,29 +59,29 @@ private async Task AuthenticateRequestCore(HttpRequestMessage request, ILo } catch (ArgumentException ex) { - logger.LogError(ex, "Unable to authenticate due to invalid MAuth authentication headers."); + _logger.LogError(ex, "Unable to authenticate due to invalid MAuth authentication headers."); throw new AuthenticationException("The request has invalid MAuth authentication headers.", ex); } catch (RetriedRequestException ex) { - logger.LogError(ex, "Unable to query the application information from MAuth server."); + _logger.LogError(ex, "Unable to query the application information from MAuth server."); throw new AuthenticationException( "Could not query the application information for the application from the MAuth server.", ex); } catch (InvalidCipherTextException ex) { - logger.LogWarning(ex, "Unable to authenticate due to invalid payload information."); + _logger.LogWarning(ex, "Unable to authenticate due to invalid payload information."); 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."); + _logger.LogError(ex, "Unable to authenticate due to invalid version."); throw new InvalidVersionException(ex.Message, ex); } catch (Exception ex) { - logger.LogError(ex, "Unable to authenticate due to unexpected error."); + _logger.LogError(ex, "Unable to authenticate due to unexpected error."); throw new AuthenticationException( "An unexpected error occured during authentication. Please see the inner exception for details.", ex diff --git a/src/Medidata.MAuth.Core/Medidata.MAuth.Core.csproj b/src/Medidata.MAuth.Core/Medidata.MAuth.Core.csproj index 23aaadf..1c15fe7 100644 --- a/src/Medidata.MAuth.Core/Medidata.MAuth.Core.csproj +++ b/src/Medidata.MAuth.Core/Medidata.MAuth.Core.csproj @@ -10,7 +10,6 @@ - diff --git a/src/Medidata.MAuth.Core/UtilityExtensions.cs b/src/Medidata.MAuth.Core/UtilityExtensions.cs index 6c826ec..3ae504e 100644 --- a/src/Medidata.MAuth.Core/UtilityExtensions.cs +++ b/src/Medidata.MAuth.Core/UtilityExtensions.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Medidata.MAuth.Core.Models; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace Medidata.MAuth.Core { @@ -66,11 +67,11 @@ public static bool TryParseAuthenticationHeader(this string headerValue, /// /// The request message to authenticate. /// The MAuth options to use for the authentication. - /// The logger factory used with authentication. + /// The logger interface used for logging. /// The task for the operation that is when completes will result in if /// the authentication is successful; otherwise . - public static Task Authenticate(this HttpRequestMessage request, MAuthOptionsBase options, ILoggerFactory loggerFactory) => - new MAuthAuthenticator(options).AuthenticateRequest(request, loggerFactory); + public static Task Authenticate(this HttpRequestMessage request, MAuthOptionsBase options, ILogger logger) => + new MAuthAuthenticator(options, logger).AuthenticateRequest(request); /// /// Determines the MAuth version enumerator reading authHeader. diff --git a/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs b/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs index 5a9cd17..ab23090 100644 --- a/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs +++ b/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs @@ -25,7 +25,7 @@ public static IAppBuilder UseMAuthAuthentication(this IAppBuilder app, MAuthMidd if (options == null) throw new ArgumentNullException(nameof(options)); - return app.Use(options); + return app.Use(options, app); } /// diff --git a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs index 9dc975b..bbd43ef 100644 --- a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs @@ -1,7 +1,10 @@ using System.Net; using System.Threading.Tasks; using Medidata.MAuth.Core; +using Microsoft.Extensions.Logging; using Microsoft.Owin; +using Microsoft.Owin.Logging; +using Owin; namespace Medidata.MAuth.Owin { @@ -9,15 +12,19 @@ internal class MAuthMiddleware: OwinMiddleware { private readonly MAuthMiddlewareOptions options; private readonly MAuthAuthenticator authenticator; + private Microsoft.Extensions.Logging.ILogger _logger; - public MAuthMiddleware(OwinMiddleware next, MAuthMiddlewareOptions options) : base(next) + public MAuthMiddleware(OwinMiddleware next, MAuthMiddlewareOptions options, IAppBuilder app) : base(next) { this.options = options; - authenticator = new MAuthAuthenticator(options); + var owinLogger = app.CreateLogger(); + this._logger = new OwinLoggerWrapper(owinLogger); + authenticator = new MAuthAuthenticator(options, _logger); } public override async Task Invoke(IOwinContext context) { + _logger.LogInformation("hello"); await context.EnsureRequestBodyStreamSeekable(); if (!options.Bypass(context.Request) && !await context.TryAuthenticate(authenticator, options.HideExceptionsAndReturnUnauthorized)) diff --git a/src/Medidata.MAuth.Owin/Medidata.MAuth.Owin.csproj b/src/Medidata.MAuth.Owin/Medidata.MAuth.Owin.csproj index cacb895..a99ed86 100644 --- a/src/Medidata.MAuth.Owin/Medidata.MAuth.Owin.csproj +++ b/src/Medidata.MAuth.Owin/Medidata.MAuth.Owin.csproj @@ -9,10 +9,6 @@ medidata;mauth;hmac;authentication;core;owin;middleware;webapi - - - - diff --git a/src/Medidata.MAuth.Core/Log4NetLogger.cs b/src/Medidata.MAuth.Owin/OwinLoggerWrapper.cs similarity index 62% rename from src/Medidata.MAuth.Core/Log4NetLogger.cs rename to src/Medidata.MAuth.Owin/OwinLoggerWrapper.cs index 86bb980..fa84d08 100644 --- a/src/Medidata.MAuth.Core/Log4NetLogger.cs +++ b/src/Medidata.MAuth.Owin/OwinLoggerWrapper.cs @@ -1,19 +1,42 @@ using System; -using log4net; +using System.Diagnostics; using Microsoft.Extensions.Logging; +using Microsoft.Owin.Logging; +using ILogger = Microsoft.Owin.Logging.ILogger; -namespace Medidata.MAuth.Core +namespace Medidata.MAuth.Owin { - /// - /// Log4NetLogger Class which implements ILogger interface for logging. - /// - internal class Log4NetLogger : ILogger + internal class OwinLoggerWrapper : Microsoft.Extensions.Logging.ILogger { - private readonly ILog _log; + private readonly ILogger _owinLogger; - public Log4NetLogger(ILog log) + public OwinLoggerWrapper(ILogger owinLogger) { - _log = log; + _owinLogger = owinLogger; + } + + public IDisposable BeginScope(TState state) + { + throw new NotImplementedException(); + } + + public bool IsEnabled(LogLevel logLevel) + { + switch (logLevel) + { + case LogLevel.Critical: + return _owinLogger.IsEnabled(TraceEventType.Critical); + case LogLevel.Debug: + case LogLevel.Trace: + case LogLevel.Information: + return _owinLogger.IsEnabled(TraceEventType.Information); + case LogLevel.Error: + return _owinLogger.IsEnabled(TraceEventType.Error); + case LogLevel.Warning: + return _owinLogger.IsEnabled(TraceEventType.Warning); + default: + throw new ArgumentOutOfRangeException(nameof(logLevel)); + } } public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) @@ -37,52 +60,25 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except switch (logLevel) { case LogLevel.Critical: - _log.Fatal(message); + _owinLogger.WriteCritical(message, exception); break; case LogLevel.Debug: case LogLevel.Trace: - _log.Debug(message); - break; case LogLevel.Error: - _log.Error(message, exception); + _owinLogger.WriteError(message, exception); break; case LogLevel.Information: - _log.Info(message); + _owinLogger.WriteInformation(message); break; case LogLevel.Warning: - _log.Warn(message, exception); + _owinLogger.WriteWarning(message, exception); break; default: - _log.Warn($"Encountered unknown log level {logLevel}, writing out as Info."); - _log.Info(message, exception); + _owinLogger.WriteWarning($"Encountered unknown log level {logLevel}, writing out as Info."); + _owinLogger.WriteInformation(message); break; } } } - - public bool IsEnabled(LogLevel logLevel) - { - switch (logLevel) - { - case LogLevel.Critical: - return _log.IsFatalEnabled; - case LogLevel.Debug: - case LogLevel.Trace: - return _log.IsDebugEnabled; - case LogLevel.Error: - return _log.IsErrorEnabled; - case LogLevel.Information: - return _log.IsInfoEnabled; - case LogLevel.Warning: - return _log.IsWarnEnabled; - default: - throw new ArgumentOutOfRangeException(nameof(logLevel)); - } - } - - public IDisposable BeginScope(TState state) - { - throw new NotImplementedException(); - } } } diff --git a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs index daf5cb6..ab10e9a 100644 --- a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs +++ b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs @@ -4,6 +4,8 @@ using System.Threading; using System.Threading.Tasks; using Medidata.MAuth.Core; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace Medidata.MAuth.WebApi { @@ -27,7 +29,9 @@ public class MAuthAuthenticatingHandler : DelegatingHandler public MAuthAuthenticatingHandler(MAuthWebApiOptions options) { this.options = options; - authenticator = new MAuthAuthenticator(options); + var loggerFactory = options.LoggerFactory ?? NullLoggerFactory.Instance; + var logger = loggerFactory.CreateLogger(typeof(MAuthAuthenticator)); + authenticator = new MAuthAuthenticator(options, logger); } /// @@ -41,7 +45,9 @@ public MAuthAuthenticatingHandler(MAuthWebApiOptions options) public MAuthAuthenticatingHandler(MAuthWebApiOptions options, HttpMessageHandler innerHandler) : base(innerHandler) { this.options = options; - authenticator = new MAuthAuthenticator(options); + var loggerFactory = options.LoggerFactory ?? NullLoggerFactory.Instance; + var logger = loggerFactory.CreateLogger(typeof(MAuthAuthenticator)); + authenticator = new MAuthAuthenticator(options, logger); } /// diff --git a/src/Medidata.MAuth.WebApi/MAuthWebApiOptions.cs b/src/Medidata.MAuth.WebApi/MAuthWebApiOptions.cs index 715b3d8..9a3239d 100644 --- a/src/Medidata.MAuth.WebApi/MAuthWebApiOptions.cs +++ b/src/Medidata.MAuth.WebApi/MAuthWebApiOptions.cs @@ -1,4 +1,5 @@ using Medidata.MAuth.Core; +using Microsoft.Extensions.Logging; namespace Medidata.MAuth.WebApi { @@ -13,5 +14,10 @@ public class MAuthWebApiOptions: MAuthOptionsBase /// The default is . /// public bool HideExceptionsAndReturnUnauthorized { get; set; } = true; + + /// + /// + /// + public ILoggerFactory LoggerFactory { get; set; } } } diff --git a/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs b/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs index 12c5055..c5331c5 100644 --- a/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs +++ b/tests/Medidata.MAuth.Tests/Infrastructure/MAuthServerHandler.cs @@ -26,7 +26,7 @@ protected override async Task SendAsync( if (currentNumberOfAttempts < SucceedAfterThisManyAttempts) return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable); - var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions); + var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLogger.Instance); var authInfo = authenticator.GetAuthenticationInfo(request, version); diff --git a/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs b/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs index 442924e..753c9b3 100644 --- a/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs +++ b/tests/Medidata.MAuth.Tests/MAuthAuthenticatorTests.cs @@ -6,6 +6,7 @@ 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 @@ -22,14 +23,14 @@ public static void MAuthAuthenticator_WithInvalidOptions_WillThrowException( ApplicationUuid = TestExtensions.ClientUuid, MAuthServiceUrl = mauthServiceUrl != null ? new Uri(mauthServiceUrl) : null, PrivateKey = privateKey - })); + }, NullLogger.Instance)); [Fact] public static void MAuthAuthenticator_WithDefaultUuid_WillThrowException() => Assert.Throws(() => new MAuthAuthenticator(new MAuthTestOptions() { ApplicationUuid = default(Guid) - })); + }, NullLogger.Instance)); [Theory] [InlineData("GET")] @@ -41,7 +42,7 @@ public static async Task AuthenticateRequest_WithValidRequest_WillAuthenticate(s // Arrange var testData = await method.FromResource(); - var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions); + var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLogger.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -69,7 +70,7 @@ public static async Task AuthenticateRequest_WithValidMWSV2Request_WillAuthentic // Arrange var testData = await method.FromResourceV2(); var version = MAuthVersion.MWSV2; - var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions); + var authenticator = new MAuthAuthenticator(TestExtensions.ServerOptions, NullLogger.Instance); var mAuthCore = new MAuthCoreV2(); var signedRequest = await mAuthCore @@ -100,7 +101,7 @@ public static async Task AuthenticateRequest_WithNumberOfAttempts_WillAuthentica var testData = await "GET".FromResource(); var authenticator = new MAuthAuthenticator(TestExtensions.GetServerOptionsWithAttempts( - policy, shouldSucceedWithin: true)); + policy, shouldSucceedWithin: true), NullLogger.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -130,7 +131,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)); + policy, shouldSucceedWithin: true), NullLogger.Instance); var mAuthCore = new MAuthCoreV2(); var signedRequest = await mAuthCore @@ -160,7 +161,7 @@ public static async Task AuthenticateRequest_AfterNumberOfAttempts_WillThrowExce var testData = await "GET".FromResource(); var authenticator = new MAuthAuthenticator(TestExtensions.GetServerOptionsWithAttempts( - policy, shouldSucceedWithin: false)); + policy, shouldSucceedWithin: false), NullLogger.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -195,7 +196,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)); + policy, shouldSucceedWithin: false), NullLogger.Instance); var mAuthCore = new MAuthCoreV2(); var signedRequest = await mAuthCore @@ -277,7 +278,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); + var authenticator = new MAuthAuthenticator(testOptions, NullLogger.Instance); var mAuthCore = new MAuthCore(); var signedRequest = await mAuthCore @@ -308,7 +309,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); + var authenticator = new MAuthAuthenticator(testOptions, NullLogger.Instance); // Act var actual = authenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), version); @@ -330,7 +331,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); + var authenticator = new MAuthAuthenticator(testOptions, NullLogger.Instance); // Act var actual = authenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), version); diff --git a/tests/Medidata.MAuth.Tests/UtilityExtensionsTest.cs b/tests/Medidata.MAuth.Tests/UtilityExtensionsTest.cs index d07c67b..e69a2fa 100644 --- a/tests/Medidata.MAuth.Tests/UtilityExtensionsTest.cs +++ b/tests/Medidata.MAuth.Tests/UtilityExtensionsTest.cs @@ -68,7 +68,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, NullLogger.Instance); // Assert Assert.True(isAuthenticated); From 76e09706b8c1732196ec1c5d22fe6ba965241cad Mon Sep 17 00:00:00 2001 From: Prajon Date: Wed, 11 Sep 2019 09:02:49 -0400 Subject: [PATCH 09/12] Refactored as per feedback --- src/Medidata.MAuth.Core/MAuthAuthenticator.cs | 18 +++++++++--------- src/Medidata.MAuth.Owin/MAuthMiddleware.cs | 6 ++---- .../MAuthAuthenticatingHandler.cs | 17 ++++++++++------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs index 44198a0..5c27b85 100644 --- a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs +++ b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs @@ -14,7 +14,7 @@ internal class MAuthAuthenticator { private readonly MAuthOptionsBase options; private readonly IMemoryCache cache = new MemoryCache(new MemoryCacheOptions()); - private readonly ILogger _logger; + private readonly ILogger logger; public Guid ApplicationUuid => options.ApplicationUuid; @@ -30,7 +30,7 @@ public MAuthAuthenticator(MAuthOptionsBase options, ILogger logger) throw new ArgumentNullException(nameof(options.PrivateKey)); this.options = options; - this._logger = logger; + this.logger = logger; } ///// @@ -42,10 +42,10 @@ public async Task AuthenticateRequest(HttpRequestMessage request) { try { - _logger.LogInformation("Initiating Authentication of the request."); + logger.LogInformation("Initiating Authentication of the request."); var version = request.GetAuthHeaderValue().GetVersionFromAuthenticationHeader(); - _logger.LogInformation("Authentication is for the {version}.",version); + logger.LogInformation("Authentication is for the {version}.",version); if (options.DisableV1 && version == MAuthVersion.MWS) throw new InvalidVersionException($"Authentication with {version} version is disabled."); @@ -59,29 +59,29 @@ public async Task AuthenticateRequest(HttpRequestMessage request) } catch (ArgumentException ex) { - _logger.LogError(ex, "Unable to authenticate due to invalid MAuth authentication headers."); + logger.LogError(ex, "Unable to authenticate due to invalid MAuth authentication headers."); throw new AuthenticationException("The request has invalid MAuth authentication headers.", ex); } catch (RetriedRequestException ex) { - _logger.LogError(ex, "Unable to query the application information from MAuth server."); + logger.LogError(ex, "Unable to query the application information from MAuth server."); throw new AuthenticationException( "Could not query the application information for the application from the MAuth server.", ex); } catch (InvalidCipherTextException ex) { - _logger.LogWarning(ex, "Unable to authenticate due to invalid payload information."); + logger.LogWarning(ex, "Unable to authenticate due to invalid payload information."); 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."); + logger.LogError(ex, "Unable to authenticate due to invalid version."); throw new InvalidVersionException(ex.Message, ex); } catch (Exception ex) { - _logger.LogError(ex, "Unable to authenticate due to unexpected error."); + logger.LogError(ex, "Unable to authenticate due to unexpected error."); throw new AuthenticationException( "An unexpected error occured during authentication. Please see the inner exception for details.", ex diff --git a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs index bbd43ef..60ef9f9 100644 --- a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs @@ -12,19 +12,17 @@ internal class MAuthMiddleware: OwinMiddleware { private readonly MAuthMiddlewareOptions options; private readonly MAuthAuthenticator authenticator; - private Microsoft.Extensions.Logging.ILogger _logger; public MAuthMiddleware(OwinMiddleware next, MAuthMiddlewareOptions options, IAppBuilder app) : base(next) { this.options = options; var owinLogger = app.CreateLogger(); - this._logger = new OwinLoggerWrapper(owinLogger); - authenticator = new MAuthAuthenticator(options, _logger); + Microsoft.Extensions.Logging.ILogger logger = new OwinLoggerWrapper(owinLogger); + authenticator = new MAuthAuthenticator(options, logger); } public override async Task Invoke(IOwinContext context) { - _logger.LogInformation("hello"); await context.EnsureRequestBodyStreamSeekable(); if (!options.Bypass(context.Request) && !await context.TryAuthenticate(authenticator, options.HideExceptionsAndReturnUnauthorized)) diff --git a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs index ab10e9a..48c2134 100644 --- a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs +++ b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs @@ -16,7 +16,7 @@ namespace Medidata.MAuth.WebApi public class MAuthAuthenticatingHandler : DelegatingHandler { private readonly MAuthWebApiOptions options; - private readonly MAuthAuthenticator authenticator; + private MAuthAuthenticator authenticator; /// Gets the Uuid of the client application. public Guid ClientAppUuid => authenticator.ApplicationUuid; @@ -29,9 +29,7 @@ public class MAuthAuthenticatingHandler : DelegatingHandler public MAuthAuthenticatingHandler(MAuthWebApiOptions options) { this.options = options; - var loggerFactory = options.LoggerFactory ?? NullLoggerFactory.Instance; - var logger = loggerFactory.CreateLogger(typeof(MAuthAuthenticator)); - authenticator = new MAuthAuthenticator(options, logger); + SetupHandler(); } /// @@ -45,9 +43,7 @@ public MAuthAuthenticatingHandler(MAuthWebApiOptions options) public MAuthAuthenticatingHandler(MAuthWebApiOptions options, HttpMessageHandler innerHandler) : base(innerHandler) { this.options = options; - var loggerFactory = options.LoggerFactory ?? NullLoggerFactory.Instance; - var logger = loggerFactory.CreateLogger(typeof(MAuthAuthenticator)); - authenticator = new MAuthAuthenticator(options, logger); + SetupHandler(); } /// @@ -71,5 +67,12 @@ protected override async Task SendAsync( .SendAsync(request, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); } + + private void SetupHandler() + { + var loggerFactory = options.LoggerFactory ?? NullLoggerFactory.Instance; + var logger = loggerFactory.CreateLogger(typeof(MAuthAuthenticatingHandler)); + this.authenticator = new MAuthAuthenticator(options, logger); + } } } From 87e28a4de450c9c59c5d2448a7b4d907f5add4df Mon Sep 17 00:00:00 2001 From: Prajon Date: Wed, 11 Sep 2019 21:15:24 -0400 Subject: [PATCH 10/12] Passed options param also to SetupHandler --- .../MAuthAuthenticatingHandler.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs index 48c2134..79549d5 100644 --- a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs +++ b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs @@ -15,7 +15,7 @@ namespace Medidata.MAuth.WebApi /// public class MAuthAuthenticatingHandler : DelegatingHandler { - private readonly MAuthWebApiOptions options; + private MAuthWebApiOptions options; private MAuthAuthenticator authenticator; /// Gets the Uuid of the client application. @@ -28,8 +28,7 @@ public class MAuthAuthenticatingHandler : DelegatingHandler /// The options for this message handler. public MAuthAuthenticatingHandler(MAuthWebApiOptions options) { - this.options = options; - SetupHandler(); + SetupHandler(options); } /// @@ -42,8 +41,7 @@ public MAuthAuthenticatingHandler(MAuthWebApiOptions options) /// public MAuthAuthenticatingHandler(MAuthWebApiOptions options, HttpMessageHandler innerHandler) : base(innerHandler) { - this.options = options; - SetupHandler(); + SetupHandler(options); } /// @@ -68,8 +66,9 @@ protected override async Task SendAsync( .ConfigureAwait(continueOnCapturedContext: false); } - private void SetupHandler() + private void SetupHandler(MAuthWebApiOptions opt) { + this.options = opt; var loggerFactory = options.LoggerFactory ?? NullLoggerFactory.Instance; var logger = loggerFactory.CreateLogger(typeof(MAuthAuthenticatingHandler)); this.authenticator = new MAuthAuthenticator(options, logger); From 9ff405b565d51432c9f934bb1e0047e809244575 Mon Sep 17 00:00:00 2001 From: Prajon Date: Wed, 11 Sep 2019 22:21:42 -0400 Subject: [PATCH 11/12] Updates --- src/Medidata.MAuth.Core/MAuthAuthenticator.cs | 12 ++++++------ .../MAuthAppBuilderExtensions.cs | 4 +++- src/Medidata.MAuth.Owin/MAuthMiddleware.cs | 7 ++----- .../MAuthAuthenticatingHandler.cs | 15 ++++++++------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs index 5c27b85..23bcaf2 100644 --- a/src/Medidata.MAuth.Core/MAuthAuthenticator.cs +++ b/src/Medidata.MAuth.Core/MAuthAuthenticator.cs @@ -33,11 +33,11 @@ public MAuthAuthenticator(MAuthOptionsBase options, ILogger logger) this.logger = logger; } - ///// - ///// Verifies if the request is authenticated or not. - ///// - ///// The request. - ///// A task object of the boolean value that verifies if the request is authenticated or not. + /// + /// Verifies if the request is authenticated or not. + /// + /// The request. + /// A task object of the boolean value that verifies if the request is authenticated or not. public async Task AuthenticateRequest(HttpRequestMessage request) { try @@ -119,7 +119,7 @@ private HttpRequestMessage CreateRequest(Guid applicationUuid, string tokenReque /// Extracts the authentication information from a . /// /// The request that has the authentication information. - /// /// Enum value of the MAuthVersion. + /// Enum value of the MAuthVersion. /// The authentication information with the payload from the request. internal PayloadAuthenticationInfo GetAuthenticationInfo(HttpRequestMessage request, MAuthVersion version) { diff --git a/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs b/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs index ab23090..819f07b 100644 --- a/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs +++ b/src/Medidata.MAuth.Owin/MAuthAppBuilderExtensions.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.Owin.Logging; using Owin; namespace Medidata.MAuth.Owin @@ -25,7 +26,8 @@ public static IAppBuilder UseMAuthAuthentication(this IAppBuilder app, MAuthMidd if (options == null) throw new ArgumentNullException(nameof(options)); - return app.Use(options, app); + var owinLogger = app.CreateLogger(); + return app.Use(options, owinLogger); } /// diff --git a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs index 60ef9f9..b2c8db0 100644 --- a/src/Medidata.MAuth.Owin/MAuthMiddleware.cs +++ b/src/Medidata.MAuth.Owin/MAuthMiddleware.cs @@ -1,10 +1,8 @@ using System.Net; using System.Threading.Tasks; using Medidata.MAuth.Core; -using Microsoft.Extensions.Logging; using Microsoft.Owin; -using Microsoft.Owin.Logging; -using Owin; +using ILogger = Microsoft.Owin.Logging.ILogger; namespace Medidata.MAuth.Owin { @@ -13,10 +11,9 @@ internal class MAuthMiddleware: OwinMiddleware private readonly MAuthMiddlewareOptions options; private readonly MAuthAuthenticator authenticator; - public MAuthMiddleware(OwinMiddleware next, MAuthMiddlewareOptions options, IAppBuilder app) : base(next) + public MAuthMiddleware(OwinMiddleware next, MAuthMiddlewareOptions options, ILogger owinLogger) : base(next) { this.options = options; - var owinLogger = app.CreateLogger(); Microsoft.Extensions.Logging.ILogger logger = new OwinLoggerWrapper(owinLogger); authenticator = new MAuthAuthenticator(options, logger); } diff --git a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs index 79549d5..b1a5a40 100644 --- a/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs +++ b/src/Medidata.MAuth.WebApi/MAuthAuthenticatingHandler.cs @@ -15,8 +15,8 @@ namespace Medidata.MAuth.WebApi /// public class MAuthAuthenticatingHandler : DelegatingHandler { - private MAuthWebApiOptions options; - private MAuthAuthenticator authenticator; + private readonly MAuthWebApiOptions options; + private readonly MAuthAuthenticator authenticator; /// Gets the Uuid of the client application. public Guid ClientAppUuid => authenticator.ApplicationUuid; @@ -28,7 +28,8 @@ public class MAuthAuthenticatingHandler : DelegatingHandler /// The options for this message handler. public MAuthAuthenticatingHandler(MAuthWebApiOptions options) { - SetupHandler(options); + this.options = options; + this.authenticator = this.SetupMAuthAuthenticator(options); } /// @@ -41,7 +42,8 @@ public MAuthAuthenticatingHandler(MAuthWebApiOptions options) /// public MAuthAuthenticatingHandler(MAuthWebApiOptions options, HttpMessageHandler innerHandler) : base(innerHandler) { - SetupHandler(options); + this.options = options; + this.authenticator = this.SetupMAuthAuthenticator(options); } /// @@ -66,12 +68,11 @@ protected override async Task SendAsync( .ConfigureAwait(continueOnCapturedContext: false); } - private void SetupHandler(MAuthWebApiOptions opt) + private MAuthAuthenticator SetupMAuthAuthenticator(MAuthWebApiOptions opt) { - this.options = opt; var loggerFactory = options.LoggerFactory ?? NullLoggerFactory.Instance; var logger = loggerFactory.CreateLogger(typeof(MAuthAuthenticatingHandler)); - this.authenticator = new MAuthAuthenticator(options, logger); + return new MAuthAuthenticator(options, logger); } } } From 452d41b8b2ae6a9bd66f1404da54f86a3bcae491 Mon Sep 17 00:00:00 2001 From: Prajon Date: Tue, 17 Sep 2019 09:11:19 -0400 Subject: [PATCH 12/12] Updated typo error --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 973d0e3..d5a8127 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,7 @@ public class Startup } } ``` -For enabling the logging from Owin application, the follofing configuration will need to be added or updated: +For enabling the logging from Owin application, the following configuration will need to be added or updated: ```C#