Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Muhammad Rehan Saeed
committed
Jul 5, 2019
1 parent
c73f152
commit aeb7c9a
Showing
56 changed files
with
2,843 additions
and
2,443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 27 additions & 16 deletions
43
Benchmarks/Boxed.Mapping.Benchmark/Mapping/BoxedMapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,27 @@ | ||
namespace Boxed.Mapping.Benchmark.Mapping | ||
{ | ||
using Boxed.Mapping.Benchmark.Models; | ||
|
||
public class BoxedMapper : IMapper<MapFrom, MapTo> | ||
{ | ||
public void Map(MapFrom source, MapTo destination) | ||
{ | ||
destination.BooleanTo = source.BooleanFrom; | ||
destination.DateTimeOffsetTo = source.DateTimeOffsetFrom; | ||
destination.IntegerTo = source.IntegerFrom; | ||
destination.LongTo = source.LongFrom; | ||
destination.StringTo = source.StringFrom; | ||
} | ||
} | ||
} | ||
namespace Boxed.Mapping.Benchmark.Mapping | ||
{ | ||
using System; | ||
using Boxed.Mapping.Benchmark.Models; | ||
|
||
public class BoxedMapper : IMapper<MapFrom, MapTo> | ||
{ | ||
public void Map(MapFrom source, MapTo destination) | ||
{ | ||
if (source == null) | ||
{ | ||
throw new ArgumentNullException(nameof(source)); | ||
} | ||
|
||
if (destination == null) | ||
{ | ||
throw new ArgumentNullException(nameof(destination)); | ||
} | ||
|
||
destination.BooleanTo = source.BooleanFrom; | ||
destination.DateTimeOffsetTo = source.DateTimeOffsetFrom; | ||
destination.IntegerTo = source.IntegerFrom; | ||
destination.LongTo = source.LongFrom; | ||
destination.StringTo = source.StringFrom; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 50 additions & 39 deletions
89
Source/Boxed.AspNetCore.Swagger/OperationFilters/ClaimsOperationFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,50 @@ | ||
namespace Boxed.AspNetCore.Swagger.OperationFilters | ||
{ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Authorization.Infrastructure; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
/// <summary> | ||
/// Adds claims from any authorization policy's <see cref="ClaimsAuthorizationRequirement"/>'s. | ||
/// </summary> | ||
/// <seealso cref="IOperationFilter" /> | ||
public class ClaimsOperationFilter : IOperationFilter | ||
{ | ||
/// <summary> | ||
/// Applies the specified operation. | ||
/// </summary> | ||
/// <param name="operation">The operation.</param> | ||
/// <param name="context">The context.</param> | ||
public void Apply(Operation operation, OperationFilterContext context) | ||
{ | ||
var filterDescriptors = context.ApiDescription.ActionDescriptor.FilterDescriptors; | ||
var authorizationRequirements = filterDescriptors.GetPolicyRequirements(); | ||
var claimTypes = authorizationRequirements | ||
.OfType<ClaimsAuthorizationRequirement>() | ||
.Select(x => x.ClaimType); | ||
if (claimTypes.Any()) | ||
{ | ||
operation.Security = new List<IDictionary<string, IEnumerable<string>>>() | ||
{ | ||
new Dictionary<string, IEnumerable<string>>() | ||
{ | ||
{ "oauth2", claimTypes } | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
namespace Boxed.AspNetCore.Swagger.OperationFilters | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Authorization.Infrastructure; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
/// <summary> | ||
/// Adds claims from any authorization policy's <see cref="ClaimsAuthorizationRequirement"/>'s. | ||
/// </summary> | ||
/// <seealso cref="IOperationFilter" /> | ||
public class ClaimsOperationFilter : IOperationFilter | ||
{ | ||
/// <summary> | ||
/// Applies the specified operation. | ||
/// </summary> | ||
/// <param name="operation">The operation.</param> | ||
/// <param name="context">The context.</param> | ||
public void Apply(Operation operation, OperationFilterContext context) | ||
{ | ||
if (operation == null) | ||
{ | ||
throw new ArgumentNullException(nameof(operation)); | ||
} | ||
|
||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
var filterDescriptors = context.ApiDescription.ActionDescriptor.FilterDescriptors; | ||
var authorizationRequirements = filterDescriptors.GetPolicyRequirements(); | ||
var claimTypes = authorizationRequirements | ||
.OfType<ClaimsAuthorizationRequirement>() | ||
.Select(x => x.ClaimType); | ||
if (claimTypes.Any()) | ||
{ | ||
operation.Security = new List<IDictionary<string, IEnumerable<string>>>() | ||
{ | ||
new Dictionary<string, IEnumerable<string>>() | ||
{ | ||
{ "oauth2", claimTypes } | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
} |
121 changes: 66 additions & 55 deletions
121
Source/Boxed.AspNetCore.Swagger/OperationFilters/ForbiddenResponseOperationFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,67 @@ | ||
namespace Boxed.AspNetCore.Swagger.OperationFilters | ||
{ | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Authorization.Infrastructure; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
/// <summary> | ||
/// Adds a 403 Forbidden response to the Swagger response documentation when the authorization policy contains a | ||
/// <see cref="ClaimsAuthorizationRequirement"/>, <see cref="NameAuthorizationRequirement"/>, | ||
/// <see cref="RolesAuthorizationRequirement"/> or <see cref="AssertionRequirement"/>. | ||
/// </summary> | ||
/// <seealso cref="IOperationFilter" /> | ||
public class ForbiddenResponseOperationFilter : IOperationFilter | ||
{ | ||
private const string ForbiddenStatusCode = "403"; | ||
private static readonly Response ForbiddenResponse = new Response() | ||
{ | ||
Description = "Forbidden - The user does not have the necessary permissions to access the resource." | ||
}; | ||
|
||
/// <summary> | ||
/// Applies the specified operation. | ||
/// </summary> | ||
/// <param name="operation">The operation.</param> | ||
/// <param name="context">The context.</param> | ||
public void Apply(Operation operation, OperationFilterContext context) | ||
{ | ||
var filterDescriptors = context.ApiDescription.ActionDescriptor.FilterDescriptors; | ||
var authorizationRequirements = filterDescriptors.GetPolicyRequirements(); | ||
if (!operation.Responses.ContainsKey(ForbiddenStatusCode) && | ||
HasAuthorizationRequirement(authorizationRequirements)) | ||
{ | ||
operation.Responses.Add(ForbiddenStatusCode, ForbiddenResponse); | ||
} | ||
} | ||
|
||
private static bool HasAuthorizationRequirement( | ||
IEnumerable<IAuthorizationRequirement> authorizationRequirements) | ||
{ | ||
foreach (var authorizationRequirement in authorizationRequirements) | ||
{ | ||
if (authorizationRequirement is ClaimsAuthorizationRequirement || | ||
authorizationRequirement is NameAuthorizationRequirement || | ||
authorizationRequirement is RolesAuthorizationRequirement || | ||
authorizationRequirement is AssertionRequirement) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
namespace Boxed.AspNetCore.Swagger.OperationFilters | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Authorization.Infrastructure; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
/// <summary> | ||
/// Adds a 403 Forbidden response to the Swagger response documentation when the authorization policy contains a | ||
/// <see cref="ClaimsAuthorizationRequirement"/>, <see cref="NameAuthorizationRequirement"/>, | ||
/// <see cref="RolesAuthorizationRequirement"/> or <see cref="AssertionRequirement"/>. | ||
/// </summary> | ||
/// <seealso cref="IOperationFilter" /> | ||
public class ForbiddenResponseOperationFilter : IOperationFilter | ||
{ | ||
private const string ForbiddenStatusCode = "403"; | ||
private static readonly Response ForbiddenResponse = new Response() | ||
{ | ||
Description = "Forbidden - The user does not have the necessary permissions to access the resource." | ||
}; | ||
|
||
/// <summary> | ||
/// Applies the specified operation. | ||
/// </summary> | ||
/// <param name="operation">The operation.</param> | ||
/// <param name="context">The context.</param> | ||
public void Apply(Operation operation, OperationFilterContext context) | ||
{ | ||
if (operation == null) | ||
{ | ||
throw new ArgumentNullException(nameof(operation)); | ||
} | ||
|
||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
var filterDescriptors = context.ApiDescription.ActionDescriptor.FilterDescriptors; | ||
var authorizationRequirements = filterDescriptors.GetPolicyRequirements(); | ||
if (!operation.Responses.ContainsKey(ForbiddenStatusCode) && | ||
HasAuthorizationRequirement(authorizationRequirements)) | ||
{ | ||
operation.Responses.Add(ForbiddenStatusCode, ForbiddenResponse); | ||
} | ||
} | ||
|
||
private static bool HasAuthorizationRequirement( | ||
IEnumerable<IAuthorizationRequirement> authorizationRequirements) | ||
{ | ||
foreach (var authorizationRequirement in authorizationRequirements) | ||
{ | ||
if (authorizationRequirement is ClaimsAuthorizationRequirement || | ||
authorizationRequirement is NameAuthorizationRequirement || | ||
authorizationRequirement is RolesAuthorizationRequirement || | ||
authorizationRequirement is AssertionRequirement) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.