ApiServiceCollectionExtensions.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Jellyfin.Api;
  2. using Jellyfin.Api.Auth;
  3. using Jellyfin.Api.Auth.FirstTimeSetupOrElevatedPolicy;
  4. using Jellyfin.Api.Auth.RequiresElevationPolicy;
  5. using Jellyfin.Api.Constants;
  6. using Jellyfin.Api.Controllers;
  7. using Microsoft.AspNetCore.Authentication;
  8. using Microsoft.AspNetCore.Authorization;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.OpenApi.Models;
  11. namespace Jellyfin.Server.Extensions
  12. {
  13. /// <summary>
  14. /// API specific extensions for the service collection.
  15. /// </summary>
  16. public static class ApiServiceCollectionExtensions
  17. {
  18. /// <summary>
  19. /// Adds jellyfin API authorization policies to the DI container.
  20. /// </summary>
  21. /// <param name="serviceCollection">The service collection.</param>
  22. /// <returns>The updated service collection.</returns>
  23. public static IServiceCollection AddJellyfinApiAuthorization(this IServiceCollection serviceCollection)
  24. {
  25. serviceCollection.AddSingleton<IAuthorizationHandler, FirstTimeSetupOrElevatedHandler>();
  26. serviceCollection.AddSingleton<IAuthorizationHandler, RequiresElevationHandler>();
  27. return serviceCollection.AddAuthorizationCore(options =>
  28. {
  29. options.AddPolicy(
  30. Policies.RequiresElevation,
  31. policy =>
  32. {
  33. policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
  34. policy.AddRequirements(new RequiresElevationRequirement());
  35. });
  36. options.AddPolicy(
  37. Policies.FirstTimeSetupOrElevated,
  38. policy =>
  39. {
  40. policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
  41. policy.AddRequirements(new FirstTimeSetupOrElevatedRequirement());
  42. });
  43. });
  44. }
  45. /// <summary>
  46. /// Adds custom legacy authentication to the service collection.
  47. /// </summary>
  48. /// <param name="serviceCollection">The service collection.</param>
  49. /// <returns>The updated service collection.</returns>
  50. public static AuthenticationBuilder AddCustomAuthentication(this IServiceCollection serviceCollection)
  51. {
  52. return serviceCollection.AddAuthentication(AuthenticationSchemes.CustomAuthentication)
  53. .AddScheme<AuthenticationSchemeOptions, CustomAuthenticationHandler>(AuthenticationSchemes.CustomAuthentication, null);
  54. }
  55. /// <summary>
  56. /// Extension method for adding the jellyfin API to the service collection.
  57. /// </summary>
  58. /// <param name="serviceCollection">The service collection.</param>
  59. /// <param name="baseUrl">The base url for the API.</param>
  60. /// <returns>The MVC builder.</returns>
  61. public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, string baseUrl)
  62. {
  63. return serviceCollection.AddMvc(opts =>
  64. {
  65. opts.UseGeneralRoutePrefix(baseUrl);
  66. })
  67. // Clear app parts to avoid other assemblies being picked up
  68. .ConfigureApplicationPartManager(a => a.ApplicationParts.Clear())
  69. .AddApplicationPart(typeof(StartupController).Assembly)
  70. .AddJsonOptions(options =>
  71. {
  72. options.JsonSerializerOptions.PropertyNamingPolicy = null;
  73. })
  74. .AddControllersAsServices();
  75. }
  76. /// <summary>
  77. /// Adds Swagger to the service collection.
  78. /// </summary>
  79. /// <param name="serviceCollection">The service collection.</param>
  80. /// <returns>The updated service collection.</returns>
  81. public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
  82. {
  83. return serviceCollection.AddSwaggerGen(c =>
  84. {
  85. c.SwaggerDoc("v1", new OpenApiInfo { Title = "Jellyfin API", Version = "v1" });
  86. });
  87. }
  88. }
  89. }