ApiServiceCollectionExtensions.cs 5.6 KB

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