| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Http.Metadata; |
| | | 3 | | using Microsoft.AspNetCore.Mvc; |
| | | 4 | | |
| | | 5 | | namespace Anichron.API.Infrastructure; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Requirements that declare the responses they cause, and the rule that describes every error |
| | | 9 | | /// without a body as the Problem Details the status code pages write for it. |
| | | 10 | | /// </summary> |
| | | 11 | | internal static class EndpointResponses |
| | | 12 | | { |
| | | 13 | | internal const string ProblemContentType = "application/problem+json"; |
| | | 14 | | |
| | | 15 | | extension<TBuilder>(TBuilder builder) where TBuilder : IEndpointConventionBuilder |
| | | 16 | | { |
| | | 17 | | /// <summary>A signed-in user, refused while their password must change.</summary> |
| | | 18 | | internal TBuilder RequireUser() |
| | 156 | 19 | | => builder.RequireAuthorization().ProducesRefusals(); |
| | | 20 | | |
| | | 21 | | /// <summary>A signed-in user under <paramref name="policy"/>, which refuses them while their password must chan |
| | | 22 | | /// <param name="policy">The policy that names the schemes the route accepts.</param> |
| | | 23 | | internal TBuilder RequireUser(AuthorizationPolicy policy) |
| | 66 | 24 | | => builder.RequireAuthorization(policy).ProducesRefusals(); |
| | | 25 | | |
| | | 26 | | internal TBuilder RequireUserAllowedWhileMustChangePassword() |
| | 230 | 27 | | => builder.RequireAuthorization() |
| | 230 | 28 | | .WithMetadata(new AllowedWhileMustChangePasswordAttribute()) |
| | 230 | 29 | | .ProducesProblem(StatusCodes.Status401Unauthorized); |
| | | 30 | | |
| | | 31 | | internal TBuilder RequireAdmin() |
| | 151 | 32 | | => builder.RequireAuthorization(AuthPolicies.Admin).ProducesRefusals(); |
| | | 33 | | |
| | | 34 | | private TBuilder ProducesRefusals() |
| | 373 | 35 | | => builder.ProducesProblem(StatusCodes.Status401Unauthorized) |
| | 373 | 36 | | .ProducesProblem(StatusCodes.Status403Forbidden); |
| | | 37 | | |
| | | 38 | | internal TBuilder RequireRateLimit(string policy) |
| | 818 | 39 | | => builder.RequireRateLimiting(policy).ProducesProblem(StatusCodes.Status429TooManyRequests); |
| | | 40 | | |
| | | 41 | | /// <summary>An error the framework answers with an empty body that the status code pages leave alone.</summary> |
| | | 42 | | /// <param name="statusCode">The response status.</param> |
| | | 43 | | internal TBuilder ProducesWithoutBody(int statusCode) |
| | 66 | 44 | | => builder.WithMetadata( |
| | 66 | 45 | | new ProducesResponseTypeMetadata(statusCode, typeof(void)), new ResponseWithoutBody(statusCode)); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Adds the 503 every endpoint answers while the database is unreachable and the 400 and 415 a |
| | | 50 | | /// request body can fail to bind with, then describes every error response still declared |
| | | 51 | | /// without a body as Problem Details. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="endpoint">The endpoint, after all its other conventions.</param> |
| | | 54 | | internal static void DescribeErrorsAsProblems(EndpointBuilder endpoint) |
| | 1201 | 55 | | { |
| | 1201 | 56 | | var metadata = endpoint.Metadata; |
| | 1201 | 57 | | metadata.Add(new ProducesResponseTypeMetadata(StatusCodes.Status503ServiceUnavailable)); |
| | 1201 | 58 | | if (metadata.OfType<IAcceptsMetadata>().Any()) |
| | 440 | 59 | | { |
| | 440 | 60 | | var undeclared = new[] { StatusCodes.Status400BadRequest, StatusCodes.Status415UnsupportedMediaType } |
| | 440 | 61 | | .Where(status => !metadata.OfType<IProducesResponseTypeMetadata>().Any(declared => declared.StatusCode = |
| | 440 | 62 | | .ToList(); |
| | 2920 | 63 | | foreach (var status in undeclared) |
| | 800 | 64 | | metadata.Add(new ProducesResponseTypeMetadata(status)); |
| | 440 | 65 | | } |
| | | 66 | | |
| | 1201 | 67 | | var bodiless = metadata.OfType<IProducesResponseTypeMetadata>() |
| | 1201 | 68 | | .GroupBy(declared => declared.StatusCode) |
| | 1201 | 69 | | .Select(declarations => declarations.Last()) |
| | 1201 | 70 | | .Where(published => published.StatusCode >= StatusCodes.Status400BadRequest |
| | 1201 | 71 | | && (published.Type is null || published.Type == typeof(void)) |
| | 1201 | 72 | | && !metadata.OfType<ResponseWithoutBody>().Any(kept => kept.StatusCode == published.Stat |
| | 1201 | 73 | | .Select(published => published.StatusCode) |
| | 1201 | 74 | | .ToList(); |
| | 9207 | 75 | | foreach (var status in bodiless) |
| | 2802 | 76 | | metadata.Add(new ProducesResponseTypeMetadata(status, typeof(ProblemDetails), [ProblemContentType])); |
| | 1201 | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | internal sealed record ResponseWithoutBody(int StatusCode); |