52 lines
1.8 KiB
C#
52 lines
1.8 KiB
C#
// 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<IAuthorizationHandler> _handlers;
|
|
private readonly AuthorizationOptions _options;
|
|
|
|
public DefaultAuthorizationService(IOptions<AuthorizationOptions> options, IEnumerable<IAuthorizationHandler> handlers)
|
|
{
|
|
_handlers = handlers.ToArray();
|
|
_options = options.Value;
|
|
}
|
|
|
|
public async Task<bool> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> 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<bool> 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);
|
|
}
|
|
}
|
|
} |