2
0

ApiServiceCollectionExtensions.cs 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // Setting the naming policy to null leaves the property names as-is when serializing objects to JSON.
  73. options.JsonSerializerOptions.PropertyNamingPolicy = null;
  74. })
  75. .AddControllersAsServices();
  76. }
  77. /// <summary>
  78. /// Adds Swagger to the service collection.
  79. /// </summary>
  80. /// <param name="serviceCollection">The service collection.</param>
  81. /// <returns>The updated service collection.</returns>
  82. public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
  83. {
  84. return serviceCollection.AddSwaggerGen(c =>
  85. {
  86. c.SwaggerDoc("v1", new OpenApiInfo { Title = "Jellyfin API", Version = "v1" });
  87. });
  88. }
  89. }
  90. }