| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using JetBrains.Annotations; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.Http; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Requirements; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents an authorization requirement for localhost requests, meaning that if a request comes from the localhost, |
| | | 10 | | /// </summary> |
| | | 11 | | public class LocalHostRequirement : IAuthorizationRequirement |
| | | 12 | | { |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | [PublicAPI] |
| | | 17 | | public class LocalHostRequirementHandler : AuthorizationHandler<LocalHostRequirement> |
| | | 18 | | { |
| | | 19 | | private readonly IHttpContextAccessor _httpContextAccessor; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | 234 | 22 | | public LocalHostRequirementHandler(IHttpContextAccessor httpContextAccessor) |
| | | 23 | | { |
| | 234 | 24 | | _httpContextAccessor = httpContextAccessor; |
| | 234 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, LocalHostRequirement requirement |
| | | 29 | | { |
| | 0 | 30 | | if (_httpContextAccessor.HttpContext?.Request.IsLocal() == false) |
| | 0 | 31 | | context.Fail(new AuthorizationFailureReason(this, "Only requests from localhost are allowed")); |
| | | 32 | | |
| | 0 | 33 | | context.Succeed(requirement); |
| | 0 | 34 | | return Task.CompletedTask; |
| | | 35 | | } |
| | | 36 | | } |