-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added authorization exceptions and friends
- Loading branch information
1 parent
c59718e
commit aec6221
Showing
27 changed files
with
535 additions
and
311 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
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
42 changes: 42 additions & 0 deletions
42
src/AspNetCore/Conventions/ProblemDetailsOptionsExtensions.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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Hellang.Middleware.ProblemDetails; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Rocket.Surgery.LaunchPad.Foundation; | ||
|
||
namespace Rocket.Surgery.LaunchPad.AspNetCore.Conventions; | ||
|
||
/// <summary> | ||
/// Extensions to ProblemDetailsOptions | ||
/// </summary> | ||
public static class ProblemDetailsOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// Creates a mapping for an exception that implements <see cref="IProblemDetailsData" /> with the given status code | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <param name="statusCode"></param> | ||
/// <typeparam name="TException"></typeparam> | ||
public static void MapToProblemDetailsDataException<TException>(this ProblemDetailsOptions options, int statusCode) | ||
where TException : Exception, IProblemDetailsData | ||
{ | ||
options.Map<TException>((_, ex) => ConstructProblemDetails(ex, statusCode)); | ||
} | ||
|
||
private static ProblemDetails ConstructProblemDetails<TException>(TException ex, int statusCode) where TException : Exception, IProblemDetailsData | ||
{ | ||
var details = new ProblemDetails | ||
{ | ||
Detail = ex.Message, | ||
Title = ex.Title, | ||
Type = ex.Link, | ||
Instance = ex.Instance, | ||
Status = statusCode | ||
}; | ||
foreach (var item in ex.Properties) | ||
{ | ||
if (details.Extensions.ContainsKey(item.Key)) continue; | ||
details.Extensions.Add(item); | ||
} | ||
|
||
return details; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Infrastructure; | ||
using Rocket.Surgery.LaunchPad.Foundation; | ||
|
||
namespace Rocket.Surgery.LaunchPad.AspNetCore.Filters; | ||
|
||
/// <summary> | ||
/// Not authorized exception that catches not authorized messages that might have been thrown by calling code. | ||
/// </summary> | ||
internal class NotAuthorizedExceptionFilter : ProblemDetailsExceptionFilter<NotAuthorizedException> | ||
{ | ||
/// <summary> | ||
/// Not authorized exception that catches not authorized messages that might have been thrown by calling code. | ||
/// </summary> | ||
public NotAuthorizedExceptionFilter(ProblemDetailsFactory problemDetailsFactory) : base(StatusCodes.Status403Forbidden, problemDetailsFactory) | ||
{ | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.AspNetCore.Mvc.Infrastructure; | ||
using Rocket.Surgery.LaunchPad.Foundation; | ||
|
||
namespace Rocket.Surgery.LaunchPad.AspNetCore.Filters; | ||
|
||
/// <summary> | ||
/// An exception filter that catches the given problem details exception and uses the given status code to return the result | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
[PublicAPI] | ||
public abstract class ProblemDetailsExceptionFilter<T> : IExceptionFilter, IAsyncExceptionFilter | ||
where T : Exception, IProblemDetailsData | ||
{ | ||
private readonly int _statusCode; | ||
private readonly ProblemDetailsFactory _problemDetailsFactory; | ||
|
||
/// <summary> | ||
/// Create the problem details filter | ||
/// </summary> | ||
/// <param name="statusCode"></param> | ||
/// <param name="problemDetailsFactory"></param> | ||
protected ProblemDetailsExceptionFilter(int statusCode, ProblemDetailsFactory problemDetailsFactory) | ||
{ | ||
_statusCode = statusCode; | ||
_problemDetailsFactory = problemDetailsFactory; | ||
} | ||
|
||
/// <summary> | ||
/// Allows changing the problem details before it is returned | ||
/// </summary> | ||
/// <param name="problemDetails"></param> | ||
/// <returns></returns> | ||
protected virtual ProblemDetails CustomizeProblemDetails(ProblemDetails problemDetails) | ||
{ | ||
return problemDetails; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Task OnExceptionAsync(ExceptionContext context) | ||
{ | ||
OnException(context); | ||
return Task.CompletedTask; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void OnException(ExceptionContext context) | ||
{ | ||
if (context.Exception is T exception) | ||
{ | ||
context.ExceptionHandled = true; | ||
var problemDetails = _problemDetailsFactory.CreateProblemDetails( | ||
context.HttpContext, | ||
_statusCode, | ||
detail: exception.Message, | ||
title: exception.Title, | ||
type: exception.Link, | ||
instance: exception.Instance | ||
); | ||
|
||
foreach (var item in exception.Properties) | ||
{ | ||
if (problemDetails.Extensions.ContainsKey(item.Key)) continue; | ||
problemDetails.Extensions.Add(item); | ||
} | ||
|
||
problemDetails = CustomizeProblemDetails(problemDetails); | ||
context.Result = new ObjectResult(problemDetails) { StatusCode = _statusCode }; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.