Skip to content

Commit 7ec3e5f

Browse files
Refactor API (#554)
* Remove ODATA * Add OpenAPI and transformers * Update api endpoint (/api) * Remove Swashbuckle * Add SCALAR (Playground) * Refactor customer retrieval order in WorkContextSetter * Refactor authentication services and middleware
1 parent 68790b9 commit 7ec3e5f

File tree

75 files changed

+1364
-2073
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1364
-2073
lines changed

Directory.Packages.props

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
<ItemGroup>
66
<PackageVersion Include="MaxMind.GeoIP2" Version="5.2.0" />
77
<PackageVersion Include="ExcelMapper" Version="5.2.593" />
8+
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
89
<PackageVersion Include="Microsoft.FeatureManagement.AspNetCore" Version="4.0.0" />
910
<PackageVersion Include="NPOI" Version="2.7.2" />
11+
<PackageVersion Include="Scalar.AspNetCore" Version="1.2.72" />
1012
<PackageVersion Include="Scryber.Core" Version="6.0.4-beta" />
1113
<PackageVersion Include="Scryber.Core.OpenType" Version="6.1.0-beta" />
1214
<PackageVersion Include="MailKit" Version="4.9.0" />
@@ -38,13 +40,10 @@
3840
<PackageVersion Include="FluentValidation" Version="11.11.0" />
3941
<PackageVersion Include="Microsoft.AspNetCore.JsonPatch" Version="9.0.0" />
4042
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.0" />
41-
<PackageVersion Include="Microsoft.AspNetCore.OData" Version="9.1.1" />
42-
<PackageVersion Include="MongoDB.AspNetCore.OData" Version="1.1.0" />
43-
<PackageVersion Include="Swashbuckle.AspNetCore" Version="7.2.0" />
44-
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="7.2.0" />
4543
<PackageVersion Include="Microsoft.AspNetCore.Authentication.Facebook" Version="9.0.0" />
4644
<PackageVersion Include="Microsoft.AspNetCore.Authentication.Google" Version="9.0.0" />
4745
<PackageVersion Include="Braintree" Version="5.28.0" />
46+
<PackageVersion Include="System.Linq.Dynamic.Core" Version="1.5.1" />
4847
<PackageVersion Include="System.Xml.XPath.XmlDocument" Version="4.7.0" />
4948
<PackageVersion Include="Stripe.net" Version="47.1.0" />
5049
<PackageVersion Include="elFinder.Net.AspNetCore" Version="1.5.0" />

src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Grand.Infrastructure.Configuration;
66
using Microsoft.AspNetCore.Authentication;
77
using Microsoft.AspNetCore.Authentication.JwtBearer;
8+
using Microsoft.AspNetCore.Authorization;
89
using Microsoft.AspNetCore.Http;
910
using Microsoft.Net.Http.Headers;
1011

@@ -34,12 +35,12 @@ public virtual async Task<Customer> GetAuthenticatedCustomer()
3435
if (string.IsNullOrEmpty(authHeader))
3536
return null;
3637

37-
if (_httpContextAccessor.HttpContext.Request.Path.Value != null
38-
&& !_httpContextAccessor.HttpContext.Request.Path.Value.StartsWith("/odata"))
38+
if (IsApiFrontAuthenticated())
3939
{
4040
customer = await ApiCustomer();
4141
return customer;
4242
}
43+
4344
var authenticateResult = await _httpContextAccessor.HttpContext.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
4445
if (!authenticateResult.Succeeded)
4546
return null;
@@ -55,6 +56,15 @@ public virtual async Task<Customer> GetAuthenticatedCustomer()
5556

5657
return customer;
5758
}
59+
private bool IsApiFrontAuthenticated()
60+
{
61+
var endpoint = _httpContextAccessor.HttpContext.GetEndpoint();
62+
if (endpoint == null) return false;
63+
64+
var authorizeAttributes = endpoint.Metadata.GetOrderedMetadata<AuthorizeAttribute>();
65+
return authorizeAttributes.Any(attr => attr.AuthenticationSchemes?.Contains(FrontendAPIConfig.AuthenticationScheme) == true);
66+
}
67+
5868

5969
private async Task<Customer> ApiCustomer()
6070
{

src/Business/Grand.Business.Authentication/Services/CookieAuthenticationService.cs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public CookieAuthenticationService(
5555
private readonly IGroupService _groupService;
5656
private readonly IHttpContextAccessor _httpContextAccessor;
5757
private readonly SecurityConfig _securityConfig;
58-
private Customer _cachedCustomer;
5958

6059
#endregion
6160

@@ -112,22 +111,15 @@ public virtual async Task SignIn(Customer customer, bool isPersistent)
112111
{
113112
_httpContextAccessor.HttpContext.Response.Cookies.Delete(CustomerCookieName);
114113

115-
await _httpContextAccessor.HttpContext.SignInAsync(
116-
GrandCookieAuthenticationDefaults.AuthenticationScheme, userPrincipal, authenticationProperties);
114+
await _httpContextAccessor.HttpContext.SignInAsync(GrandCookieAuthenticationDefaults.AuthenticationScheme, userPrincipal, authenticationProperties);
117115
}
118-
119-
//cache authenticated customer
120-
_cachedCustomer = customer;
121116
}
122117

123118
/// <summary>
124119
/// Sign out customer
125120
/// </summary>
126121
public virtual async Task SignOut()
127122
{
128-
//Firstly reset cached customer
129-
_cachedCustomer = null;
130-
131123
//and then sign out customer from the present scheme of authentication
132124
if (_httpContextAccessor.HttpContext != null)
133125
{
@@ -145,15 +137,7 @@ await _httpContextAccessor.HttpContext.SignOutAsync(GrandCookieAuthenticationDef
145137
/// <returns>Customer</returns>
146138
public virtual async Task<Customer> GetAuthenticatedCustomer()
147139
{
148-
//check if there is a cached customer
149-
if (_cachedCustomer != null)
150-
return _cachedCustomer;
151-
152-
//get the authenticated user identity
153-
if (_httpContextAccessor.HttpContext == null) return _cachedCustomer;
154-
var authenticateResult =
155-
await _httpContextAccessor.HttpContext.AuthenticateAsync(GrandCookieAuthenticationDefaults
156-
.AuthenticationScheme);
140+
var authenticateResult = await _httpContextAccessor.HttpContext.AuthenticateAsync(GrandCookieAuthenticationDefaults.AuthenticationScheme);
157141
if (!authenticateResult.Succeeded)
158142
return null;
159143

@@ -195,10 +179,7 @@ await _httpContextAccessor.HttpContext.AuthenticateAsync(GrandCookieAuthenticati
195179
if (customer is not { Active: true } || customer.Deleted || !await _groupService.IsRegistered(customer))
196180
return null;
197181

198-
//Cache the authenticated customer
199-
_cachedCustomer = customer;
200-
201-
return _cachedCustomer;
182+
return customer;
202183
}
203184

204185
/// <summary>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Grand.SharedKernel.Attributes;
2+
3+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
4+
public sealed class ApiGroupAttribute : Attribute
5+
{
6+
public string GroupName { get; }
7+
8+
public ApiGroupAttribute(string groupName)
9+
{
10+
GroupName = groupName;
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Grand.SharedKernel.Extensions;
2+
3+
public static class ApiConstants
4+
{
5+
public const string ApiGroupNameV1 = "v1";
6+
public const string ApiGroupNameV2 = "v2";
7+
}

src/Modules/Grand.Module.Api/ApiExplorer/ApiParameterContext.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)