ApiServiceCollectionExtensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. .AddControllersAsServices();
  71. }
  72. /// <summary>
  73. /// Adds Swagger to the service collection.
  74. /// </summary>
  75. /// <param name="serviceCollection">The service collection.</param>
  76. /// <returns>The updated service collection.</returns>
  77. public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
  78. {
  79. return serviceCollection.AddSwaggerGen(c =>
  80. {
  81. c.SwaggerDoc("v1", new OpenApiInfo { Title = "Jellyfin API", Version = "v1" });
  82. });
  83. }
  84. }
  85. }