| | | 1 | | using SVETA.Api.Helpers.Authorize; |
| | | 2 | | using Microsoft.AspNetCore.Authorization; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | using Newtonsoft.Json; |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Security.Authentication; |
| | | 10 | | using System.Security.Claims; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using Microsoft.AspNetCore.Authorization.Infrastructure; |
| | | 13 | | using Microsoft.EntityFrameworkCore.Internal; |
| | | 14 | | |
| | | 15 | | namespace SVETA.Api.Services.Implements |
| | | 16 | | { |
| | | 17 | | public class CaptureAuthorizationService : DefaultAuthorizationService, IAuthorizationService |
| | | 18 | | { |
| | | 19 | | private readonly AuthorizationOptions _options; |
| | | 20 | | private readonly IAuthorizationHandlerContextFactory _contextFactory; |
| | | 21 | | private readonly IAuthorizationHandlerProvider _handlers; |
| | | 22 | | private readonly IAuthorizationEvaluator _evaluator; |
| | | 23 | | private readonly IAuthorizationPolicyProvider _policyProvider; |
| | | 24 | | private readonly ILogger _logger; |
| | | 25 | | |
| | | 26 | | public CaptureAuthorizationService(IAuthorizationPolicyProvider policyProvider |
| | | 27 | | , IAuthorizationHandlerProvider handlers |
| | | 28 | | , ILogger<DefaultAuthorizationService> logger |
| | | 29 | | , IAuthorizationHandlerContextFactory contextFactory |
| | | 30 | | , IAuthorizationEvaluator evaluator |
| | | 31 | | , IOptions<AuthorizationOptions> options) |
| | 0 | 32 | | : base(policyProvider, handlers, logger, contextFactory, evaluator, options) |
| | 0 | 33 | | { |
| | 0 | 34 | | _options = options.Value; |
| | 0 | 35 | | _handlers = handlers; |
| | 0 | 36 | | _policyProvider = policyProvider; |
| | 0 | 37 | | _logger = logger; |
| | 0 | 38 | | _evaluator = evaluator; |
| | 0 | 39 | | _contextFactory = contextFactory; |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | public new async Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAu |
| | | 43 | | { |
| | | 44 | | if (requirements == null) |
| | | 45 | | { |
| | | 46 | | throw new ArgumentNullException(nameof(requirements)); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | var authContext = _contextFactory.CreateContext(requirements, user, resource); |
| | | 50 | | var handlers = await _handlers.GetHandlersAsync(authContext); |
| | | 51 | | foreach (var handler in handlers) |
| | | 52 | | { |
| | | 53 | | await handler.HandleAsync(authContext); |
| | | 54 | | if (!_options.InvokeHandlersAfterFailure && authContext.HasFailed) |
| | | 55 | | { |
| | | 56 | | break; |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | var result = _evaluator.Evaluate(authContext); |
| | | 61 | | if (!result.Succeeded) |
| | | 62 | | { |
| | | 63 | | var json = JsonConvert.SerializeObject(result.Failure.FailedRequirements); |
| | | 64 | | _logger.LogInformation($"Authorization is failed for { json }"); |
| | | 65 | | |
| | | 66 | | //перехват проблемной авторизации и создание исключения |
| | 0 | 67 | | if (result.Failure.FailedRequirements.Any(d => d is DenyAnonymousAuthorizationRequirement)) |
| | | 68 | | throw new AuthorizationException("Not authorizated"); |
| | | 69 | | else |
| | | 70 | | throw new ForbidException("Access denied"); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | //ответ только при удачной авторизации |
| | | 74 | | return result; |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |