ApiServiceCollectionExtensions.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.Json.Serialization;
  6. using Jellyfin.Api;
  7. using Jellyfin.Api.Auth;
  8. using Jellyfin.Api.Auth.FirstTimeSetupOrElevatedPolicy;
  9. using Jellyfin.Api.Auth.RequiresElevationPolicy;
  10. using Jellyfin.Api.Constants;
  11. using Jellyfin.Api.Controllers;
  12. using Microsoft.AspNetCore.Authentication;
  13. using Microsoft.AspNetCore.Authorization;
  14. using Microsoft.Extensions.DependencyInjection;
  15. using Microsoft.OpenApi.Models;
  16. namespace Jellyfin.Server.Extensions
  17. {
  18. /// <summary>
  19. /// API specific extensions for the service collection.
  20. /// </summary>
  21. public static class ApiServiceCollectionExtensions
  22. {
  23. /// <summary>
  24. /// Adds jellyfin API authorization policies to the DI container.
  25. /// </summary>
  26. /// <param name="serviceCollection">The service collection.</param>
  27. /// <returns>The updated service collection.</returns>
  28. public static IServiceCollection AddJellyfinApiAuthorization(this IServiceCollection serviceCollection)
  29. {
  30. serviceCollection.AddSingleton<IAuthorizationHandler, FirstTimeSetupOrElevatedHandler>();
  31. serviceCollection.AddSingleton<IAuthorizationHandler, RequiresElevationHandler>();
  32. return serviceCollection.AddAuthorizationCore(options =>
  33. {
  34. options.AddPolicy(
  35. Policies.RequiresElevation,
  36. policy =>
  37. {
  38. policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
  39. policy.AddRequirements(new RequiresElevationRequirement());
  40. });
  41. options.AddPolicy(
  42. Policies.FirstTimeSetupOrElevated,
  43. policy =>
  44. {
  45. policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
  46. policy.AddRequirements(new FirstTimeSetupOrElevatedRequirement());
  47. });
  48. });
  49. }
  50. /// <summary>
  51. /// Adds custom legacy authentication to the service collection.
  52. /// </summary>
  53. /// <param name="serviceCollection">The service collection.</param>
  54. /// <returns>The updated service collection.</returns>
  55. public static AuthenticationBuilder AddCustomAuthentication(this IServiceCollection serviceCollection)
  56. {
  57. return serviceCollection.AddAuthentication(AuthenticationSchemes.CustomAuthentication)
  58. .AddScheme<AuthenticationSchemeOptions, CustomAuthenticationHandler>(AuthenticationSchemes.CustomAuthentication, null);
  59. }
  60. /// <summary>
  61. /// Extension method for adding the jellyfin API to the service collection.
  62. /// </summary>
  63. /// <param name="serviceCollection">The service collection.</param>
  64. /// <param name="baseUrl">The base url for the API.</param>
  65. /// <returns>The MVC builder.</returns>
  66. public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, string baseUrl)
  67. {
  68. return serviceCollection.AddMvc(opts =>
  69. {
  70. opts.UseGeneralRoutePrefix(baseUrl);
  71. })
  72. // Clear app parts to avoid other assemblies being picked up
  73. .ConfigureApplicationPartManager(a => a.ApplicationParts.Clear())
  74. .AddApplicationPart(typeof(StartupController).Assembly)
  75. .AddJsonOptions(options =>
  76. {
  77. // Setting the naming policy to null leaves the property names as-is when serializing objects to JSON.
  78. options.JsonSerializerOptions.PropertyNamingPolicy = null;
  79. // Accept string enums
  80. options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
  81. })
  82. .AddControllersAsServices();
  83. }
  84. /// <summary>
  85. /// Adds Swagger to the service collection.
  86. /// </summary>
  87. /// <param name="serviceCollection">The service collection.</param>
  88. /// <returns>The updated service collection.</returns>
  89. public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
  90. {
  91. return serviceCollection.AddSwaggerGen(c =>
  92. {
  93. c.SwaggerDoc("v1", new OpenApiInfo { Title = "Jellyfin API", Version = "v1" });
  94. // Add all xml doc files to swagger generator.
  95. var xmlFiles = Directory.GetFiles(
  96. AppContext.BaseDirectory,
  97. "*.xml",
  98. SearchOption.TopDirectoryOnly);
  99. foreach (var xmlFile in xmlFiles)
  100. {
  101. c.IncludeXmlComments(xmlFile);
  102. }
  103. });
  104. }
  105. }
  106. }