| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | | 5 | | using Microsoft.AspNetCore.Authorization; |
| | | 6 | | using Microsoft.AspNetCore.Http; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Requirements; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Add the "create:application" permission to the current user if the request is local. |
| | | 12 | | /// </summary> |
| | | 13 | | public class LocalHostPermissionRequirement : IAuthorizationRequirement |
| | | 14 | | { |
| | | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | [PublicAPI] |
| | | 19 | | public class LocalHostPermissionRequirementHandler : AuthorizationHandler<LocalHostPermissionRequirement> |
| | | 20 | | { |
| | | 21 | | private readonly IHttpContextAccessor _httpContextAccessor; |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | 234 | 24 | | public LocalHostPermissionRequirementHandler(IHttpContextAccessor httpContextAccessor) |
| | | 25 | | { |
| | 234 | 26 | | _httpContextAccessor = httpContextAccessor; |
| | 234 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, LocalHostPermissionRequirement r |
| | | 31 | | { |
| | 0 | 32 | | if (_httpContextAccessor.HttpContext?.Request.IsLocal() == false) |
| | 0 | 33 | | return Task.CompletedTask; |
| | | 34 | | |
| | 0 | 35 | | var currentIdentity = context.User.Identity; |
| | | 36 | | |
| | 0 | 37 | | if (currentIdentity?.IsAuthenticated == false) |
| | | 38 | | { |
| | 0 | 39 | | var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); |
| | 0 | 40 | | identity.AddClaim(new Claim("permissions", "create:application")); |
| | 0 | 41 | | identity.AddClaim(new Claim("permissions", "create:user")); |
| | 0 | 42 | | identity.AddClaim(new Claim("permissions", "create:role")); |
| | 0 | 43 | | context.User.AddIdentity(identity); |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | context.Succeed(requirement); |
| | 0 | 47 | | return Task.CompletedTask; |
| | | 48 | | } |
| | | 49 | | } |