2020-04-19 19:24:32 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-01-08 01:05:15 +01:00
|
|
|
using System.Globalization;
|
2020-04-19 19:24:32 +02:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-09-10 11:05:46 +02:00
|
|
|
using System.Net;
|
2021-01-08 01:05:15 +01:00
|
|
|
using System.Net.Sockets;
|
2020-05-08 16:40:37 +02:00
|
|
|
using System.Reflection;
|
2021-01-13 16:29:57 +01:00
|
|
|
using System.Runtime.CompilerServices;
|
2021-01-08 01:05:15 +01:00
|
|
|
using System.Text;
|
2020-11-06 21:00:14 +01:00
|
|
|
using Emby.Server.Implementations;
|
2019-11-23 19:43:30 +01:00
|
|
|
using Jellyfin.Api.Auth;
|
2020-06-15 20:49:54 +02:00
|
|
|
using Jellyfin.Api.Auth.DefaultAuthorizationPolicy;
|
2020-06-19 21:10:10 +02:00
|
|
|
using Jellyfin.Api.Auth.DownloadPolicy;
|
2020-08-07 01:59:48 +02:00
|
|
|
using Jellyfin.Api.Auth.FirstTimeOrIgnoreParentalControlSetupPolicy;
|
2020-08-06 16:17:45 +02:00
|
|
|
using Jellyfin.Api.Auth.FirstTimeSetupOrDefaultPolicy;
|
2019-11-23 19:43:30 +01:00
|
|
|
using Jellyfin.Api.Auth.FirstTimeSetupOrElevatedPolicy;
|
2020-08-06 16:17:45 +02:00
|
|
|
using Jellyfin.Api.Auth.IgnoreParentalControlPolicy;
|
|
|
|
using Jellyfin.Api.Auth.LocalAccessOrRequiresElevationPolicy;
|
2020-06-15 20:49:54 +02:00
|
|
|
using Jellyfin.Api.Auth.LocalAccessPolicy;
|
2019-11-23 19:43:30 +01:00
|
|
|
using Jellyfin.Api.Auth.RequiresElevationPolicy;
|
2020-12-03 10:43:44 +01:00
|
|
|
using Jellyfin.Api.Auth.SyncPlayAccessPolicy;
|
2019-11-24 19:25:46 +01:00
|
|
|
using Jellyfin.Api.Constants;
|
2019-11-23 19:43:30 +01:00
|
|
|
using Jellyfin.Api.Controllers;
|
2020-11-21 19:58:35 +01:00
|
|
|
using Jellyfin.Api.ModelBinders;
|
2020-12-03 10:43:44 +01:00
|
|
|
using Jellyfin.Data.Enums;
|
2021-01-08 01:05:15 +01:00
|
|
|
using Jellyfin.Networking.Configuration;
|
2020-09-05 21:02:53 +02:00
|
|
|
using Jellyfin.Server.Configuration;
|
2020-09-02 01:26:49 +02:00
|
|
|
using Jellyfin.Server.Filters;
|
2020-04-20 02:10:59 +02:00
|
|
|
using Jellyfin.Server.Formatters;
|
2020-05-21 16:44:15 +02:00
|
|
|
using MediaBrowser.Common.Json;
|
2020-12-05 17:00:34 +01:00
|
|
|
using MediaBrowser.Common.Net;
|
2020-06-02 19:47:00 +02:00
|
|
|
using MediaBrowser.Model.Entities;
|
2019-11-23 19:43:30 +01:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-06-17 16:05:30 +02:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2020-09-05 17:10:05 +02:00
|
|
|
using Microsoft.AspNetCore.Cors.Infrastructure;
|
2020-06-17 16:05:30 +02:00
|
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
2019-11-23 19:43:30 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-11-06 21:00:14 +01:00
|
|
|
using Microsoft.OpenApi.Any;
|
|
|
|
using Microsoft.OpenApi.Interfaces;
|
2019-11-23 19:43:30 +01:00
|
|
|
using Microsoft.OpenApi.Models;
|
2020-05-08 16:40:37 +02:00
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
2020-09-10 11:05:46 +02:00
|
|
|
using AuthenticationSchemes = Jellyfin.Api.Constants.AuthenticationSchemes;
|
2019-11-23 19:43:30 +01:00
|
|
|
|
2021-01-17 14:35:30 +01:00
|
|
|
[assembly: InternalsVisibleTo("Jellyfin.Api.Tests")]
|
|
|
|
|
2019-11-24 15:27:58 +01:00
|
|
|
namespace Jellyfin.Server.Extensions
|
2019-11-23 19:43:30 +01:00
|
|
|
{
|
2019-11-23 20:31:17 +01:00
|
|
|
/// <summary>
|
|
|
|
/// API specific extensions for the service collection.
|
|
|
|
/// </summary>
|
2019-11-23 19:43:30 +01:00
|
|
|
public static class ApiServiceCollectionExtensions
|
|
|
|
{
|
2019-11-23 20:31:17 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Adds jellyfin API authorization policies to the DI container.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
|
|
|
/// <returns>The updated service collection.</returns>
|
2019-11-23 19:43:30 +01:00
|
|
|
public static IServiceCollection AddJellyfinApiAuthorization(this IServiceCollection serviceCollection)
|
|
|
|
{
|
2020-06-15 20:49:54 +02:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, DefaultAuthorizationHandler>();
|
2020-06-19 21:10:10 +02:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, DownloadHandler>();
|
2020-08-06 16:17:45 +02:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, FirstTimeSetupOrDefaultHandler>();
|
2019-11-23 19:43:30 +01:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, FirstTimeSetupOrElevatedHandler>();
|
2020-08-06 16:17:45 +02:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, IgnoreParentalControlHandler>();
|
2020-08-07 01:59:48 +02:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, FirstTimeOrIgnoreParentalControlSetupHandler>();
|
2020-06-15 20:49:54 +02:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, LocalAccessHandler>();
|
2020-08-06 16:17:45 +02:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, LocalAccessOrRequiresElevationHandler>();
|
2019-11-23 19:43:30 +01:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, RequiresElevationHandler>();
|
2020-12-03 10:43:44 +01:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, SyncPlayAccessHandler>();
|
2019-11-23 19:43:30 +01:00
|
|
|
return serviceCollection.AddAuthorizationCore(options =>
|
|
|
|
{
|
|
|
|
options.AddPolicy(
|
2020-06-15 20:49:54 +02:00
|
|
|
Policies.DefaultAuthorization,
|
2019-11-23 19:43:30 +01:00
|
|
|
policy =>
|
|
|
|
{
|
2019-11-24 19:25:46 +01:00
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
2020-06-15 20:49:54 +02:00
|
|
|
policy.AddRequirements(new DefaultAuthorizationRequirement());
|
2019-11-23 19:43:30 +01:00
|
|
|
});
|
2020-06-19 21:10:10 +02:00
|
|
|
options.AddPolicy(
|
|
|
|
Policies.Download,
|
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
|
|
|
policy.AddRequirements(new DownloadRequirement());
|
|
|
|
});
|
2020-08-06 16:17:45 +02:00
|
|
|
options.AddPolicy(
|
|
|
|
Policies.FirstTimeSetupOrDefault,
|
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
|
|
|
policy.AddRequirements(new FirstTimeSetupOrDefaultRequirement());
|
|
|
|
});
|
2019-11-23 19:43:30 +01:00
|
|
|
options.AddPolicy(
|
2019-11-24 19:25:46 +01:00
|
|
|
Policies.FirstTimeSetupOrElevated,
|
2019-11-23 19:43:30 +01:00
|
|
|
policy =>
|
|
|
|
{
|
2019-11-24 19:25:46 +01:00
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
2019-11-23 19:43:30 +01:00
|
|
|
policy.AddRequirements(new FirstTimeSetupOrElevatedRequirement());
|
|
|
|
});
|
2020-06-15 20:49:54 +02:00
|
|
|
options.AddPolicy(
|
2020-08-06 16:17:45 +02:00
|
|
|
Policies.IgnoreParentalControl,
|
2020-06-15 20:49:54 +02:00
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
2020-08-06 16:17:45 +02:00
|
|
|
policy.AddRequirements(new IgnoreParentalControlRequirement());
|
|
|
|
});
|
|
|
|
options.AddPolicy(
|
2020-08-07 01:59:48 +02:00
|
|
|
Policies.FirstTimeSetupOrIgnoreParentalControl,
|
2020-08-06 16:17:45 +02:00
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
2020-08-07 01:59:48 +02:00
|
|
|
policy.AddRequirements(new FirstTimeOrIgnoreParentalControlSetupRequirement());
|
2020-06-15 20:49:54 +02:00
|
|
|
});
|
|
|
|
options.AddPolicy(
|
|
|
|
Policies.LocalAccessOnly,
|
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
|
|
|
policy.AddRequirements(new LocalAccessRequirement());
|
|
|
|
});
|
2020-08-06 16:17:45 +02:00
|
|
|
options.AddPolicy(
|
|
|
|
Policies.LocalAccessOrRequiresElevation,
|
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
|
|
|
policy.AddRequirements(new LocalAccessOrRequiresElevationRequirement());
|
|
|
|
});
|
2020-06-15 20:49:54 +02:00
|
|
|
options.AddPolicy(
|
|
|
|
Policies.RequiresElevation,
|
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
|
|
|
policy.AddRequirements(new RequiresElevationRequirement());
|
|
|
|
});
|
2020-12-03 10:43:44 +01:00
|
|
|
options.AddPolicy(
|
2020-12-07 10:33:15 +01:00
|
|
|
Policies.SyncPlayHasAccess,
|
2020-12-03 10:43:44 +01:00
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
2020-12-07 10:33:15 +01:00
|
|
|
policy.AddRequirements(new SyncPlayAccessRequirement(SyncPlayAccessRequirementType.HasAccess));
|
2020-12-03 10:43:44 +01:00
|
|
|
});
|
|
|
|
options.AddPolicy(
|
2020-12-07 10:33:15 +01:00
|
|
|
Policies.SyncPlayCreateGroup,
|
2020-12-03 10:43:44 +01:00
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
2020-12-07 10:33:15 +01:00
|
|
|
policy.AddRequirements(new SyncPlayAccessRequirement(SyncPlayAccessRequirementType.CreateGroup));
|
|
|
|
});
|
|
|
|
options.AddPolicy(
|
|
|
|
Policies.SyncPlayJoinGroup,
|
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
|
|
|
policy.AddRequirements(new SyncPlayAccessRequirement(SyncPlayAccessRequirementType.JoinGroup));
|
|
|
|
});
|
|
|
|
options.AddPolicy(
|
|
|
|
Policies.SyncPlayIsInGroup,
|
|
|
|
policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
|
|
|
policy.AddRequirements(new SyncPlayAccessRequirement(SyncPlayAccessRequirementType.IsInGroup));
|
2020-12-03 10:43:44 +01:00
|
|
|
});
|
2019-11-23 19:43:30 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:31:17 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Adds custom legacy authentication to the service collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
|
|
|
/// <returns>The updated service collection.</returns>
|
2019-11-23 19:43:30 +01:00
|
|
|
public static AuthenticationBuilder AddCustomAuthentication(this IServiceCollection serviceCollection)
|
|
|
|
{
|
2019-11-24 19:25:46 +01:00
|
|
|
return serviceCollection.AddAuthentication(AuthenticationSchemes.CustomAuthentication)
|
|
|
|
.AddScheme<AuthenticationSchemeOptions, CustomAuthenticationHandler>(AuthenticationSchemes.CustomAuthentication, null);
|
2019-11-23 19:43:30 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 01:05:15 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets up the proxy configuration based on the addresses in <paramref name="userList"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="networkManager">The <see cref="INetworkManager"/> instance.</param>
|
|
|
|
/// <param name="config">The <see cref="NetworkConfiguration"/> containing the config settings.</param>
|
|
|
|
/// <param name="userList">The string array to parse.</param>
|
|
|
|
/// <param name="options">The <see cref="ForwardedHeadersOptions"/> instance.</param>
|
2021-01-13 16:29:57 +01:00
|
|
|
internal static void ParseList(INetworkManager networkManager, NetworkConfiguration config, string[] userList, ForwardedHeadersOptions options)
|
2021-01-08 01:05:15 +01:00
|
|
|
{
|
|
|
|
for (var i = 0; i < userList.Length; i++)
|
|
|
|
{
|
|
|
|
if (IPNetAddress.TryParse(userList[i], out var addr))
|
|
|
|
{
|
|
|
|
if ((!config.EnableIPV4 && addr.AddressFamily == AddressFamily.InterNetwork)
|
|
|
|
|| (!config.EnableIPV6 && addr.AddressFamily == AddressFamily.InterNetworkV6))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (networkManager.SystemIP6Enabled && addr.AddressFamily == AddressFamily.InterNetwork)
|
|
|
|
{
|
|
|
|
// If the server is using dual-mode sockets, IPv4 addresses are supplied in an IPv6 format.
|
|
|
|
// https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-5.0 .
|
|
|
|
addr.Address = addr.Address.MapToIPv6();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr.PrefixLength == 32)
|
|
|
|
{
|
|
|
|
options.KnownProxies.Add(addr.Address);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
options.KnownNetworks.Add(new IPNetwork(addr.Address, addr.PrefixLength));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (IPHost.TryParse(userList[i], out var host))
|
|
|
|
{
|
|
|
|
foreach (var address in host.GetAddresses())
|
|
|
|
{
|
2021-01-13 16:14:04 +01:00
|
|
|
if ((!config.EnableIPV4 && address.AddressFamily == AddressFamily.InterNetwork)
|
|
|
|
|| (!config.EnableIPV6 && address.AddressFamily == AddressFamily.InterNetworkV6))
|
2021-01-08 01:05:15 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var hostAddr = address;
|
|
|
|
if (networkManager.SystemIP6Enabled && address.AddressFamily == AddressFamily.InterNetwork)
|
|
|
|
{
|
|
|
|
// If the server is using dual-mode sockets, IPv4 addresses are supplied in an IPv6 format.
|
|
|
|
// https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-5.0 .
|
|
|
|
hostAddr = address.MapToIPv6();
|
|
|
|
}
|
|
|
|
|
|
|
|
options.KnownProxies.Add(hostAddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:31:17 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Extension method for adding the jellyfin API to the service collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
2020-09-02 16:03:15 +02:00
|
|
|
/// <param name="pluginAssemblies">An IEnumerable containing all plugin assemblies with API controllers.</param>
|
2021-01-08 01:05:15 +01:00
|
|
|
/// <param name="config">The <see cref="NetworkConfiguration"/>.</param>
|
|
|
|
/// <param name="networkManager">The <see cref="INetworkManager"/> instance.</param>
|
2019-11-23 20:31:17 +01:00
|
|
|
/// <returns>The MVC builder.</returns>
|
2021-01-08 01:05:15 +01:00
|
|
|
public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, IEnumerable<Assembly> pluginAssemblies, NetworkConfiguration config, INetworkManager networkManager)
|
2019-11-23 19:43:30 +01:00
|
|
|
{
|
2020-08-10 16:12:22 +02:00
|
|
|
IMvcBuilder mvcBuilder = serviceCollection
|
2021-01-12 21:43:25 +01:00
|
|
|
.AddCors()
|
|
|
|
.AddTransient<ICorsPolicyProvider, CorsPolicyProvider>()
|
|
|
|
.Configure<ForwardedHeadersOptions>(options =>
|
2020-06-17 16:05:30 +02:00
|
|
|
{
|
2021-01-08 01:05:15 +01:00
|
|
|
// https://github.com/dotnet/aspnetcore/blob/master/src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs
|
|
|
|
// Enable debug logging on Microsoft.AspNetCore.HttpOverrides.ForwardedHeadersMiddleware to help investigate issues.
|
|
|
|
|
2020-06-17 16:05:30 +02:00
|
|
|
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
2021-01-08 01:05:15 +01:00
|
|
|
if (config.KnownProxies.Length == 0)
|
2020-09-10 11:05:46 +02:00
|
|
|
{
|
2021-01-08 01:05:15 +01:00
|
|
|
options.KnownNetworks.Clear();
|
2021-01-12 14:23:10 +01:00
|
|
|
options.KnownProxies.Clear();
|
2020-12-05 10:18:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-08 01:05:15 +01:00
|
|
|
ParseList(networkManager, config, config.KnownProxies, options);
|
2020-09-10 11:05:46 +02:00
|
|
|
}
|
2021-01-08 01:05:15 +01:00
|
|
|
|
|
|
|
// Only set forward limit if we have some known proxies or some known networks.
|
|
|
|
if (options.KnownProxies.Count != 0 || options.KnownNetworks.Count != 0)
|
|
|
|
{
|
|
|
|
options.ForwardLimit = null;
|
|
|
|
}
|
2020-06-17 16:05:30 +02:00
|
|
|
})
|
2020-06-01 19:03:08 +02:00
|
|
|
.AddMvc(opts =>
|
2019-11-23 19:43:30 +01:00
|
|
|
{
|
2020-08-20 19:17:27 +02:00
|
|
|
// Allow requester to change between camelCase and PascalCase
|
|
|
|
opts.RespectBrowserAcceptHeader = true;
|
|
|
|
|
2020-04-20 02:10:59 +02:00
|
|
|
opts.OutputFormatters.Insert(0, new CamelCaseJsonProfileFormatter());
|
|
|
|
opts.OutputFormatters.Insert(0, new PascalCaseJsonProfileFormatter());
|
2020-06-07 00:51:21 +02:00
|
|
|
|
|
|
|
opts.OutputFormatters.Add(new CssOutputFormatter());
|
2020-08-15 18:39:24 +02:00
|
|
|
opts.OutputFormatters.Add(new XmlOutputFormatter());
|
2020-11-21 19:58:35 +01:00
|
|
|
|
|
|
|
opts.ModelBinderProviders.Insert(0, new NullableEnumModelBinderProvider());
|
2019-11-23 19:43:30 +01:00
|
|
|
})
|
2019-11-23 20:31:17 +01:00
|
|
|
|
2019-11-23 19:43:30 +01:00
|
|
|
// Clear app parts to avoid other assemblies being picked up
|
|
|
|
.ConfigureApplicationPartManager(a => a.ApplicationParts.Clear())
|
|
|
|
.AddApplicationPart(typeof(StartupController).Assembly)
|
2020-04-15 08:24:15 +02:00
|
|
|
.AddJsonOptions(options =>
|
2020-04-13 03:17:46 +02:00
|
|
|
{
|
2020-05-21 16:44:15 +02:00
|
|
|
// Update all properties that are set in JsonDefaults
|
2020-06-13 21:11:41 +02:00
|
|
|
var jsonOptions = JsonDefaults.GetPascalCaseOptions();
|
2020-05-21 16:44:15 +02:00
|
|
|
|
|
|
|
// From JsonDefaults
|
|
|
|
options.JsonSerializerOptions.ReadCommentHandling = jsonOptions.ReadCommentHandling;
|
|
|
|
options.JsonSerializerOptions.WriteIndented = jsonOptions.WriteIndented;
|
2020-08-25 15:33:58 +02:00
|
|
|
options.JsonSerializerOptions.DefaultIgnoreCondition = jsonOptions.DefaultIgnoreCondition;
|
2020-08-26 16:22:48 +02:00
|
|
|
options.JsonSerializerOptions.NumberHandling = jsonOptions.NumberHandling;
|
2020-12-15 20:39:41 +01:00
|
|
|
options.JsonSerializerOptions.PropertyNameCaseInsensitive = jsonOptions.PropertyNameCaseInsensitive;
|
2020-08-23 15:48:12 +02:00
|
|
|
|
2020-05-21 16:44:15 +02:00
|
|
|
options.JsonSerializerOptions.Converters.Clear();
|
|
|
|
foreach (var converter in jsonOptions.Converters)
|
|
|
|
{
|
|
|
|
options.JsonSerializerOptions.Converters.Add(converter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// From JsonDefaults.PascalCase
|
|
|
|
options.JsonSerializerOptions.PropertyNamingPolicy = jsonOptions.PropertyNamingPolicy;
|
2020-08-11 17:04:11 +02:00
|
|
|
});
|
2020-08-10 16:12:22 +02:00
|
|
|
|
2020-08-31 17:53:55 +02:00
|
|
|
foreach (Assembly pluginAssembly in pluginAssemblies)
|
2020-08-10 16:12:22 +02:00
|
|
|
{
|
2020-08-11 17:04:11 +02:00
|
|
|
mvcBuilder.AddApplicationPart(pluginAssembly);
|
2020-08-10 16:12:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 17:04:11 +02:00
|
|
|
return mvcBuilder.AddControllersAsServices();
|
2019-11-23 19:43:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-23 20:31:17 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Adds Swagger to the service collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
|
|
|
/// <returns>The updated service collection.</returns>
|
2019-11-23 19:43:30 +01:00
|
|
|
public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
|
|
|
|
{
|
|
|
|
return serviceCollection.AddSwaggerGen(c =>
|
|
|
|
{
|
2020-11-06 21:00:14 +01:00
|
|
|
c.SwaggerDoc("api-docs", new OpenApiInfo
|
|
|
|
{
|
|
|
|
Title = "Jellyfin API",
|
|
|
|
Version = "v1",
|
|
|
|
Extensions = new Dictionary<string, IOpenApiExtension>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
"x-jellyfin-version",
|
|
|
|
new OpenApiString(typeof(ApplicationHost).Assembly.GetName().Version?.ToString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-01 19:12:33 +02:00
|
|
|
c.AddSecurityDefinition(AuthenticationSchemes.CustomAuthentication, new OpenApiSecurityScheme
|
|
|
|
{
|
|
|
|
Type = SecuritySchemeType.ApiKey,
|
|
|
|
In = ParameterLocation.Header,
|
2020-08-30 00:44:33 +02:00
|
|
|
Name = "X-Emby-Authorization",
|
2020-06-01 19:12:33 +02:00
|
|
|
Description = "API key header parameter"
|
|
|
|
});
|
|
|
|
|
2020-04-19 19:24:32 +02:00
|
|
|
// Add all xml doc files to swagger generator.
|
|
|
|
var xmlFiles = Directory.GetFiles(
|
|
|
|
AppContext.BaseDirectory,
|
|
|
|
"*.xml",
|
|
|
|
SearchOption.TopDirectoryOnly);
|
|
|
|
|
|
|
|
foreach (var xmlFile in xmlFiles)
|
|
|
|
{
|
|
|
|
c.IncludeXmlComments(xmlFile);
|
|
|
|
}
|
2020-04-27 07:28:32 +02:00
|
|
|
|
|
|
|
// Order actions by route path, then by http method.
|
|
|
|
c.OrderActionsBy(description =>
|
2020-06-26 01:44:11 +02:00
|
|
|
$"{description.ActionDescriptor.RouteValues["controller"]}_{description.RelativePath}");
|
2020-05-08 16:40:37 +02:00
|
|
|
|
|
|
|
// Use method name as operationId
|
2020-08-03 22:38:51 +02:00
|
|
|
c.CustomOperationIds(
|
|
|
|
description =>
|
|
|
|
{
|
|
|
|
description.TryGetMethodInfo(out MethodInfo methodInfo);
|
|
|
|
// Attribute name, method name, none.
|
|
|
|
return description?.ActionDescriptor?.AttributeRouteInfo?.Name
|
|
|
|
?? methodInfo?.Name
|
|
|
|
?? null;
|
|
|
|
});
|
2020-06-02 19:47:00 +02:00
|
|
|
|
|
|
|
// TODO - remove when all types are supported in System.Text.Json
|
|
|
|
c.AddSwaggerTypeMappings();
|
2020-09-02 01:26:49 +02:00
|
|
|
|
2020-12-02 22:59:57 +01:00
|
|
|
c.OperationFilter<SecurityRequirementsOperationFilter>();
|
2020-09-02 01:26:49 +02:00
|
|
|
c.OperationFilter<FileResponseFilter>();
|
2020-10-01 16:10:47 +02:00
|
|
|
c.DocumentFilter<WebsocketModelFilter>();
|
2019-11-23 19:43:30 +01:00
|
|
|
});
|
|
|
|
}
|
2020-06-02 19:47:00 +02:00
|
|
|
|
|
|
|
private static void AddSwaggerTypeMappings(this SwaggerGenOptions options)
|
|
|
|
{
|
|
|
|
/*
|
2020-11-30 16:47:52 +01:00
|
|
|
* TODO remove when System.Text.Json properly supports non-string keys.
|
|
|
|
* Used in BaseItemDto.ImageBlurHashes
|
2020-06-02 19:47:00 +02:00
|
|
|
*/
|
|
|
|
options.MapType<Dictionary<ImageType, string>>(() =>
|
|
|
|
new OpenApiSchema
|
|
|
|
{
|
|
|
|
Type = "object",
|
2020-11-30 16:47:52 +01:00
|
|
|
AdditionalProperties = new OpenApiSchema
|
|
|
|
{
|
|
|
|
Type = "string"
|
|
|
|
}
|
2020-06-02 19:47:00 +02:00
|
|
|
});
|
2020-06-19 12:24:39 +02:00
|
|
|
|
2020-06-19 15:49:44 +02:00
|
|
|
/*
|
|
|
|
* Support BlurHash dictionary
|
|
|
|
*/
|
2020-06-19 12:24:39 +02:00
|
|
|
options.MapType<Dictionary<ImageType, Dictionary<string, string>>>(() =>
|
|
|
|
new OpenApiSchema
|
|
|
|
{
|
|
|
|
Type = "object",
|
|
|
|
Properties = typeof(ImageType).GetEnumNames().ToDictionary(
|
|
|
|
name => name,
|
|
|
|
name => new OpenApiSchema
|
|
|
|
{
|
2020-11-30 16:47:52 +01:00
|
|
|
Type = "object",
|
|
|
|
AdditionalProperties = new OpenApiSchema
|
2020-06-19 15:49:44 +02:00
|
|
|
{
|
2020-11-30 16:47:52 +01:00
|
|
|
Type = "string"
|
2020-06-19 15:49:44 +02:00
|
|
|
}
|
2020-06-19 12:24:39 +02:00
|
|
|
})
|
|
|
|
});
|
2020-06-02 19:47:00 +02:00
|
|
|
}
|
2019-11-23 19:43:30 +01:00
|
|
|
}
|
|
|
|
}
|