Rename Delegate => AssertionRequirement
And moar sugar...
This commit is contained in:
parent
197a2aa3fa
commit
2e1a8b31cd
|
|
@ -123,14 +123,19 @@ namespace Microsoft.AspNet.Authorization
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AuthorizationPolicyBuilder RequireDelegate(Action<AuthorizationContext, DelegateRequirement> handler)
|
/// <summary>
|
||||||
|
/// Requires that this Function returns true
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="assert">Function that must return true</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public AuthorizationPolicyBuilder RequireAssertion(Func<AuthorizationContext, bool> assert)
|
||||||
{
|
{
|
||||||
if (handler == null)
|
if (assert == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(handler));
|
throw new ArgumentNullException(nameof(assert));
|
||||||
}
|
}
|
||||||
|
|
||||||
Requirements.Add(new DelegateRequirement(handler));
|
Requirements.Add(new AssertionRequirement(assert));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Authorization.Infrastructure
|
||||||
|
{
|
||||||
|
public class AssertionRequirement : AuthorizationHandler<AssertionRequirement>, IAuthorizationRequirement
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Function that is called to handle this requirement
|
||||||
|
/// </summary>
|
||||||
|
public Func<AuthorizationContext, bool> Handler { get; }
|
||||||
|
|
||||||
|
public AssertionRequirement(Func<AuthorizationContext, bool> assert)
|
||||||
|
{
|
||||||
|
if (assert == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(assert));
|
||||||
|
}
|
||||||
|
|
||||||
|
Handler = assert;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Handle(AuthorizationContext context, AssertionRequirement requirement)
|
||||||
|
{
|
||||||
|
if (Handler(context))
|
||||||
|
{
|
||||||
|
context.Succeed(requirement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
// 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;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Authorization.Infrastructure
|
|
||||||
{
|
|
||||||
public class DelegateRequirement : AuthorizationHandler<DelegateRequirement>, IAuthorizationRequirement
|
|
||||||
{
|
|
||||||
public Action<AuthorizationContext, DelegateRequirement> Handler { get; }
|
|
||||||
|
|
||||||
public DelegateRequirement(Action<AuthorizationContext, DelegateRequirement> handleMe)
|
|
||||||
{
|
|
||||||
Handler = handleMe;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void Handle(AuthorizationContext context, DelegateRequirement requirement)
|
|
||||||
{
|
|
||||||
Handler(context, requirement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -905,13 +905,13 @@ namespace Microsoft.AspNet.Authorization.Test
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task CanAuthorizeWithDelegateRequirement()
|
public async Task CanAuthorizeWithAssertionRequirement()
|
||||||
{
|
{
|
||||||
var authorizationService = BuildAuthorizationService(services =>
|
var authorizationService = BuildAuthorizationService(services =>
|
||||||
{
|
{
|
||||||
services.AddAuthorization(options =>
|
services.AddAuthorization(options =>
|
||||||
{
|
{
|
||||||
options.AddPolicy("Basic", policy => policy.RequireDelegate((context, req) => context.Succeed(req)));
|
options.AddPolicy("Basic", policy => policy.RequireAssertion(context => true));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
var user = new ClaimsPrincipal();
|
var user = new ClaimsPrincipal();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue