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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.AspNet.Authorization.Infrastructure;
namespace Microsoft.AspNet.Authorization namespace Microsoft.AspNet.Authorization
{ {

View File

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

View File

@ -37,7 +37,6 @@ namespace Microsoft.AspNet.Authorization
_logger = logger; _logger = logger;
} }
public async Task<bool> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> requirements) public async Task<bool> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> requirements)
{ {
if (requirements == null) if (requirements == null)
@ -71,9 +70,11 @@ namespace Microsoft.AspNet.Authorization
} }
var policy = _options.GetPolicy(policyName); var policy = _options.GetPolicy(policyName);
return (policy == null) if (policy == null)
? Task.FromResult(false) {
: this.AuthorizeAsync(user, resource, policy); 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.Collections.Generic;
using System.Linq; 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 // 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 // If AllowedValues is null or empty, that means any claim is valid

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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 public class OperationAuthorizationRequirement : IAuthorizationRequirement
{ {

View File

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

View File

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

View File

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

View File

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