ApiServiceCollectionExtensions.cs 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.Formatters;
  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. opts.OutputFormatters.Insert(0, new CamelCaseJsonProfileFormatter());
  68. opts.OutputFormatters.Insert(0, new PascalCaseJsonProfileFormatter());
  69. })
  70. // Clear app parts to avoid other assemblies being picked up
  71. .ConfigureApplicationPartManager(a => a.ApplicationParts.Clear())
  72. .AddApplicationPart(typeof(StartupController).Assembly)
  73. .AddJsonOptions(options =>
  74. {
  75. // Setting the naming policy to null leaves the property names as-is when serializing objects to JSON.
  76. options.JsonSerializerOptions.PropertyNamingPolicy = null;
  77. })
  78. .AddControllersAsServices();
  79. }
  80. /// <summary>
  81. /// Adds Swagger to the service collection.
  82. /// </summary>
  83. /// <param name="serviceCollection">The service collection.</param>
  84. /// <returns>The updated service collection.</returns>
  85. public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
  86. {
  87. return serviceCollection.AddSwaggerGen(c =>
  88. {
  89. c.SwaggerDoc("v1", new OpenApiInfo { Title = "Jellyfin API", Version = "v1" });
  90. });
  91. }
  92. }
  93. }