2
0

ApiServiceCollectionExtensions.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Jellyfin.Api.Auth;
  2. using Jellyfin.Api.Auth.FirstTimeSetupOrElevatedPolicy;
  3. using Jellyfin.Api.Auth.RequiresElevationPolicy;
  4. using Jellyfin.Api.Controllers;
  5. using Microsoft.AspNetCore.Authentication;
  6. using Microsoft.AspNetCore.Authorization;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.Mvc.Authorization;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.OpenApi.Models;
  11. namespace Jellyfin.Api.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. "RequiresElevation",
  31. policy =>
  32. {
  33. policy.AddAuthenticationSchemes("CustomAuthentication");
  34. policy.AddRequirements(new RequiresElevationRequirement());
  35. });
  36. options.AddPolicy(
  37. "FirstTimeSetupOrElevated",
  38. policy =>
  39. {
  40. policy.AddAuthenticationSchemes("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("CustomAuthentication")
  53. .AddScheme<AuthenticationSchemeOptions, CustomAuthenticationHandler>("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. var policy = new AuthorizationPolicyBuilder()
  66. .RequireAuthenticatedUser()
  67. .Build();
  68. opts.Filters.Add(new AuthorizeFilter(policy));
  69. opts.EnableEndpointRouting = false;
  70. opts.UseGeneralRoutePrefix(baseUrl);
  71. })
  72. .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
  73. // Clear app parts to avoid other assemblies being picked up
  74. .ConfigureApplicationPartManager(a => a.ApplicationParts.Clear())
  75. .AddApplicationPart(typeof(StartupController).Assembly)
  76. .AddControllersAsServices();
  77. }
  78. /// <summary>
  79. /// Adds Swagger to the service collection.
  80. /// </summary>
  81. /// <param name="serviceCollection">The service collection.</param>
  82. /// <returns>The updated service collection.</returns>
  83. public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
  84. {
  85. return serviceCollection.AddSwaggerGen(c =>
  86. {
  87. c.SwaggerDoc("v1", new OpenApiInfo { Title = "Jellyfin API", Version = "v1" });
  88. });
  89. }
  90. }
  91. }