| | | 1 | | using Anichron.API.Infrastructure; |
| | | 2 | | using Anichron.Core.Data.Repository; |
| | | 3 | | using Anichron.Infrastructure.Storage; |
| | | 4 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | | 5 | | using Microsoft.AspNetCore.Authorization; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | using Microsoft.Net.Http.Headers; |
| | | 8 | | using System.Security.Claims; |
| | | 9 | | |
| | | 10 | | namespace Anichron.API.Endpoints; |
| | | 11 | | |
| | | 12 | | /// <summary>The proxy route, the one route that accepts the media cookie as well as the bearer header (ADR 0013).</summ |
| | | 13 | | public static class ProxyEndpoints |
| | | 14 | | { |
| | | 15 | | internal const string MediaCookieName = "media_token"; |
| | | 16 | | internal const string MediaCookiePath = $"{ApiPaths.Base}/{ApiPaths.Proxies.Group}"; |
| | | 17 | | |
| | | 18 | | private const string mediaCookieScheme = "MediaCookie"; |
| | | 19 | | private const string currentProxyCacheControl = "private, max-age=31536000, immutable"; |
| | | 20 | | private const string staleProxyCacheControl = "private, no-cache"; |
| | | 21 | | |
| | | 22 | | public static IServiceCollection AddProxyEndpoints(this IServiceCollection services) |
| | 31 | 23 | | { |
| | 31 | 24 | | services.AddAuthentication().AddJwtBearer(mediaCookieScheme, options => options.Events = new JwtBearerEvents |
| | 31 | 25 | | { |
| | 31 | 26 | | OnMessageReceived = context => |
| | 31 | 27 | | { |
| | 31 | 28 | | context.Token = context.Request.Cookies[MediaCookieName]; |
| | 31 | 29 | | if (context.Token is null) |
| | 31 | 30 | | context.NoResult(); |
| | 31 | 31 | | return Task.CompletedTask; |
| | 31 | 32 | | }, |
| | 31 | 33 | | }); |
| | 31 | 34 | | return services.AddJwtValidation(mediaCookieScheme); |
| | 31 | 35 | | } |
| | | 36 | | |
| | | 37 | | public static IEndpointRouteBuilder MapProxyEndpoints(this IEndpointRouteBuilder app) |
| | 66 | 38 | | { |
| | 66 | 39 | | var proxies = app.MapGroup(ApiPaths.Proxies.Group).WithTags("Media").RequireUser(BearerOrMediaCookie()); |
| | 66 | 40 | | proxies.MapGet(ApiPaths.Proxies.Proxy, GetProxyAsync) |
| | 66 | 41 | | .ProducesProxy(StatusCodes.Status200OK) |
| | 66 | 42 | | .ProducesProxy(StatusCodes.Status206PartialContent) |
| | 66 | 43 | | .Produces(StatusCodes.Status304NotModified) |
| | 66 | 44 | | .Produces(StatusCodes.Status404NotFound) |
| | 66 | 45 | | .Produces(StatusCodes.Status412PreconditionFailed) |
| | 66 | 46 | | .ProducesWithoutBody(StatusCodes.Status416RangeNotSatisfiable); |
| | 66 | 47 | | return app; |
| | 66 | 48 | | } |
| | | 49 | | |
| | | 50 | | private static AuthorizationPolicy BearerOrMediaCookie() |
| | 66 | 51 | | => new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme, mediaCookieScheme) |
| | 66 | 52 | | .RequireAuthenticatedUser() |
| | 66 | 53 | | .AddRequirements(new MustChangePasswordRequirement()) |
| | 66 | 54 | | .Build(); |
| | | 55 | | |
| | | 56 | | private static RouteHandlerBuilder ProducesProxy(this RouteHandlerBuilder builder, int statusCode) |
| | 132 | 57 | | { |
| | 132 | 58 | | var contentTypes = ProxyKinds.All.Select(kind => kind.ContentType).Distinct().ToArray(); |
| | 132 | 59 | | return builder.Produces<Stream>(statusCode, contentTypes[0], contentTypes[1..]); |
| | 132 | 60 | | } |
| | | 61 | | |
| | | 62 | | internal static async Task<IResult> GetProxyAsync( |
| | | 63 | | Guid assetId, |
| | | 64 | | string type, |
| | | 65 | | [FromQuery(Name = "v")] string? version, |
| | | 66 | | ClaimsPrincipal user, |
| | | 67 | | [FromServices] IMediaAssetReadRepository assets, |
| | | 68 | | [FromServices] IProxyStore proxies, |
| | | 69 | | HttpContext context, |
| | | 70 | | CancellationToken ct) |
| | 21 | 71 | | { |
| | 21 | 72 | | if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId)) |
| | 0 | 73 | | return Results.Unauthorized(); |
| | | 74 | | |
| | 21 | 75 | | if (ProxyKinds.FromSlug(type) is not { } kind) |
| | 3 | 76 | | return Results.NotFound(); |
| | | 77 | | |
| | 18 | 78 | | var proxy = await assets.FindVisibleProxyAsync(assetId, userId, kind.Type, ct); |
| | 18 | 79 | | if (proxy is null) |
| | 1 | 80 | | return Results.NotFound(); |
| | | 81 | | |
| | 17 | 82 | | if (proxies.OpenRead(proxy.ProxyPath) is not { } stream) |
| | 1 | 83 | | return Results.NotFound(); |
| | | 84 | | |
| | 16 | 85 | | context.Response.Headers.CacheControl = version == proxy.ContentHash ? currentProxyCacheControl : staleProxyCach |
| | 16 | 86 | | return Results.Stream( |
| | 16 | 87 | | stream, |
| | 16 | 88 | | kind.ContentType, |
| | 16 | 89 | | entityTag: new EntityTagHeaderValue($"\"{proxy.ContentHash}-{kind.Type}\""), |
| | 16 | 90 | | enableRangeProcessing: true); |
| | 21 | 91 | | } |
| | | 92 | | } |