Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Refactor Swagger for Dev-Only, Extract CORS to CorsStartup (#539)

Krzysztof Pajak229c80c3de64
diff --git a/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs b/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs
index 1ba913def..c84bf95e6 100644
--- a/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs
+++ b/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs
@@ -15,8 +15,6 @@ public class ApiAuthenticationService : IApiAuthenticationService
private readonly ICustomerService _customerService;
private readonly IGroupService _groupService;
private readonly IHttpContextAccessor _httpContextAccessor;
- private Customer _cachedCustomer;
-
public ApiAuthenticationService(
ICustomerService customerService,
@@ -29,10 +27,6 @@ public class ApiAuthenticationService : IApiAuthenticationService
public virtual async Task<Customer> GetAuthenticatedCustomer()
{
- //whether there is a cached customer
- if (_cachedCustomer != null)
- return _cachedCustomer;
-
Customer customer = null;
if (_httpContextAccessor.HttpContext == null) return null;
@@ -44,13 +38,9 @@ public class ApiAuthenticationService : IApiAuthenticationService
&& !_httpContextAccessor.HttpContext.Request.Path.Value.StartsWith("/odata"))
{
customer = await ApiCustomer();
- if (customer == null) return null;
- _cachedCustomer = customer;
- return _cachedCustomer;
+ return customer;
}
-
- var authenticateResult =
- await _httpContextAccessor.HttpContext.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
+ var authenticateResult = await _httpContextAccessor.HttpContext.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
if (!authenticateResult.Succeeded)
return null;
@@ -63,25 +53,15 @@ public class ApiAuthenticationService : IApiAuthenticationService
if (customer is not { Active: true } || customer.Deleted || !await _groupService.IsRegistered(customer))
return null;
- //cache authenticated customer
- _cachedCustomer = customer;
-
- return _cachedCustomer;
+ return customer;
}
private async Task<Customer> ApiCustomer()
{
Customer customer = null;
- var authResult =
- await _httpContextAccessor.HttpContext!.AuthenticateAsync(FrontendAPIConfig.AuthenticationScheme);
+ var authResult = await _httpContextAccessor.HttpContext!.AuthenticateAsync(FrontendAPIConfig.AuthenticationScheme);
if (!authResult.Succeeded)
- {
- _httpContextAccessor.HttpContext.Response.StatusCode = 400;
- _httpContextAccessor.HttpContext.Response.ContentType = "text/plain";
- if (authResult.Failure != null)
- await _httpContextAccessor.HttpContext.Response.WriteAsync(authResult.Failure.Message);
return await _customerService.GetCustomerBySystemName(SystemCustomerNames.Anonymous);
- }
var email = authResult.Principal.Claims.ToList().FirstOrDefault(x => x.Type == "Email")?.Value;
if (email is null)
@@ -91,9 +71,7 @@ public class ApiAuthenticationService : IApiAuthenticationService
if (id != null) customer = await _customerService.GetCustomerByGuid(Guid.Parse(id));
}
else
- {
customer = await _customerService.GetCustomerByEmail(email);
- }
return customer;
}
diff --git a/src/Modules/Grand.Module.Api/Infrastructure/CorsStartup.cs b/src/Modules/Grand.Module.Api/Infrastructure/CorsStartup.cs
new file mode 100644
index 000000000..b0aad3237
--- /dev/null
+++ b/src/Modules/Grand.Module.Api/Infrastructure/CorsStartup.cs
@@ -0,0 +1,29 @@
+using Grand.Infrastructure;
+using Grand.Module.Api.Constants;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace Grand.Module.Api.Infrastructure;
+
+public class CorsStartup : IStartupApplication
+{
+ public void Configure(WebApplication application, IWebHostEnvironment webHostEnvironment)
+ {
+ application.UseCors(Configurations.CorsPolicyName);
+ }
+
+ public void ConfigureServices(IServiceCollection services,
+ IConfiguration configuration)
+ {
+ services.AddCors(options =>
+ {
+ options.AddPolicy(Configurations.CorsPolicyName,
+ builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
+ });
+ }
+
+ public int Priority => 0;
+ public bool BeforeConfigure => true;
+}
\ No newline at end of file
diff --git a/src/Modules/Grand.Module.Api/Infrastructure/ODataStartup.cs b/src/Modules/Grand.Module.Api/Infrastructure/ODataStartup.cs
index 208da7bca..88f63a31e 100644
--- a/src/Modules/Grand.Module.Api/Infrastructure/ODataStartup.cs
+++ b/src/Modules/Grand.Module.Api/Infrastructure/ODataStartup.cs
@@ -35,8 +35,6 @@ public class ODataStartup : IStartupApplication
{
public void Configure(WebApplication application, IWebHostEnvironment webHostEnvironment)
{
- var apiConfig = application.Services.GetService<BackendAPIConfig>();
- if (apiConfig.Enabled) application.UseCors(Configurations.CorsPolicyName);
}
public void ConfigureServices(IServiceCollection services,
@@ -48,12 +46,6 @@ public class ODataStartup : IStartupApplication
//register RequestHandler
RegisterRequestHandler(services);
- //cors
- services.AddCors(options =>
- {
- options.AddPolicy(Configurations.CorsPolicyName,
- builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
- });
//Add OData
services.AddControllers(options =>
{
diff --git a/src/Modules/Grand.Module.Api/Infrastructure/SwaggerStartup.cs b/src/Modules/Grand.Module.Api/Infrastructure/SwaggerStartup.cs
index a7ec1e541..57dc59aa0 100644
--- a/src/Modules/Grand.Module.Api/Infrastructure/SwaggerStartup.cs
+++ b/src/Modules/Grand.Module.Api/Infrastructure/SwaggerStartup.cs
@@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.OData;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
namespace Grand.Module.Api.Infrastructure;
@@ -18,11 +19,10 @@ public class SwaggerStartup : IStartupApplication
{
var backendApiConfig = application.Services.GetService<BackendAPIConfig>();
var frontApiConfig = application.Services.GetService<FrontendAPIConfig>();
- var swagger = application.Services.GetService<IConfiguration>().GetValue<bool>("UseSwagger");
if (backendApiConfig.Enabled)
application.UseODataQueryRequest();
- if (swagger && (backendApiConfig.Enabled || frontApiConfig.Enabled))
+ if (webHostEnvironment.IsDevelopment() && (backendApiConfig.Enabled || frontApiConfig.Enabled))
{
application.UseSwagger();
application.UseSwaggerUI(c =>
@@ -40,8 +40,9 @@ public class SwaggerStartup : IStartupApplication
{
var backendApiConfig = services.BuildServiceProvider().GetService<BackendAPIConfig>();
var frontApiConfig = services.BuildServiceProvider().GetService<FrontendAPIConfig>();
- var swagger = configuration.GetValue<bool>("UseSwagger");
- if (swagger && (backendApiConfig.Enabled || frontApiConfig.Enabled))
+ var webHostEnvironment = services.BuildServiceProvider().GetService<IWebHostEnvironment>();
+
+ if (webHostEnvironment.IsDevelopment() && (backendApiConfig.Enabled || frontApiConfig.Enabled))
services.AddSwaggerGen(c =>
{
if (backendApiConfig.Enabled)
diff --git a/src/Web/Grand.Web.Admin/App_Data/appsettings.json b/src/Web/Grand.Web.Admin/App_Data/appsettings.json
index b2cd45740..584b2b7bb 100644
--- a/src/Web/Grand.Web.Admin/App_Data/appsettings.json
+++ b/src/Web/Grand.Web.Admin/App_Data/appsettings.json
@@ -194,7 +194,6 @@
//generate system model
"SystemModel": true
},
- "UseSwagger": false,
"Logging": {
"LogLevel": {
"Default": "Information",
diff --git a/src/Web/Grand.Web.Vendor/App_Data/appsettings.json b/src/Web/Grand.Web.Vendor/App_Data/appsettings.json
index ef3ec6f17..bc02b7fd1 100644
--- a/src/Web/Grand.Web.Vendor/App_Data/appsettings.json
+++ b/src/Web/Grand.Web.Vendor/App_Data/appsettings.json
@@ -194,7 +194,6 @@
//generate system model
"SystemModel": true
},
- "UseSwagger": false,
"Logging": {
"LogLevel": {
"Default": "Information",
diff --git a/src/Web/Grand.Web/App_Data/appsettings.json b/src/Web/Grand.Web/App_Data/appsettings.json
index dd6effd0f..c46438c4b 100644
--- a/src/Web/Grand.Web/App_Data/appsettings.json
+++ b/src/Web/Grand.Web/App_Data/appsettings.json
@@ -213,7 +213,6 @@
//generate system model
"SystemModel": true
},
- "UseSwagger": true,
"Logging": {
"LogLevel": {
"Default": "Information",