AuthZ API review changes

This commit is contained in:
Hao Kung 2015-10-21 16:45:20 -07:00
parent 7dfac2fd78
commit 0eaec216b1
12 changed files with 20 additions and 23 deletions

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Authorization.Infrastructure;
namespace Microsoft.AspNet.Authorization
{

View File

@ -3,6 +3,7 @@
using System;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Authorization.Infrastructure;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection

View File

@ -37,7 +37,6 @@ namespace Microsoft.AspNet.Authorization
_logger = logger;
}
public async Task<bool> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> requirements)
{
if (requirements == null)
@ -71,9 +70,11 @@ namespace Microsoft.AspNet.Authorization
}
var policy = _options.GetPolicy(policyName);
return (policy == null)
? Task.FromResult(false)
: this.AuthorizeAsync(user, resource, policy);
if (policy == null)
{
throw new InvalidOperationException($"No policy found: {policyName}.");
}
return this.AuthorizeAsync(user, resource, policy);
}
}
}

View File

@ -5,7 +5,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNet.Authorization
namespace Microsoft.AspNet.Authorization.Infrastructure
{
// Must contain a claim with the specified name, and at least one of the required values
// If AllowedValues is null or empty, that means any claim is valid

View File

@ -3,7 +3,7 @@
using System;
namespace Microsoft.AspNet.Authorization
namespace Microsoft.AspNet.Authorization.Infrastructure
{
public class DelegateRequirement : AuthorizationHandler<DelegateRequirement>, IAuthorizationRequirement
{

View File

@ -3,7 +3,7 @@
using System.Linq;
namespace Microsoft.AspNet.Authorization
namespace Microsoft.AspNet.Authorization.Infrastructure
{
public class DenyAnonymousAuthorizationRequirement : AuthorizationHandler<DenyAnonymousAuthorizationRequirement>, IAuthorizationRequirement
{

View File

@ -4,7 +4,7 @@
using System;
using System.Linq;
namespace Microsoft.AspNet.Authorization
namespace Microsoft.AspNet.Authorization.Infrastructure
{
/// <summary>
/// Requirement that ensures a specific Name

View File

@ -1,7 +1,7 @@
// 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.
namespace Microsoft.AspNet.Authorization
namespace Microsoft.AspNet.Authorization.Infrastructure
{
public class OperationAuthorizationRequirement : IAuthorizationRequirement
{

View File

@ -4,7 +4,7 @@
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Authorization
namespace Microsoft.AspNet.Authorization.Infrastructure
{
public class PassThroughAuthorizationHandler : IAuthorizationHandler
{

View File

@ -5,7 +5,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNet.Authorization
namespace Microsoft.AspNet.Authorization.Infrastructure
{
// Must belong to with one of specified roles
// If AllowedRoles is null or empty, that means any role is valid

View File

@ -4,6 +4,7 @@
using System;
using System.Linq;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Authorization.Infrastructure;
using Xunit;
namespace Microsoft.AspNet.Authroization.Test

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Authorization.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
@ -268,23 +269,15 @@ namespace Microsoft.AspNet.Authorization.Test
}
[Fact]
public async Task Authorize_ShouldNotAllowIfUnknownPolicy()
public async Task Authorize_ThrowsWithUnknownPolicy()
{
// Arrange
var authorizationService = BuildAuthorizationService();
var user = new ClaimsPrincipal(
new ClaimsIdentity(
new Claim[] {
new Claim("Permission", "CanViewComment"),
},
null)
);
// Act
var allowed = await authorizationService.AuthorizeAsync(user, "Basic");
// Assert
Assert.False(allowed);
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => authorizationService.AuthorizeAsync(new ClaimsPrincipal(), "whatever", "BogusPolicy"));
Assert.Equal("No policy found: BogusPolicy.", exception.Message);
}
[Fact]
@ -459,7 +452,7 @@ namespace Microsoft.AspNet.Authorization.Test
);
// Act
var allowed = await authorizationService.AuthorizeAsync(user, "Any");
var allowed = await authorizationService.AuthorizeAsync(user, "Hao");
// Assert
Assert.False(allowed);