2020-09-01 14:53:19 -04:00
|
|
|
using System.Collections.Generic;
|
2023-01-13 21:37:37 -05:00
|
|
|
using Jellyfin.Api.Middleware;
|
2020-10-08 14:00:55 -04:00
|
|
|
using Jellyfin.Networking.Configuration;
|
2020-04-21 18:15:31 -04:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2019-11-23 13:43:30 -05:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2020-09-01 14:53:19 -04:00
|
|
|
using Microsoft.OpenApi.Models;
|
2019-11-23 13:43:30 -05:00
|
|
|
|
2019-11-24 09:27:58 -05:00
|
|
|
namespace Jellyfin.Server.Extensions
|
2019-11-23 13:43:30 -05:00
|
|
|
{
|
2019-11-23 14:31:17 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Extensions for adding API specific functionality to the application pipeline.
|
|
|
|
/// </summary>
|
2019-11-23 13:43:30 -05:00
|
|
|
public static class ApiApplicationBuilderExtensions
|
|
|
|
{
|
2019-11-23 14:31:17 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Adds swagger and swagger UI to the application pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="applicationBuilder">The application builder.</param>
|
2020-04-21 18:15:31 -04:00
|
|
|
/// <param name="serverConfigurationManager">The server configuration.</param>
|
2019-11-23 14:31:17 -05:00
|
|
|
/// <returns>The updated application builder.</returns>
|
2020-04-21 18:15:31 -04:00
|
|
|
public static IApplicationBuilder UseJellyfinApiSwagger(
|
|
|
|
this IApplicationBuilder applicationBuilder,
|
|
|
|
IServerConfigurationManager serverConfigurationManager)
|
2019-11-23 13:43:30 -05:00
|
|
|
{
|
|
|
|
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
|
|
|
|
// specifying the Swagger JSON endpoint.
|
2020-04-21 18:15:31 -04:00
|
|
|
|
2020-10-08 14:00:55 -04:00
|
|
|
var baseUrl = serverConfigurationManager.GetNetworkConfiguration().BaseUrl.Trim('/');
|
|
|
|
var apiDocBaseUrl = serverConfigurationManager.GetNetworkConfiguration().BaseUrl;
|
2020-04-21 18:15:31 -04:00
|
|
|
if (!string.IsNullOrEmpty(baseUrl))
|
|
|
|
{
|
|
|
|
baseUrl += '/';
|
|
|
|
}
|
|
|
|
|
2020-04-19 13:28:56 -04:00
|
|
|
return applicationBuilder
|
2020-04-21 18:15:31 -04:00
|
|
|
.UseSwagger(c =>
|
|
|
|
{
|
2020-04-29 10:04:05 -04:00
|
|
|
// Custom path requires {documentName}, SwaggerDoc documentName is 'api-docs'
|
2020-09-02 10:03:15 -04:00
|
|
|
c.RouteTemplate = "{documentName}/openapi.json";
|
2020-09-01 14:53:19 -04:00
|
|
|
c.PreSerializeFilters.Add((swagger, httpReq) =>
|
|
|
|
{
|
|
|
|
swagger.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}{apiDocBaseUrl}" } };
|
|
|
|
});
|
2020-04-21 18:15:31 -04:00
|
|
|
})
|
2020-04-19 12:51:51 -04:00
|
|
|
.UseSwaggerUI(c =>
|
|
|
|
{
|
2020-04-29 10:04:05 -04:00
|
|
|
c.DocumentTitle = "Jellyfin API";
|
|
|
|
c.SwaggerEndpoint($"/{baseUrl}api-docs/openapi.json", "Jellyfin API");
|
2020-08-18 17:52:42 -04:00
|
|
|
c.InjectStylesheet($"/{baseUrl}api-docs/swagger/custom.css");
|
2020-09-02 10:03:15 -04:00
|
|
|
c.RoutePrefix = "api-docs/swagger";
|
2020-04-19 12:51:51 -04:00
|
|
|
})
|
|
|
|
.UseReDoc(c =>
|
|
|
|
{
|
2020-04-29 10:04:05 -04:00
|
|
|
c.DocumentTitle = "Jellyfin API";
|
|
|
|
c.SpecUrl($"/{baseUrl}api-docs/openapi.json");
|
2020-08-18 17:52:42 -04:00
|
|
|
c.InjectStylesheet($"/{baseUrl}api-docs/redoc/custom.css");
|
2020-09-02 10:03:15 -04:00
|
|
|
c.RoutePrefix = "api-docs/redoc";
|
2020-04-19 12:51:51 -04:00
|
|
|
});
|
2019-11-23 13:43:30 -05:00
|
|
|
}
|
2020-09-03 05:32:22 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds IP based access validation to the application pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appBuilder">The application builder.</param>
|
|
|
|
/// <returns>The updated application builder.</returns>
|
2023-02-17 13:27:36 -05:00
|
|
|
public static IApplicationBuilder UseIPBasedAccessValidation(this IApplicationBuilder appBuilder)
|
2020-09-03 05:32:22 -04:00
|
|
|
{
|
2023-02-17 13:27:36 -05:00
|
|
|
return appBuilder.UseMiddleware<IPBasedAccessValidationMiddleware>();
|
2020-09-03 05:32:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds LAN based access filtering to the application pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appBuilder">The application builder.</param>
|
|
|
|
/// <returns>The updated application builder.</returns>
|
|
|
|
public static IApplicationBuilder UseLanFiltering(this IApplicationBuilder appBuilder)
|
|
|
|
{
|
|
|
|
return appBuilder.UseMiddleware<LanFilteringMiddleware>();
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:52:39 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Enables url decoding before binding to the application pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appBuilder">The <see cref="IApplicationBuilder"/>.</param>
|
|
|
|
/// <returns>The updated application builder.</returns>
|
|
|
|
public static IApplicationBuilder UseQueryStringDecoding(this IApplicationBuilder appBuilder)
|
|
|
|
{
|
|
|
|
return appBuilder.UseMiddleware<QueryStringDecodingMiddleware>();
|
|
|
|
}
|
|
|
|
|
2020-09-03 05:32:22 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Adds base url redirection to the application pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appBuilder">The application builder.</param>
|
|
|
|
/// <returns>The updated application builder.</returns>
|
|
|
|
public static IApplicationBuilder UseBaseUrlRedirection(this IApplicationBuilder appBuilder)
|
|
|
|
{
|
|
|
|
return appBuilder.UseMiddleware<BaseUrlRedirectionMiddleware>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a custom message during server startup to the application pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appBuilder">The application builder.</param>
|
|
|
|
/// <returns>The updated application builder.</returns>
|
|
|
|
public static IApplicationBuilder UseServerStartupMessage(this IApplicationBuilder appBuilder)
|
|
|
|
{
|
|
|
|
return appBuilder.UseMiddleware<ServerStartupMessageMiddleware>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a WebSocket request handler to the application pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appBuilder">The application builder.</param>
|
|
|
|
/// <returns>The updated application builder.</returns>
|
|
|
|
public static IApplicationBuilder UseWebSocketHandler(this IApplicationBuilder appBuilder)
|
|
|
|
{
|
|
|
|
return appBuilder.UseMiddleware<WebSocketHandlerMiddleware>();
|
|
|
|
}
|
2020-12-06 21:40:43 -05:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds robots.txt redirection to the application pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appBuilder">The application builder.</param>
|
|
|
|
/// <returns>The updated application builder.</returns>
|
|
|
|
public static IApplicationBuilder UseRobotsRedirection(this IApplicationBuilder appBuilder)
|
|
|
|
{
|
|
|
|
return appBuilder.UseMiddleware<RobotsRedirectionMiddleware>();
|
|
|
|
}
|
2020-12-10 10:17:02 -05:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds /emby and /mediabrowser route trimming to the application pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This must be injected before any path related middleware.
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="appBuilder">The application builder.</param>
|
|
|
|
/// <returns>The updated application builder.</returns>
|
|
|
|
public static IApplicationBuilder UsePathTrim(this IApplicationBuilder appBuilder)
|
|
|
|
{
|
2020-12-11 09:17:06 -05:00
|
|
|
return appBuilder.UseMiddleware<LegacyEmbyRouteRewriteMiddleware>();
|
2020-12-10 10:17:02 -05:00
|
|
|
}
|
2019-11-23 13:43:30 -05:00
|
|
|
}
|
|
|
|
}
|