-
Notifications
You must be signed in to change notification settings - Fork 0
new authorization command handler #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
7 changes: 7 additions & 0 deletions
7
src/Core/Applications.Abstractions/Authorizes/AllowAnonymousAttribute.cs
This file contains hidden or 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,7 @@ | ||
| namespace Honamic.Framework.Applications.Authorizes; | ||
|
|
||
| [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
| public class AllowAnonymousAttribute : Attribute | ||
| { | ||
|
|
||
| } |
17 changes: 17 additions & 0 deletions
17
src/Core/Applications.Abstractions/Authorizes/AuthorizeAttribute.cs
This file contains hidden or 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,17 @@ | ||
| namespace Honamic.Framework.Applications.Authorizes; | ||
|
|
||
| [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] | ||
| public class AuthorizeAttribute : Attribute | ||
| { | ||
| public AuthorizeAttribute() | ||
| { | ||
| Permissions = null; | ||
| } | ||
|
|
||
| public AuthorizeAttribute(params string[] permissions) | ||
| { | ||
| Permissions = permissions; | ||
| } | ||
|
|
||
| public string[]? Permissions { get; } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/Core/Applications.Abstractions/Authorizes/DynamicAuthorizeAttribute.cs
This file contains hidden or 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,7 @@ | ||
| namespace Honamic.Framework.Applications.Authorizes; | ||
|
|
||
| [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] | ||
| public class DynamicAuthorizeAttribute : Attribute | ||
| { | ||
|
|
||
| } |
11 changes: 11 additions & 0 deletions
11
src/Core/Applications.Abstractions/Authorizes/IAuthorization.cs
This file contains hidden or 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,11 @@ | ||
| namespace Honamic.Framework.Applications.Authorizes; | ||
|
|
||
| public interface IAuthorization | ||
| { | ||
| [Obsolete("Use HaveAccessAsync instead. This method will be removed in future versions.")] | ||
| bool HaveAccess(string permission); | ||
|
|
||
| Task<bool> HaveAccessAsync(string permission); | ||
|
|
||
| bool IsAuthenticated(); | ||
| } |
This file contains hidden or 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
106 changes: 106 additions & 0 deletions
106
src/Core/Applications/CommandHandlerDecorators/AuthorizeCommandHandlerDecorator.cs
This file contains hidden or 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,106 @@ | ||
| using Honamic.Framework.Applications.Authorizes; | ||
| using Honamic.Framework.Applications.Exceptions; | ||
| using Honamic.Framework.Commands; | ||
| using System.Reflection; | ||
|
|
||
| namespace Honamic.Framework.Applications.CommandHandlerDecorators; | ||
|
|
||
| public class AuthorizeCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand> | ||
| where TCommand : ICommand | ||
| { | ||
| private readonly ICommandHandler<TCommand> _commandHandler; | ||
| private readonly IAuthorization _authorization; | ||
|
|
||
| public AuthorizeCommandHandlerDecorator(ICommandHandler<TCommand> commandHandler, IAuthorization authorization) | ||
| { | ||
| _commandHandler = commandHandler; | ||
| _authorization = authorization; | ||
| } | ||
|
|
||
| public async Task HandleAsync(TCommand command, CancellationToken cancellationToken) | ||
| { | ||
| await _authorization.AuthorizeCommandAttributes(typeof(TCommand)); | ||
|
|
||
| await _commandHandler.HandleAsync(command, cancellationToken); | ||
| } | ||
| } | ||
|
|
||
| public class AuthorizeCommandHandlerDecorator<TCommand, TResponse> : ICommandHandler<TCommand, TResponse> | ||
| where TCommand : ICommand<TResponse> | ||
| { | ||
| private readonly ICommandHandler<TCommand, TResponse> _commandHandler; | ||
| private readonly IAuthorization _authorization; | ||
|
|
||
| public AuthorizeCommandHandlerDecorator(ICommandHandler<TCommand, TResponse> commandHandler, IAuthorization authorization) | ||
| { | ||
| _commandHandler = commandHandler; | ||
| _authorization = authorization; | ||
| } | ||
|
|
||
| public async Task<TResponse> HandleAsync(TCommand command, CancellationToken cancellationToken) | ||
| { | ||
| await _authorization.AuthorizeCommandAttributes(typeof(TCommand)); | ||
|
|
||
| return await _commandHandler.HandleAsync(command, cancellationToken); | ||
| } | ||
| } | ||
|
|
||
| internal static class AuthorizeCommandHandlerDecoratorHelper | ||
| { | ||
|
|
||
| public static async Task AuthorizeCommandAttributes(this IAuthorization authorization, Type type) | ||
| { | ||
| await authorization.AuthorizeWithAttributes(type); | ||
|
|
||
| await authorization.AuthorizeWithDynamicPermissions(type); | ||
| } | ||
|
|
||
| public static async Task AuthorizeWithDynamicPermissions(this IAuthorization authorization, Type type) | ||
| { | ||
| var dynamicAuthorizeAttribute = type.GetCustomAttribute<DynamicAuthorizeAttribute>(); | ||
|
|
||
| if (dynamicAuthorizeAttribute is not null) | ||
| { | ||
| if (!authorization.IsAuthenticated()) | ||
| { | ||
| throw new UnauthenticatedException(); | ||
| } | ||
|
|
||
| string dynamicPermission = CalculatePermissionName(type); | ||
|
|
||
| if (!await authorization.HaveAccessAsync(dynamicPermission)) | ||
| { | ||
| throw new UnauthorizedException(dynamicPermission); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static async Task AuthorizeWithAttributes(this IAuthorization authorization, Type type) | ||
| { | ||
| var authorizeAttribute = type.GetCustomAttribute<AuthorizeAttribute>(); | ||
|
|
||
| if (authorizeAttribute is not null) | ||
| { | ||
| if (!authorization.IsAuthenticated()) | ||
| { | ||
| throw new UnauthenticatedException(); | ||
| } | ||
|
|
||
| if (authorizeAttribute.Permissions?.Length > 0) | ||
| { | ||
| foreach (var permission in authorizeAttribute.Permissions) | ||
| { | ||
| if (!await authorization.HaveAccessAsync(permission)) | ||
| { | ||
| throw new UnauthorizedException(permission); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static string CalculatePermissionName(Type type) | ||
| { | ||
| return type.Name; | ||
| } | ||
| } |
107 changes: 107 additions & 0 deletions
107
src/Core/Applications/CommandHandlerDecorators/ExceptionCommandHandlerDecorator.cs
This file contains hidden or 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,107 @@ | ||
| using Honamic.Framework.Applications.Exceptions; | ||
| using Honamic.Framework.Applications.Results; | ||
| using Honamic.Framework.Commands; | ||
| using Honamic.Framework.Domain; | ||
|
|
||
| namespace Honamic.Framework.Applications.CommandHandlerDecorators; | ||
|
|
||
| public class ExceptionCommandHandlerDecorator<TCommand, TResponse> : ICommandHandler<TCommand, TResponse> | ||
| where TCommand : ICommand<TResponse> | ||
| { | ||
| private readonly ICommandHandler<TCommand, TResponse> _commandHandler; | ||
|
|
||
| public ExceptionCommandHandlerDecorator(ICommandHandler<TCommand, TResponse> commandHandler) | ||
| { | ||
| _commandHandler = commandHandler; | ||
| } | ||
|
|
||
| public async Task<TResponse> HandleAsync(TCommand command, CancellationToken cancellationToken) | ||
| { | ||
| TResponse result; | ||
| try | ||
| { | ||
| result = await _commandHandler.HandleAsync(command, cancellationToken); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| if (IsResultOriented(typeof(TResponse))) | ||
| { | ||
| result = CreateResultWithError(typeof(TResponse), ex); | ||
| return result; | ||
| } | ||
|
|
||
| throw; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private TResponse CreateResultWithError(Type type, Exception ex) | ||
|
||
| { | ||
| var resultObject = CreateResult(type); | ||
|
|
||
| if (resultObject is Result result) | ||
| { | ||
| switch (ex) | ||
| { | ||
| case UnauthenticatedException: | ||
| result.SetStatusAsUnauthenticated(); | ||
| result.AppendError(ex.Message); | ||
| break; | ||
| case UnauthorizedException: | ||
| result.SetStatusAsUnauthorized(); | ||
| result.AppendError(ex.Message); | ||
| break; | ||
| case BusinessException businessException: | ||
| result.Status = ResultStatus.ValidationError; | ||
| var code = businessException.GetCode(); | ||
| var message = businessException.GetMessage(); | ||
| result.AppendError(message, null, code); | ||
| break; | ||
| default: | ||
| result.SetStatusAsUnhandledExceptionWithSorryError(); | ||
| result.AppendError(ex.ToString(), "Exception"); | ||
| break; | ||
| } | ||
| return resultObject; | ||
| } | ||
|
|
||
| // If we can't cast to Result, we have a serious error | ||
| throw new ArgumentException($"Expected a Result type but got {type.FullName}"); | ||
| } | ||
|
|
||
| private TResponse CreateResult(Type type) | ||
| { | ||
| // For non-generic Result | ||
| if (type == typeof(Result)) | ||
| { | ||
| return (TResponse)(object)new Result(); | ||
| } | ||
|
|
||
| // For Result<T> | ||
| if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Result<>)) | ||
| { | ||
| var genericArgType = type.GenericTypeArguments[0]; | ||
| var resultType = typeof(Result<>).MakeGenericType(genericArgType); | ||
| return (TResponse)Activator.CreateInstance(resultType); | ||
| } | ||
|
|
||
| return default; | ||
| } | ||
|
|
||
| private bool IsResultOriented(Type type) | ||
| { | ||
| if (type == typeof(Result)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (type.IsGenericType | ||
| && type.GetGenericTypeDefinition() == typeof(Result<>)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider removing the commented-out '[DynamicAuthorize]' attribute if it is no longer required to reduce code clutter.