2020-04-19 10:24:32 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text.Json.Serialization;
|
2019-11-24 07:27:58 -07:00
|
|
|
using Jellyfin.Api;
|
2019-11-23 11:43:30 -07:00
|
|
|
using Jellyfin.Api.Auth;
|
|
|
|
using Jellyfin.Api.Auth.FirstTimeSetupOrElevatedPolicy;
|
|
|
|
using Jellyfin.Api.Auth.RequiresElevationPolicy;
|
2019-11-24 11:25:46 -07:00
|
|
|
using Jellyfin.Api.Constants;
|
2019-11-23 11:43:30 -07:00
|
|
|
using Jellyfin.Api.Controllers;
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
2019-11-24 07:27:58 -07:00
|
|
|
namespace Jellyfin.Server.Extensions
|
2019-11-23 11:43:30 -07:00
|
|
|
{
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <summary>
|
|
|
|
/// API specific extensions for the service collection.
|
|
|
|
/// </summary>
|
2019-11-23 11:43:30 -07:00
|
|
|
public static class ApiServiceCollectionExtensions
|
|
|
|
{
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Adds jellyfin API authorization policies to the DI container.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
|
|
|
/// <returns>The updated service collection.</returns>
|
2019-11-23 11:43:30 -07:00
|
|
|
public static IServiceCollection AddJellyfinApiAuthorization(this IServiceCollection serviceCollection)
|
|
|
|
{
|
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, FirstTimeSetupOrElevatedHandler>();
|
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, RequiresElevationHandler>();
|
|
|
|
return serviceCollection.AddAuthorizationCore(options =>
|
|
|
|
{
|
|
|
|
options.AddPolicy(
|
2019-11-24 11:25:46 -07:00
|
|
|
Policies.RequiresElevation,
|
2019-11-23 11:43:30 -07:00
|
|
|
policy =>
|
|
|
|
{
|
2019-11-24 11:25:46 -07:00
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
2019-11-23 11:43:30 -07:00
|
|
|
policy.AddRequirements(new RequiresElevationRequirement());
|
|
|
|
});
|
|
|
|
options.AddPolicy(
|
2019-11-24 11:25:46 -07:00
|
|
|
Policies.FirstTimeSetupOrElevated,
|
2019-11-23 11:43:30 -07:00
|
|
|
policy =>
|
|
|
|
{
|
2019-11-24 11:25:46 -07:00
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication);
|
2019-11-23 11:43:30 -07:00
|
|
|
policy.AddRequirements(new FirstTimeSetupOrElevatedRequirement());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Adds custom legacy authentication to the service collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
|
|
|
/// <returns>The updated service collection.</returns>
|
2019-11-23 11:43:30 -07:00
|
|
|
public static AuthenticationBuilder AddCustomAuthentication(this IServiceCollection serviceCollection)
|
|
|
|
{
|
2019-11-24 11:25:46 -07:00
|
|
|
return serviceCollection.AddAuthentication(AuthenticationSchemes.CustomAuthentication)
|
|
|
|
.AddScheme<AuthenticationSchemeOptions, CustomAuthenticationHandler>(AuthenticationSchemes.CustomAuthentication, null);
|
2019-11-23 11:43:30 -07:00
|
|
|
}
|
|
|
|
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Extension method for adding the jellyfin API to the service collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
|
|
|
/// <param name="baseUrl">The base url for the API.</param>
|
|
|
|
/// <returns>The MVC builder.</returns>
|
2019-11-23 11:43:30 -07:00
|
|
|
public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, string baseUrl)
|
|
|
|
{
|
|
|
|
return serviceCollection.AddMvc(opts =>
|
|
|
|
{
|
|
|
|
opts.UseGeneralRoutePrefix(baseUrl);
|
|
|
|
})
|
2019-11-23 12:31:17 -07:00
|
|
|
|
2019-11-23 11:43:30 -07:00
|
|
|
// Clear app parts to avoid other assemblies being picked up
|
|
|
|
.ConfigureApplicationPartManager(a => a.ApplicationParts.Clear())
|
|
|
|
.AddApplicationPart(typeof(StartupController).Assembly)
|
2020-04-14 23:24:15 -07:00
|
|
|
.AddJsonOptions(options =>
|
2020-04-12 18:17:46 -07:00
|
|
|
{
|
2020-04-15 16:02:43 -07:00
|
|
|
// Setting the naming policy to null leaves the property names as-is when serializing objects to JSON.
|
2020-04-12 18:17:46 -07:00
|
|
|
options.JsonSerializerOptions.PropertyNamingPolicy = null;
|
|
|
|
})
|
2019-11-23 11:43:30 -07:00
|
|
|
.AddControllersAsServices();
|
|
|
|
}
|
|
|
|
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Adds Swagger to the service collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
|
|
|
/// <returns>The updated service collection.</returns>
|
2019-11-23 11:43:30 -07:00
|
|
|
public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
|
|
|
|
{
|
|
|
|
return serviceCollection.AddSwaggerGen(c =>
|
|
|
|
{
|
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Jellyfin API", Version = "v1" });
|
2020-04-19 10:24:32 -07:00
|
|
|
|
|
|
|
// Add all xml doc files to swagger generator.
|
|
|
|
var xmlFiles = Directory.GetFiles(
|
|
|
|
AppContext.BaseDirectory,
|
|
|
|
"*.xml",
|
|
|
|
SearchOption.TopDirectoryOnly);
|
|
|
|
|
|
|
|
foreach (var xmlFile in xmlFiles)
|
|
|
|
{
|
|
|
|
c.IncludeXmlComments(xmlFile);
|
|
|
|
}
|
2019-11-23 11:43:30 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|