Skip to content

Commit de9babd

Browse files
committed
add SecurityHeaders middleware
1 parent 00e9222 commit de9babd

File tree

10 files changed

+29
-22
lines changed

10 files changed

+29
-22
lines changed

restclient.http

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
@host = http://localhost:5000
22

33
###
4-
GET {{host}}/product-api/products?api-version=1.0 HTTP/1.1
4+
GET {{host}}/product-api/v1/products HTTP/1.1
55
content-type: {{contentType}}
66
x-query: {"filters":[{"fieldName": "Name", "comparision": "Contains", "fieldValue": "test"}],"sorts":["nameDesc"],"page":1,"pageSize":20}
77

88
###
99
@id = 23537dac-303f-446f-be2a-1dbea22b3eba
10-
GET {{host}}/product-api/products/{{id}}?api-version=1.0 HTTP/1.1
10+
GET {{host}}/product-api/v1/products/{{id}} HTTP/1.1
1111
content-type: {{contentType}}
1212

1313
###
@@ -16,7 +16,7 @@ GET {{host}}/setting-api/countries/{{country-id}} HTTP/1.1
1616
content-type: {{contentType}}
1717

1818
###
19-
POST {{host}}/product-api/products?api-version=1.0 HTTP/1.1
19+
POST {{host}}/product-api/v1/products HTTP/1.1
2020
content-type: {{contentType}}
2121

2222
{
@@ -29,7 +29,7 @@ content-type: {{contentType}}
2929
}
3030

3131
###
32-
POST {{host}}/customer-api/customers?api-version=1.0 HTTP/1.1
32+
POST {{host}}/customer-api/v1/customers HTTP/1.1
3333
content-type: {{contentType}}
3434

3535
{
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace N8T.Core.Domain
44
{
@@ -10,22 +10,22 @@ public CoreException(string message) : base(message)
1010

1111
public static CoreException Exception(string message)
1212
{
13-
return new CoreException(message);
13+
return new(message);
1414
}
1515

1616
public static CoreException NullArgument(string arg)
1717
{
18-
return new CoreException($"{arg} cannot be null");
18+
return new($"{arg} cannot be null");
1919
}
2020

2121
public static CoreException InvalidArgument(string arg)
2222
{
23-
return new CoreException($"{arg} is invalid");
23+
return new($"{arg} is invalid");
2424
}
2525

2626
public static CoreException NotFound(string arg)
2727
{
28-
return new CoreException($"{arg} was not found");
28+
return new($"{arg} was not found");
2929
}
3030
}
31-
}
31+
}

src/BuildingBlocks/N8T.Infrastructure/Extensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using N8T.Infrastructure.Logging;
1212
using N8T.Infrastructure.Validator;
1313
using Newtonsoft.Json;
14+
using Newtonsoft.Json.Serialization;
1415
using Serilog;
1516

1617
namespace N8T.Infrastructure
@@ -50,10 +51,12 @@ public static TResult SafeGetListQuery<TResult, TResponse>(this HttpContext http
5051
var queryModel = new TResult();
5152
if (!(string.IsNullOrEmpty(query) || query == "{}"))
5253
{
53-
queryModel = JsonConvert.DeserializeObject<TResult>(query);
54+
queryModel = JsonConvert.DeserializeObject<TResult>(query);
5455
}
5556

56-
httpContext?.Response.Headers.Add("x-query", JsonConvert.SerializeObject(queryModel));
57+
httpContext?.Response.Headers.Add("x-query",
58+
JsonConvert.SerializeObject(queryModel,
59+
new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()}));
5760

5861
return queryModel;
5962
}

src/Customer/CustomerService.Application/V1/Endpoints/Commands/CreateCustomer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414

1515
namespace CustomerService.Application.V1.Endpoints.Commands
1616
{
17-
[ApiVersion( "1.0" )]
1817
public class CreateCustomer : BaseAsyncEndpoint.WithRequest<CreateCustomer.Command>.WithoutResponse
1918
{
20-
[HttpPost("/api/customers")]
19+
[ApiVersion( "1.0" )]
20+
[HttpPost("/api/v{version:apiVersion}/customers")]
2121
public override async Task<ActionResult> HandleAsync(Command request, CancellationToken cancellationToken = new())
2222
{
2323
return Ok(await Mediator.Send(request, cancellationToken));
2424
}
2525

2626
public record Command : ICreateCommand<Command.CreateCustomerModel, CustomerDto>
2727
{
28-
public CreateCustomerModel Model { get; init; }
28+
public CreateCustomerModel Model { get; init; } = default!;
2929

3030
public record CreateCustomerModel(string FirstName, string LastName, string Email, Guid CountryId);
3131

src/Gateways/AppGateway/AppGateway.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<ItemGroup>
88
<PackageReference Include="Microsoft.ReverseProxy" Version="1.0.0-preview.9.*" />
9+
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.13.0" />
910
</ItemGroup>
1011

1112
</Project>

src/Gateways/AppGateway/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
1515
.ConfigureWebHostDefaults(webBuilder =>
1616
{
1717
webBuilder.UseStartup<Startup>();
18+
webBuilder.UseKestrel(options => options.AddServerHeader = false);
1819
});
1920
}
2021
}

src/Gateways/AppGateway/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public void ConfigureServices(IServiceCollection services)
2323

2424
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
2525
{
26+
app.UseSecurityHeaders();
27+
2628
if (env.IsDevelopment())
2729
{
2830
app.UseDeveloperExceptionPage();

src/Product/ProductService.Application/V1/Endpoints/Commands/CreateProduct.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212

1313
namespace ProductService.Application.V1.Endpoints.Commands
1414
{
15-
[ApiVersion( "1.0" )]
1615
public class CreateProduct : BaseAsyncEndpoint.WithRequest<CreateProduct.Command>.WithoutResponse
1716
{
18-
[HttpPost("/api/products")]
17+
[ApiVersion( "1.0" )]
18+
[HttpPost("/api/v{version:apiVersion}/products")]
1919
public override async Task<ActionResult> HandleAsync(Command request, CancellationToken cancellationToken = new())
2020
{
2121
return Ok(await Mediator.Send(request, cancellationToken));
2222
}
2323

2424
public record Command : ICreateCommand<Command.CreateProductModel, ProductDto>
2525
{
26-
public CreateProductModel Model { get; init; }
26+
public CreateProductModel Model { get; init; } = default!;
2727

2828
public record CreateProductModel(string Name, int Quantity, decimal Cost, string ProductCodeName);
2929

src/Product/ProductService.Application/V1/Endpoints/Queries/GetProductById.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
namespace ProductService.Application.V1.Endpoints.Queries
1616
{
17-
[ApiVersion( "1.0" )]
1817
public class GetProductById : BaseAsyncEndpoint.WithRequest<Guid>.WithResponse<ProductDto>
1918
{
20-
[HttpGet("/api/products/{id:guid}")]
19+
[ApiVersion( "1.0" )]
20+
[HttpGet("/api/v{version:apiVersion}/products/{id:guid}")]
2121
public override async Task<ActionResult<ProductDto>> HandleAsync(Guid id,
2222
CancellationToken cancellationToken = new())
2323
{

src/Product/ProductService.Application/V1/Endpoints/Queries/GetProducts.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
namespace ProductService.Application.V1.Endpoints.Queries
1818
{
19-
[ApiVersion( "1.0" )]
2019
public class GetProducts : BaseAsyncEndpoint.WithRequest<string>.WithoutResponse
2120
{
22-
[HttpGet("/api/products")]
21+
[ApiVersion( "1.0" )]
22+
[HttpGet("/api/v{version:apiVersion}/products")]
2323
public override async Task<ActionResult> HandleAsync([FromHeader(Name = "x-query")] string query,
2424
CancellationToken cancellationToken = new())
2525
{

0 commit comments

Comments
 (0)