diff --git a/EstateReportingAPI.Tests/General/BootstrapperTests.cs b/EstateReportingAPI.Tests/General/BootstrapperTests.cs deleted file mode 100644 index 31b702c..0000000 --- a/EstateReportingAPI.Tests/General/BootstrapperTests.cs +++ /dev/null @@ -1,72 +0,0 @@ -namespace EstateReportingAPI.Tests.General -{ - using System; - using System.Collections.Generic; - using System.Diagnostics; - using EstateReportingAPI; - using Lamar; - using Microsoft.AspNetCore.Hosting; - using Microsoft.Extensions.Configuration; - using Microsoft.Extensions.DependencyInjection; - using Microsoft.Extensions.Hosting; - using Moq; - using Xunit; - - [Collection("TestCollection")] - public class BootstrapperTests - { - [Fact] - public void VerifyBootstrapperIsValid(){ - - Mock hostingEnvironment = new Mock(); - hostingEnvironment.Setup(he => he.EnvironmentName).Returns("Development"); - hostingEnvironment.Setup(he => he.ContentRootPath).Returns("/home"); - hostingEnvironment.Setup(he => he.ApplicationName).Returns("Test Application"); - - ServiceRegistry services = new ServiceRegistry(); - Startup s = new Startup(hostingEnvironment.Object); - Startup.Configuration = this.SetupMemoryConfiguration(); - - this.AddTestRegistrations(services, hostingEnvironment.Object); - s.ConfigureContainer(services); - Startup.Container.AssertConfigurationIsValid(AssertMode.Full); - } - - private IConfigurationRoot SetupMemoryConfiguration() - { - Dictionary configuration = new Dictionary(); - - IConfigurationBuilder builder = new ConfigurationBuilder(); - - configuration.Add("ConnectionStrings:HealthCheck", "HeathCheckConnString"); - configuration.Add("ConnectionStrings:TransactionProcessorReadModel", "TransactionProcessorReadModel"); - - configuration.Add("SecurityConfiguration:Authority", "https://127.0.0.1"); - configuration.Add("EventStoreSettings:ConnectionString", "https://127.0.0.1:2113"); - configuration.Add("EventStoreSettings:ConnectionName", "UnitTestConnection"); - configuration.Add("EventStoreSettings:UserName", "admin"); - configuration.Add("EventStoreSettings:Password", "changeit"); - configuration.Add("AppSettings:UseConnectionStringConfig", "false"); - configuration.Add("AppSettings:SecurityService", "http://127.0.0.1"); - configuration.Add("AppSettings:MessagingServiceApi", "http://127.0.0.1"); - configuration.Add("AppSettings:TransactionProcessorApi", "http://127.0.0.1"); - configuration.Add("AppSettings:DatabaseEngine", "SqlServer"); - - builder.AddInMemoryCollection(configuration); - - return builder.Build(); - } - - private void AddTestRegistrations(ServiceRegistry services, - IWebHostEnvironment hostingEnvironment) - { - services.AddLogging(); - DiagnosticListener diagnosticSource = new DiagnosticListener(hostingEnvironment.ApplicationName); - services.AddSingleton(diagnosticSource); - services.AddSingleton(diagnosticSource); - services.AddSingleton(hostingEnvironment); - services.AddSingleton(hostingEnvironment); - services.AddSingleton(Startup.Configuration); - } - } -} diff --git a/EstateReportingAPI/Bootstrapper/MiddlewareRegistry.cs b/EstateReportingAPI/Bootstrapper/MiddlewareRegistry.cs index 3061de9..51d5871 100644 --- a/EstateReportingAPI/Bootstrapper/MiddlewareRegistry.cs +++ b/EstateReportingAPI/Bootstrapper/MiddlewareRegistry.cs @@ -1,4 +1,5 @@ using Microsoft.OpenApi; +using OpenIddict.Client; using Shared.Middleware; namespace EstateReportingAPI.Bootstrapper{ @@ -80,29 +81,24 @@ private void ConfigureSwagger(){ private void ConfigureAuthentication(){ String? inTestMode = Environment.GetEnvironmentVariable("InTestMode"); if (String.Compare(inTestMode, Boolean.TrueString, StringComparison.InvariantCultureIgnoreCase) != 0){ - this.AddAuthentication(options => { - options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; - }).AddJwtBearer(options => { - options.BackchannelHttpHandler = new HttpClientHandler{ - ServerCertificateCustomValidationCallback = (message, - certificate, - chain, - sslPolicyErrors) => true - }; - options.Authority = ConfigurationReader.GetValue("SecurityConfiguration", "Authority"); - options.Audience = ConfigurationReader.GetValue("SecurityConfiguration", "ApiName"); - - options.TokenValidationParameters = new TokenValidationParameters{ - ValidateAudience = false, - ValidAudience = - ConfigurationReader.GetValue("SecurityConfiguration", "ApiName"), - ValidIssuer = - ConfigurationReader.GetValue("SecurityConfiguration", "Authority"), - }; - options.IncludeErrorDetails = true; - }); + this.AddOpenIddict() + // Register the OpenIddict client components. + .AddClient(options => { + // Allow grant_type=client_credentials to be negotiated. + options.AllowClientCredentialsFlow(); + + // Disable token storage, which is not necessary for non-interactive flows like + // grant_type=password, grant_type=client_credentials or grant_type=refresh_token. + options.DisableTokenStorage(); + + // Register the System.Net.Http integration and use the identity of the current + // assembly as a more specific user agent, which can be useful when dealing with + // providers that use the user agent as a way to throttle requests (e.g Reddit). + options.UseSystemNetHttp().SetProductInformation(typeof(Program).Assembly); + + // Add a client registration matching the client application definition in the server project. + options.AddRegistration(new OpenIddictClientRegistration { Issuer = new Uri(ConfigurationReader.GetValue("SecurityConfiguration", "Authority"), UriKind.Absolute), ClientId = ConfigurationReader.GetValue("SecurityConfiguration", "ApiName") }); + }); } } diff --git a/EstateReportingAPI/EstateReportingAPI.csproj b/EstateReportingAPI/EstateReportingAPI.csproj index 12a5dfb..7fa3f81 100644 --- a/EstateReportingAPI/EstateReportingAPI.csproj +++ b/EstateReportingAPI/EstateReportingAPI.csproj @@ -18,6 +18,7 @@ +