You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.8 KiB
90 lines
3.8 KiB
5 years ago
|
using Jellyfin.Api;
|
||
5 years ago
|
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.Extensions.DependencyInjection;
|
||
|
using Microsoft.OpenApi.Models;
|
||
|
|
||
5 years ago
|
namespace Jellyfin.Server.Extensions
|
||
5 years ago
|
{
|
||
5 years ago
|
/// <summary>
|
||
|
/// API specific extensions for the service collection.
|
||
|
/// </summary>
|
||
5 years ago
|
public static class ApiServiceCollectionExtensions
|
||
|
{
|
||
5 years ago
|
/// <summary>
|
||
|
/// Adds jellyfin API authorization policies to the DI container.
|
||
|
/// </summary>
|
||
|
/// <param name="serviceCollection">The service collection.</param>
|
||
|
/// <returns>The updated service collection.</returns>
|
||
5 years ago
|
public static IServiceCollection AddJellyfinApiAuthorization(this IServiceCollection serviceCollection)
|
||
|
{
|
||
|
serviceCollection.AddSingleton<IAuthorizationHandler, FirstTimeSetupOrElevatedHandler>();
|
||
|
serviceCollection.AddSingleton<IAuthorizationHandler, RequiresElevationHandler>();
|
||
|
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());
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
5 years ago
|
/// <summary>
|
||
|
/// Adds custom legacy authentication to the service collection.
|
||
|
/// </summary>
|
||
|
/// <param name="serviceCollection">The service collection.</param>
|
||
|
/// <returns>The updated service collection.</returns>
|
||
5 years ago
|
public static AuthenticationBuilder AddCustomAuthentication(this IServiceCollection serviceCollection)
|
||
|
{
|
||
|
return serviceCollection.AddAuthentication("CustomAuthentication")
|
||
|
.AddScheme<AuthenticationSchemeOptions, CustomAuthenticationHandler>("CustomAuthentication", null);
|
||
|
}
|
||
|
|
||
5 years ago
|
/// <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>
|
||
5 years ago
|
public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, string baseUrl)
|
||
|
{
|
||
|
return serviceCollection.AddMvc(opts =>
|
||
|
{
|
||
|
opts.UseGeneralRoutePrefix(baseUrl);
|
||
|
})
|
||
5 years ago
|
|
||
5 years ago
|
// Clear app parts to avoid other assemblies being picked up
|
||
|
.ConfigureApplicationPartManager(a => a.ApplicationParts.Clear())
|
||
|
.AddApplicationPart(typeof(StartupController).Assembly)
|
||
|
.AddControllersAsServices();
|
||
|
}
|
||
|
|
||
5 years ago
|
/// <summary>
|
||
|
/// Adds Swagger to the service collection.
|
||
|
/// </summary>
|
||
|
/// <param name="serviceCollection">The service collection.</param>
|
||
|
/// <returns>The updated service collection.</returns>
|
||
5 years ago
|
public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
|
||
|
{
|
||
|
return serviceCollection.AddSwaggerGen(c =>
|
||
|
{
|
||
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Jellyfin API", Version = "v1" });
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|