ApiServiceCollectionExtensions.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.Json.Serialization;
  6. using Jellyfin.Api;
  7. using Jellyfin.Api.Auth;
  8. using Jellyfin.Api.Auth.FirstTimeSetupOrElevatedPolicy;
  9. using Jellyfin.Api.Auth.RequiresElevationPolicy;
  10. using Jellyfin.Api.Constants;
  11. using Jellyfin.Api.Controllers;
  12. using Jellyfin.Server.Formatters;
  13. using Microsoft.AspNetCore.Authentication;
  14. using Microsoft.AspNetCore.Authorization;
  15. using Microsoft.Extensions.DependencyInjection;
  16. using Microsoft.OpenApi.Models;
  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. // Setting the naming policy to null leaves the property names as-is when serializing objects to JSON.
  81. options.JsonSerializerOptions.PropertyNamingPolicy = null;
  82. })
  83. .AddControllersAsServices();
  84. }
  85. /// <summary>
  86. /// Adds Swagger to the service collection.
  87. /// </summary>
  88. /// <param name="serviceCollection">The service collection.</param>
  89. /// <returns>The updated service collection.</returns>
  90. public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
  91. {
  92. return serviceCollection.AddSwaggerGen(c =>
  93. {
  94. c.SwaggerDoc("api-docs", new OpenApiInfo { Title = "Jellyfin API" });
  95. // Add all xml doc files to swagger generator.
  96. var xmlFiles = Directory.GetFiles(
  97. AppContext.BaseDirectory,
  98. "*.xml",
  99. SearchOption.TopDirectoryOnly);
  100. foreach (var xmlFile in xmlFiles)
  101. {
  102. c.IncludeXmlComments(xmlFile);
  103. }
  104. // Order actions by route path, then by http method.
  105. c.OrderActionsBy(description =>
  106. $"{description.ActionDescriptor.RouteValues["controller"]}_{description.HttpMethod}");
  107. });
  108. }
  109. }
  110. }