ApiServiceCollectionExtensions.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using Jellyfin.Api;
  5. using Jellyfin.Api.Auth;
  6. using Jellyfin.Api.Auth.FirstTimeSetupOrElevatedPolicy;
  7. using Jellyfin.Api.Auth.RequiresElevationPolicy;
  8. using Jellyfin.Api.Constants;
  9. using Jellyfin.Api.Controllers;
  10. using Jellyfin.Server.Formatters;
  11. using MediaBrowser.Common.Json;
  12. using Microsoft.AspNetCore.Authentication;
  13. using Microsoft.AspNetCore.Authorization;
  14. using Microsoft.Extensions.DependencyInjection;
  15. using Microsoft.OpenApi.Models;
  16. using Swashbuckle.AspNetCore.SwaggerGen;
  17. namespace Jellyfin.Server.Extensions
  18. {
  19. /// <summary>
  20. /// API specific extensions for the service collection.
  21. /// </summary>
  22. public static class ApiServiceCollectionExtensions
  23. {
  24. /// <summary>
  25. /// Adds jellyfin API authorization policies to the DI container.
  26. /// </summary>
  27. /// <param name="serviceCollection">The service collection.</param>
  28. /// <returns>The updated service collection.</returns>
  29. public static IServiceCollection AddJellyfinApiAuthorization(this IServiceCollection serviceCollection)
  30. {
  31. serviceCollection.AddSingleton<IAuthorizationHandler, FirstTimeSetupOrElevatedHandler>();
  32. serviceCollection.AddSingleton<IAuthorizationHandler, RequiresElevationHandler>();
  33. return serviceCollection.AddAuthorizationCore(options =>
  34. {
  35. options.AddPolicy(
  36. Policies.RequiresElevation,
  37. policy =>
  38. {
  39. policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
  40. policy.AddRequirements(new RequiresElevationRequirement());
  41. });
  42. options.AddPolicy(
  43. Policies.FirstTimeSetupOrElevated,
  44. policy =>
  45. {
  46. policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
  47. policy.AddRequirements(new FirstTimeSetupOrElevatedRequirement());
  48. });
  49. });
  50. }
  51. /// <summary>
  52. /// Adds custom legacy authentication to the service collection.
  53. /// </summary>
  54. /// <param name="serviceCollection">The service collection.</param>
  55. /// <returns>The updated service collection.</returns>
  56. public static AuthenticationBuilder AddCustomAuthentication(this IServiceCollection serviceCollection)
  57. {
  58. return serviceCollection.AddAuthentication(AuthenticationSchemes.CustomAuthentication)
  59. .AddScheme<AuthenticationSchemeOptions, CustomAuthenticationHandler>(AuthenticationSchemes.CustomAuthentication, null);
  60. }
  61. /// <summary>
  62. /// Extension method for adding the jellyfin API to the service collection.
  63. /// </summary>
  64. /// <param name="serviceCollection">The service collection.</param>
  65. /// <param name="baseUrl">The base url for the API.</param>
  66. /// <returns>The MVC builder.</returns>
  67. public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, string baseUrl)
  68. {
  69. return serviceCollection.AddMvc(opts =>
  70. {
  71. opts.UseGeneralRoutePrefix(baseUrl);
  72. opts.OutputFormatters.Insert(0, new CamelCaseJsonProfileFormatter());
  73. opts.OutputFormatters.Insert(0, new PascalCaseJsonProfileFormatter());
  74. })
  75. // Clear app parts to avoid other assemblies being picked up
  76. .ConfigureApplicationPartManager(a => a.ApplicationParts.Clear())
  77. .AddApplicationPart(typeof(StartupController).Assembly)
  78. .AddJsonOptions(options =>
  79. {
  80. // Update all properties that are set in JsonDefaults
  81. var jsonOptions = JsonDefaults.PascalCase;
  82. // From JsonDefaults
  83. options.JsonSerializerOptions.ReadCommentHandling = jsonOptions.ReadCommentHandling;
  84. options.JsonSerializerOptions.WriteIndented = jsonOptions.WriteIndented;
  85. options.JsonSerializerOptions.Converters.Clear();
  86. foreach (var converter in jsonOptions.Converters)
  87. {
  88. options.JsonSerializerOptions.Converters.Add(converter);
  89. }
  90. // From JsonDefaults.PascalCase
  91. options.JsonSerializerOptions.PropertyNamingPolicy = jsonOptions.PropertyNamingPolicy;
  92. })
  93. .AddControllersAsServices();
  94. }
  95. /// <summary>
  96. /// Adds Swagger to the service collection.
  97. /// </summary>
  98. /// <param name="serviceCollection">The service collection.</param>
  99. /// <returns>The updated service collection.</returns>
  100. public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
  101. {
  102. return serviceCollection.AddSwaggerGen(c =>
  103. {
  104. c.SwaggerDoc("api-docs", new OpenApiInfo { Title = "Jellyfin API", Version = "v1" });
  105. // Add all xml doc files to swagger generator.
  106. var xmlFiles = Directory.GetFiles(
  107. AppContext.BaseDirectory,
  108. "*.xml",
  109. SearchOption.TopDirectoryOnly);
  110. foreach (var xmlFile in xmlFiles)
  111. {
  112. c.IncludeXmlComments(xmlFile);
  113. }
  114. // Order actions by route path, then by http method.
  115. c.OrderActionsBy(description =>
  116. $"{description.ActionDescriptor.RouteValues["controller"]}_{description.HttpMethod}");
  117. // Use method name as operationId
  118. c.CustomOperationIds(description =>
  119. description.TryGetMethodInfo(out MethodInfo methodInfo) ? methodInfo.Name : null);
  120. });
  121. }
  122. }
  123. }