Skip to content

Commit e7462dc

Browse files
Revert "refactor(Core.Application): update ISecuredRequest interface and related classes"
This reverts commit 3f877ea.
1 parent 5233c37 commit e7462dc

File tree

5 files changed

+74
-67
lines changed

5 files changed

+74
-67
lines changed

CorePackages.sln

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
Microsoft Visual Studio Solution File, Format Version 12.00
23
# Visual Studio Version 17
34
VisualStudioVersion = 17.5.33424.131
@@ -71,12 +72,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7C1F8BBD-7E3
7172
EndProject
7273
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core.Persistence.DependencyInjection", "src\Core.Persistence.DependencyInjection\Core.Persistence.DependencyInjection.csproj", "{21482084-82B8-41FA-8477-ED78B3037375}"
7374
EndProject
74-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{2D2E79DB-3C2D-42DE-AB71-4FE1AFEF9791}"
75-
EndProject
76-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core.Application.BenchmarkTests", "src\tests\Core.Application.BenchmarkTests\Core.Application.BenchmarkTests.csproj", "{B9213521-B60F-459A-B12D-F168797DC6E0}"
77-
EndProject
78-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core.Application.Tests", "src\tests\Core.Application.Tests\Core.Application.Tests\Core.Application.Tests.csproj", "{4D25A18E-E56D-4C02-AE8A-CCC4D884D330}"
79-
EndProject
8075
Global
8176
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8277
Debug|Any CPU = Debug|Any CPU
@@ -87,10 +82,6 @@ Global
8782
{0542DF94-63CD-430F-9AF5-AE84D88319BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
8883
{0542DF94-63CD-430F-9AF5-AE84D88319BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
8984
{0542DF94-63CD-430F-9AF5-AE84D88319BC}.Release|Any CPU.Build.0 = Release|Any CPU
90-
{4D25A18E-E56D-4C02-AE8A-CCC4D884D330}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
91-
{4D25A18E-E56D-4C02-AE8A-CCC4D884D330}.Debug|Any CPU.Build.0 = Debug|Any CPU
92-
{4D25A18E-E56D-4C02-AE8A-CCC4D884D330}.Release|Any CPU.ActiveCfg = Release|Any CPU
93-
{4D25A18E-E56D-4C02-AE8A-CCC4D884D330}.Release|Any CPU.Build.0 = Release|Any CPU
9485
{D5B3C51B-75DF-4A33-BCBB-BAF2BF076617}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
9586
{D5B3C51B-75DF-4A33-BCBB-BAF2BF076617}.Debug|Any CPU.Build.0 = Debug|Any CPU
9687
{D5B3C51B-75DF-4A33-BCBB-BAF2BF076617}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -191,17 +182,12 @@ Global
191182
{21482084-82B8-41FA-8477-ED78B3037375}.Debug|Any CPU.Build.0 = Debug|Any CPU
192183
{21482084-82B8-41FA-8477-ED78B3037375}.Release|Any CPU.ActiveCfg = Release|Any CPU
193184
{21482084-82B8-41FA-8477-ED78B3037375}.Release|Any CPU.Build.0 = Release|Any CPU
194-
{B9213521-B60F-459A-B12D-F168797DC6E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
195-
{B9213521-B60F-459A-B12D-F168797DC6E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
196-
{B9213521-B60F-459A-B12D-F168797DC6E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
197-
{B9213521-B60F-459A-B12D-F168797DC6E0}.Release|Any CPU.Build.0 = Release|Any CPU
198185
EndGlobalSection
199186
GlobalSection(SolutionProperties) = preSolution
200187
HideSolutionNode = FALSE
201188
EndGlobalSection
202189
GlobalSection(NestedProjects) = preSolution
203190
{0542DF94-63CD-430F-9AF5-AE84D88319BC} = {7C1F8BBD-7E3D-44BE-81E6-55EB807C14B9}
204-
{4D25A18E-E56D-4C02-AE8A-CCC4D884D330} = {2D2E79DB-3C2D-42DE-AB71-4FE1AFEF9791}
205191
{D5B3C51B-75DF-4A33-BCBB-BAF2BF076617} = {7C1F8BBD-7E3D-44BE-81E6-55EB807C14B9}
206192
{1B0EA46B-8C78-4E44-8171-D9F142356CE8} = {7C1F8BBD-7E3D-44BE-81E6-55EB807C14B9}
207193
{B5C9694A-C714-465D-836A-8C77366B4E03} = {7C1F8BBD-7E3D-44BE-81E6-55EB807C14B9}
@@ -229,8 +215,6 @@ Global
229215
{8ED08D8F-DAC6-4BD1-9A1F-69704AD8368F} = {7C1F8BBD-7E3D-44BE-81E6-55EB807C14B9}
230216
{5F448267-A147-4C86-9BEA-797AE93B0BBA} = {7C1F8BBD-7E3D-44BE-81E6-55EB807C14B9}
231217
{21482084-82B8-41FA-8477-ED78B3037375} = {7C1F8BBD-7E3D-44BE-81E6-55EB807C14B9}
232-
{2D2E79DB-3C2D-42DE-AB71-4FE1AFEF9791} = {7C1F8BBD-7E3D-44BE-81E6-55EB807C14B9}
233-
{B9213521-B60F-459A-B12D-F168797DC6E0} = {2D2E79DB-3C2D-42DE-AB71-4FE1AFEF9791}
234218
EndGlobalSection
235219
GlobalSection(ExtensibilityGlobals) = postSolution
236220
SolutionGuid = {9E988190-9FBF-4E2E-9B3A-35C418139397}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace NArchitecture.Core.Application.Dtos;
4+
5+
public class UserForLoginDto : IDto
6+
{
7+
public required string Email { get; set; }
8+
9+
[JsonIgnore]
10+
public string Password { get; set; }
11+
12+
[JsonIgnore]
13+
public string? AuthenticatorCode { get; set; }
14+
15+
public UserForLoginDto()
16+
{
17+
Email = string.Empty;
18+
Password = string.Empty;
19+
}
20+
21+
public UserForLoginDto(string email, string password)
22+
{
23+
Email = email;
24+
Password = password;
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace NArchitecture.Core.Application.Dtos;
4+
5+
public class UserForRegisterDto : IDto
6+
{
7+
public required string Email { get; set; }
8+
9+
[JsonIgnore]
10+
public string Password { get; set; }
11+
12+
public UserForRegisterDto()
13+
{
14+
Email = string.Empty;
15+
Password = string.Empty;
16+
}
17+
18+
public UserForRegisterDto(string email, string password)
19+
{
20+
Email = email;
21+
Password = password;
22+
}
23+
}
Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,44 @@
1-
using System.Security.Authentication;
21
using MediatR;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.IdentityModel.Tokens;
34
using NArchitecture.Core.CrossCuttingConcerns.Exception.Types;
45
using NArchitecture.Core.Security.Constants;
6+
using NArchitecture.Core.Security.Extensions;
57

68
namespace NArchitecture.Core.Application.Pipelines.Authorization;
79

810
public class AuthorizationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
911
where TRequest : IRequest<TResponse>, ISecuredRequest
1012
{
11-
public virtual async Task<TResponse> Handle(
13+
private readonly IHttpContextAccessor _httpContextAccessor;
14+
15+
public AuthorizationBehavior(IHttpContextAccessor httpContextAccessor)
16+
{
17+
_httpContextAccessor = httpContextAccessor;
18+
}
19+
20+
public async Task<TResponse> Handle(
1221
TRequest request,
1322
RequestHandlerDelegate<TResponse> next,
1423
CancellationToken cancellationToken
1524
)
1625
{
17-
// Is Authenticated
18-
if (request.IdentityRoles == null)
19-
throwAuthenticationException();
26+
if (!_httpContextAccessor.HttpContext.User.Claims.Any())
27+
throw new AuthorizationException("You are not authenticated.");
2028

21-
// Is Authorized
22-
if (!request.RequiredRoleClaims.IsEmpty)
29+
if (request.Roles.Any())
2330
{
24-
if (!request.IdentityRoles!.GetEnumerator().MoveNext())
25-
throwAuthorizationException();
26-
27-
if (
28-
!(
29-
request.IdentityRoles!.Contains(GeneralOperationClaims.Admin)
30-
|| isHasRequiredRole(request.IdentityRoles, request.RequiredRoleClaims)
31+
ICollection<string>? userRoleClaims = _httpContextAccessor.HttpContext.User.GetRoleClaims() ?? [];
32+
bool isNotMatchedAUserRoleClaimWithRequestRoles = userRoleClaims
33+
.FirstOrDefault(userRoleClaim =>
34+
userRoleClaim == GeneralOperationClaims.Admin || request.Roles.Contains(userRoleClaim)
3135
)
32-
)
33-
throwAuthorizationException();
36+
.IsNullOrEmpty();
37+
if (isNotMatchedAUserRoleClaimWithRequestRoles)
38+
throw new AuthorizationException("You are not authorized.");
3439
}
3540

36-
return await next();
37-
}
38-
39-
private bool isHasRequiredRole(IEnumerable<string> identityRoles, ReadOnlySpan<char> requiredRoleClaims)
40-
{
41-
bool isMatch = false;
42-
foreach (var role in identityRoles)
43-
for (int i = 0; i < requiredRoleClaims.Length; ++i)
44-
{
45-
if (requiredRoleClaims[i] == ',')
46-
continue;
47-
48-
if (requiredRoleClaims[i] == role[i])
49-
isMatch = true;
50-
else
51-
{
52-
isMatch = false;
53-
break;
54-
}
55-
}
56-
57-
return isMatch;
58-
}
59-
60-
protected virtual void throwAuthenticationException()
61-
{
62-
throw new AuthenticationException("You are not authenticated.");
63-
}
64-
65-
protected virtual void throwAuthorizationException()
66-
{
67-
throw new AuthorizationException("You are not authorized.");
41+
TResponse response = await next();
42+
return response;
6843
}
6944
}

src/Core.Application/Pipelines/Authorization/ISecuredRequest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22

33
public interface ISecuredRequest
44
{
5-
public IEnumerable<string> IdentityRoles { get; set; }
6-
public ReadOnlySpan<char> RequiredRoleClaims { get; }
5+
public string[] Roles { get; }
76
}

0 commit comments

Comments
 (0)