ApiServiceCollectionExtensions.cs 4.3 KB

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