| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Http; |
| | | 3 | | using Microsoft.AspNetCore.Mvc.Abstractions; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Util.WebApi.Security.Extensions; |
| | | 6 | | |
| | | 7 | | /// <summary>Detects endpoints exempt from authentication, marked with this library's |
| | | 8 | | /// <see cref="Attributes.AllowAnonymousAttribute"/> or ASP.NET Core's own <c>[AllowAnonymous]</c>.</summary> |
| | | 9 | | internal static class EndpointMetadataExtensions |
| | | 10 | | { |
| | | 11 | | /// <param name="endpoint">The endpoint the request was routed to, if any.</param> |
| | | 12 | | extension(Endpoint? endpoint) |
| | | 13 | | { |
| | | 14 | | /// <summary>Whether the endpoint carries <see cref="IAllowAnonymous"/> metadata.</summary> |
| | | 15 | | public bool AllowsAnonymous() => |
| | 16 | 16 | | endpoint?.Metadata.GetMetadata<IAllowAnonymous>() is not null; |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <param name="actionDescriptor">The MVC action being authorized.</param> |
| | | 20 | | extension(ActionDescriptor actionDescriptor) |
| | | 21 | | { |
| | | 22 | | /// <summary>Whether the action carries <see cref="IAllowAnonymous"/> metadata, at action or controller level.</ |
| | | 23 | | public bool AllowsAnonymous() => |
| | 11 | 24 | | actionDescriptor.EndpointMetadata.OfType<IAllowAnonymous>().Any(); |
| | | 25 | | } |
| | | 26 | | } |