// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.Extensions.OptionsModel; namespace Microsoft.AspNet.Authorization { public class DefaultAuthorizationService : IAuthorizationService { private readonly IList _handlers; private readonly AuthorizationOptions _options; public DefaultAuthorizationService(IOptions options, IEnumerable handlers) { _handlers = handlers.ToArray(); _options = options.Value; } public async Task AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable requirements) { if (requirements == null) { throw new ArgumentNullException(nameof(requirements)); } var authContext = new AuthorizationContext(requirements, user, resource); foreach (var handler in _handlers) { await handler.HandleAsync(authContext); } return authContext.HasSucceeded; } public Task AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName) { if (policyName == null) { throw new ArgumentNullException(nameof(policyName)); } var policy = _options.GetPolicy(policyName); return (policy == null) ? Task.FromResult(false) : this.AuthorizeAsync(user, resource, policy); } } }