using Jellyfin.Api.Auth; using Jellyfin.Api.Auth.FirstTimeSetupOrElevatedPolicy; using Jellyfin.Api.Auth.RequiresElevationPolicy; using Jellyfin.Api.Controllers; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; namespace Jellyfin.Api.Extensions { /// /// API specific extensions for the service collection. /// public static class ApiServiceCollectionExtensions { /// /// Adds jellyfin API authorization policies to the DI container. /// /// The service collection. /// The updated service collection. public static IServiceCollection AddJellyfinApiAuthorization(this IServiceCollection serviceCollection) { serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); return serviceCollection.AddAuthorizationCore(options => { options.AddPolicy( "RequiresElevation", policy => { policy.AddAuthenticationSchemes("CustomAuthentication"); policy.AddRequirements(new RequiresElevationRequirement()); }); options.AddPolicy( "FirstTimeSetupOrElevated", policy => { policy.AddAuthenticationSchemes("CustomAuthentication"); policy.AddRequirements(new FirstTimeSetupOrElevatedRequirement()); }); }); } /// /// Adds custom legacy authentication to the service collection. /// /// The service collection. /// The updated service collection. public static AuthenticationBuilder AddCustomAuthentication(this IServiceCollection serviceCollection) { return serviceCollection.AddAuthentication("CustomAuthentication") .AddScheme("CustomAuthentication", null); } /// /// Extension method for adding the jellyfin API to the service collection. /// /// The service collection. /// The base url for the API. /// The MVC builder. public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, string baseUrl) { return serviceCollection.AddMvc(opts => { var policy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); opts.Filters.Add(new AuthorizeFilter(policy)); opts.EnableEndpointRouting = false; opts.UseGeneralRoutePrefix(baseUrl); }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2) // Clear app parts to avoid other assemblies being picked up .ConfigureApplicationPartManager(a => a.ApplicationParts.Clear()) .AddApplicationPart(typeof(StartupController).Assembly) .AddControllersAsServices(); } /// /// Adds Swagger to the service collection. /// /// The service collection. /// The updated service collection. public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection) { return serviceCollection.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Jellyfin API", Version = "v1" }); }); } } }