-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(apis-user): Resolved issues with updates to domain classes
Resolved issues with updates to domain classes Added Identity service
- Loading branch information
1 parent
c252d76
commit 455f6c0
Showing
142 changed files
with
3,659 additions
and
471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
libs/core/dotnet/application/Attributes/AuthorizeAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace OpenSystem.Core.DotNet.Application.Attributes | ||
{ | ||
/// <summary> | ||
/// Specifies the class this attribute is applied to requires authorization. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] | ||
public class AuthorizeAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AuthorizeAttribute"/> class. | ||
/// </summary> | ||
public AuthorizeAttribute() { } | ||
|
||
/// <summary> | ||
/// Gets or sets a comma delimited list of roles that are allowed to access the resource. | ||
/// </summary> | ||
public string Roles { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets the policy name that determines access to the resource. | ||
/// </summary> | ||
public string Policy { get; set; } = string.Empty; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
libs/core/dotnet/application/Behaviors/AuthorizationBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System.Reflection; | ||
using MediatR; | ||
using OpenSystem.Core.DotNet.Application.Attributes; | ||
using OpenSystem.Core.DotNet.Application.Interfaces; | ||
using OpenSystem.Core.DotNet.Domain.Exceptions; | ||
|
||
namespace OpenSystem.Core.DotNet.Application.Behaviors | ||
{ | ||
public class AuthorizationBehavior<TRequest, TResponse> | ||
: IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : MediatR.IRequest<TResponse> | ||
{ | ||
private readonly ICurrentUserService _currentUserService; | ||
|
||
private readonly IIdentityService _identityService; | ||
|
||
public AuthorizationBehavior(ICurrentUserService currentUserService, | ||
IIdentityService identityService) | ||
{ | ||
_currentUserService = currentUserService; | ||
_identityService = identityService; | ||
} | ||
|
||
public async Task<TResponse> Handle(TRequest request, | ||
RequestHandlerDelegate<TResponse> next, | ||
CancellationToken cancellationToken) | ||
{ | ||
var authorizeAttributes = request.GetType().GetCustomAttributes<AuthorizeAttribute>(); | ||
|
||
if (authorizeAttributes.Any()) | ||
{ | ||
// Must be authenticated user | ||
if (_currentUserService.UserId == null) | ||
{ | ||
throw new UnauthorizedAccessException(); | ||
} | ||
|
||
// Role-based authorization | ||
var authorizeAttributesWithRoles = authorizeAttributes.Where(a => | ||
!string.IsNullOrWhiteSpace(a.Roles)); | ||
|
||
if (authorizeAttributesWithRoles.Any()) | ||
{ | ||
var authorized = false; | ||
foreach (var roles in authorizeAttributesWithRoles.Select(a => | ||
a.Roles.Split(','))) | ||
{ | ||
foreach (var role in roles) | ||
{ | ||
var isInRole = await _identityService.IsInRoleAsync(_currentUserService.UserId, | ||
role.Trim()); | ||
if (isInRole) | ||
{ | ||
authorized = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// Must be a member of at least one role in roles | ||
if (!authorized) | ||
{ | ||
throw new ForbiddenAccessException(); | ||
} | ||
} | ||
|
||
// Policy-based authorization | ||
var authorizeAttributesWithPolicies = authorizeAttributes.Where(a => | ||
!string.IsNullOrWhiteSpace(a.Policy)); | ||
if (authorizeAttributesWithPolicies.Any()) | ||
{ | ||
foreach (var policy in authorizeAttributesWithPolicies.Select(a => | ||
a.Policy)) | ||
{ | ||
var authorized = await _identityService.AuthorizeAsync(_currentUserService.UserId, policy); | ||
|
||
if (!authorized) | ||
{ | ||
throw new ForbiddenAccessException(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// User is authorized / authorization not required | ||
return await next(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
libs/core/dotnet/application/Behaviors/PerformanceBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using OpenSystem.Core.DotNet.Application.Interfaces; | ||
using Microsoft.Extensions.Logging; | ||
using System.Diagnostics; | ||
using MediatR; | ||
|
||
namespace OpenSystem.Core.DotNet.Application.Behaviors | ||
{ | ||
public class PerformanceBehavior<TRequest, TResponse> | ||
: IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : MediatR.IRequest<TResponse> | ||
{ | ||
private readonly Stopwatch _timer; | ||
|
||
private readonly ILogger<TRequest> _logger; | ||
|
||
private readonly ICurrentUserService _currentUserService; | ||
|
||
private readonly IIdentityService _identityService; | ||
|
||
public PerformanceBehavior( | ||
ILogger<TRequest> logger, | ||
ICurrentUserService currentUserService, | ||
IIdentityService identityService) | ||
{ | ||
_timer = new Stopwatch(); | ||
|
||
_logger = logger; | ||
_currentUserService = currentUserService; | ||
_identityService = identityService; | ||
} | ||
|
||
public async Task<TResponse> Handle(TRequest request, | ||
RequestHandlerDelegate<TResponse> next, | ||
CancellationToken cancellationToken) | ||
{ | ||
_timer.Start(); | ||
|
||
var response = await next(); | ||
|
||
_timer.Stop(); | ||
|
||
var elapsedMilliseconds = _timer.ElapsedMilliseconds; | ||
|
||
if (elapsedMilliseconds > 500) | ||
{ | ||
var requestName = typeof(TRequest).Name; | ||
var userId = _currentUserService.UserId ?? string.Empty; | ||
var userName = string.Empty; | ||
|
||
if (!string.IsNullOrEmpty(userId)) | ||
{ | ||
userName = await _identityService.GetUserNameAsync(userId); | ||
} | ||
|
||
_logger.LogWarning("CleanArchitecture Long Running Request: {Name} ({ElapsedMilliseconds} milliseconds) {@UserId} {@UserName} {@Request}", | ||
requestName, | ||
elapsedMilliseconds, | ||
userId, | ||
userName, | ||
request); | ||
} | ||
|
||
return response; | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.