diff --git a/MessagingService/Bootstrapper/MiddlewareRegistry.cs b/MessagingService/Bootstrapper/MiddlewareRegistry.cs index 5a8f1ab..f27927c 100644 --- a/MessagingService/Bootstrapper/MiddlewareRegistry.cs +++ b/MessagingService/Bootstrapper/MiddlewareRegistry.cs @@ -43,61 +43,9 @@ public MiddlewareRegistry() { failureStatus: HealthStatus.Unhealthy, tags: new string[] { "db", "eventstore" }); - this.AddSwaggerGen(c => - { - c.SwaggerDoc("v1", new OpenApiInfo - { - Title = "Messaging API", - Version = "1.0", - Description = "A REST Api to manage sending of various messages over different formats, currently only Email and SMS are supported.", - Contact = new OpenApiContact - { - Name = "Stuart Ferguson", - Email = "golfhandicapping@btinternet.com" - } - }); - // add a custom operation filter which sets default values - c.OperationFilter(); - c.ExampleFilters(); - - //Locate the XML files being generated by ASP.NET... - var directory = new DirectoryInfo(AppContext.BaseDirectory); - var xmlFiles = directory.GetFiles("*.xml"); - - //... and tell Swagger to use those XML comments. - foreach (FileInfo fileInfo in xmlFiles) - { - c.IncludeXmlComments(fileInfo.FullName); - } - }); - - this.AddSwaggerExamplesFromAssemblyOf(); - - 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 = GetSecurityConfigSetting("Authority"); - options.Audience = GetSecurityConfigSetting("ApiName"); - - options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters() - { - ValidateAudience = false, - ValidAudience = GetSecurityConfigSetting("ApiName"), - ValidIssuer = GetSecurityConfigSetting("Authority"), - }; - options.IncludeErrorDetails = true; - }); - + this.ConfigureAuthentication(); + this.ConfigureSwagger(); + this.AddControllers().AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; @@ -118,5 +66,63 @@ public MiddlewareRegistry() { this.AddSingleton(config); } + + private void ConfigureAuthentication() { + 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 = GetSecurityConfigSetting("Authority"); + options.Audience = GetSecurityConfigSetting("ApiName"); + + options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters() + { + ValidateAudience = false, + ValidAudience = GetSecurityConfigSetting("ApiName"), + ValidIssuer = GetSecurityConfigSetting("Authority"), + }; + options.IncludeErrorDetails = true; + }); + } + private void ConfigureSwagger() { + this.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo + { + Title = "Messaging API", + Version = "1.0", + Description = "A REST Api to manage sending of various messages over different formats, currently only Email and SMS are supported.", + Contact = new OpenApiContact + { + Name = "Stuart Ferguson", + Email = "golfhandicapping@btinternet.com" + } + }); + // add a custom operation filter which sets default values + c.OperationFilter(); + c.ExampleFilters(); + + //Locate the XML files being generated by ASP.NET... + var directory = new DirectoryInfo(AppContext.BaseDirectory); + var xmlFiles = directory.GetFiles("*.xml"); + + //... and tell Swagger to use those XML comments. + foreach (FileInfo fileInfo in xmlFiles) + { + c.IncludeXmlComments(fileInfo.FullName); + } + }); + + this.AddSwaggerExamplesFromAssemblyOf(); + } } } diff --git a/MessagingService/Controllers/DomainEventController.cs b/MessagingService/Controllers/DomainEventController.cs index b48c2d0..4efff4e 100644 --- a/MessagingService/Controllers/DomainEventController.cs +++ b/MessagingService/Controllers/DomainEventController.cs @@ -148,7 +148,7 @@ private String ValidateEvent(String domainEventJson) { /// /// The controller name /// - public const String ControllerName = "domainevents"; + private const String ControllerName = "domainevents"; /// /// The controller route diff --git a/MessagingService/Controllers/EmailController.cs b/MessagingService/Controllers/EmailController.cs index d6712cd..795bb4e 100644 --- a/MessagingService/Controllers/EmailController.cs +++ b/MessagingService/Controllers/EmailController.cs @@ -146,7 +146,7 @@ private FileType ConvertFileType(DataTransferObjects.FileType emailAttachmentFil /// /// The controller name /// - public const String ControllerName = "email"; + private const String ControllerName = "email"; /// /// The controller route diff --git a/MessagingService/Controllers/SMSController.cs b/MessagingService/Controllers/SMSController.cs index 56edb02..15115fd 100644 --- a/MessagingService/Controllers/SMSController.cs +++ b/MessagingService/Controllers/SMSController.cs @@ -113,7 +113,7 @@ public async Task ResendSMS([FromBody] ResendSMSRequest resendSMS /// /// The controller name /// - public const String ControllerName = "sms"; + private const String ControllerName = "sms"; /// /// The controller route